-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinstall.sh
executable file
·223 lines (201 loc) · 7.18 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd 2>/dev/null)
source "${SCRIPT_DIR}/install_helpers"
get_collectd_config() {
printf "Getting config file for collectd..."
COLLECTD_CONFIG=$(${COLLECTD} -h 2>/dev/null | grep 'Config file' | awk '{ print $3; }')
if [ -z "$COLLECTD_CONFIG" ]; then
echo "Failed"
exit 2;
else
echo "Success";
fi
COLLECTD_ETC=$(dirname "${COLLECTD_CONFIG}")
if [ "$COLLECTD_ETC" == "/etc" ]; then
COLLECTD_ETC="/etc/collectd.d"
printf "Making /etc/collectd.d..."
mkdir -p ${COLLECTD_ETC};
check_for_err "Success\n";
fi
COLLECTD_MANAGED_CONFIG_DIR=${COLLECTD_ETC}/managed_config
printf "Getting TypesDB default value..."
if [ -x /usr/bin/strings ]; then
TYPESDB=$(strings "${COLLECTD}" | grep /types.db)
else
TYPESDB=$(grep -oP -a "/[-_/[:alpha:]0-9]+/types.db\x00" "${COLLECTD}")
fi
if [ -z "$TYPESDB" ]; then
echo "FAILED"
exit 2;
else
echo "Success";
fi
find_collectd_ver
}
get_source_config() {
if [ -z "$SOURCE_TYPE" ]; then
echo "There are three ways to configure the source name to be used by collectd"
echo "when reporting metrics."
echo "dns - Use the name of the host by resolving it in dns"
echo "input - You can enter a hostname to use as the source name"
echo "aws - Use the AWS instance id. This is is helpful if you use tags"
echo " or other AWS attributes to group metrics"
echo
read -p "How would you like to configure your Hostname? (dns, input, or aws): " SOURCE_TYPE < /dev/tty
while [ "$SOURCE_TYPE" != "dns" -a "$SOURCE_TYPE" != "input" -a "$SOURCE_TYPE" != "aws" ]; do
read -p "Invalid answer. How would you like to configure your Hostname? (dns, input, or aws): " SOURCE_TYPE < /dev/tty
done
fi
case $SOURCE_TYPE in
"aws")
printf "Fetching AWS instance id.."
SOURCE_NAME_INFO="Hostname \"$(curl -s http://169.254.169.254/latest/meta-data/instance-id)\""
if [ -z "${SOURCE_NAME_INFO}" ]; then
echo "FAILED";
else
echo "Success";
fi
;;
"input")
if [ -z "$INPUT_HOSTNAME" ]; then
read -p "Input hostname value: " INPUT_HOSTNAME < /dev/tty
while [ -z "$INPUT_HOSTNAME" ]; do
read -p "Invalid input. Input hostname value: " INPUT_HOSTNAME < /dev/tty
done
fi
SOURCE_NAME_INFO="Hostname \"${INPUT_HOSTNAME}\""
;;
"dns")
SOURCE_NAME_INFO="FQDNLookup true"
;;
*)
echo "Invalid SOURCE_TYPE value ${SOURCE_TYPE}";
exit 2;
esac
}
usage(){
echo "$0 [-s SOURCE_TYPE] [-t API_TOKEN] [-u SIGNALFX_USER]"
echo " [-o SIGNALFX_ORG] [-H HOSTNAME] [/path/to/collectd]"
echo "Installs collectd.conf and configures it for talking to SignalFx."
echo "If path to collectd is not specified then it will be searched for in well know places."
echo
echo " -s SOURCE_TYPE : How to configure the Hostname field in collectd.conf:"
echo " aws - use the aws instance id."
echo " input - set a hostname. See --hostname"
echo " dns - use FQDN of the host as the Hostname"
echo
echo " -H HOSTNAME: The Hostname value to use if you selected hostname as your source_type"
echo
echo " Configuring SignalFX access"
echo "------------------------------"
echo " -t API_TOKEN: If you already know your SignalFx API Token you can specify it."
echo " -u SIGNALFX_USER: The SignalFx user name to use to fetch a user token"
echo " -o SIGNALFX_ORG: If the SignalFxe user is part of more than one organization this"
echo " parameter is required."
echo
exit "$1";
}
parse_args(){
while getopts ":s:t:u:o:H:h" opt; do
case "$opt" in
s)
SOURCE_TYPE="$OPTARG" ;;
H)
INPUT_HOSTNAME="$OPTARG" ;;
t)
API_TOKEN="$OPTARG" ;;
u)
SFX_USER="$OPTARG" ;;
o)
SFX_ORG="--org=$OPTARG" ;;
h)
usage 0; ;;
\?) echo "Invalid option: -$OPTARG" >&2;
exit 2;
;;
:) echo "Option -$OPTARG requires an argument." >&2;
exit 2;
;;
*) break ;;
esac
done
COLLECTD=${@:$OPTIND:1}
if [ -z "${COLLECTD}" ]; then
find_installed_collectd
if [ -z "${COLLECTD}" ]; then
echo "Unable to find collectd"
usage 2
else
echo "Collectd not specified using: ${COLLECTD}"
fi
fi
}
install_config(){
printf "Installing %s.." "$2"
cp "${MANAGED_CONF_DIR}/$1" "${COLLECTD_MANAGED_CONFIG_DIR}"
check_for_err "Success\n"
}
install_write_http_plugin(){
if [ -z "$API_TOKEN" ]; then
if [ -z "${SFX_USER}" ]; then
read -p "Input SignalFx user name: " SFX_USER < /dev/tty
while [ -z "${SFX_USER}" ]; do
read -p "Invalid input. Input SignalFx user name: " SFX_USER < /dev/tty
done
fi
API_TOKEN=$(python ${SCRIPT_DIR}/get_all_auth_tokens.py --error_on_multiple ${SFX_ORG} "${SFX_USER}")
if [ -z "$API_TOKEN" ]; then
echo "Failed to get SignalFx API token";
exit 2;
fi
fi
printf "Fixing SignalFX plugin configuration.."
sed -e "s#%%%API_TOKEN%%%#${API_TOKEN}#" \
"${MANAGED_CONF_DIR}/10-write_http-plugin.conf" > "${COLLECTD_MANAGED_CONFIG_DIR}/10-write_http-plugin.conf"
check_for_err "Success\n";
}
copy_configs(){
okay_ver=$(vercomp "$COLLECTD_VER" 5.2)
if [ "$okay_ver" != 2 ]; then
install_config 10-aggregation-cpu.conf "CPU Aggregation Plugin"
fi
install_write_http_plugin
}
verify_configs(){
echo "Verifying config"
${COLLECTD} -t
echo "All good"
}
main() {
get_collectd_config
get_source_config
okay_ver=$(vercomp "$COLLECTD_VER" 5.4.0)
if [ "$okay_ver" != 2 ]; then
WRITE_QUEUE_CONFIG="WriteQueueLimitHigh 2000000\\nWriteQueueLimitLow 1800000";
fi
printf "Making managed config dir %s ..." "${COLLECTD_MANAGED_CONFIG_DIR}"
mkdir -p "${COLLECTD_MANAGED_CONFIG_DIR}"
check_for_err "Success\n";
if [ -e "${COLLECTD_CONFIG}" ]; then
printf "Backing up %s: " "${COLLECTD_CONFIG}";
_bkupname=${COLLECTD_CONFIG}.$(date +"%Y-%m-%d-%T");
mv "${COLLECTD_CONFIG}" "${_bkupname}"
check_for_err "Success(${_bkupname})\n";
fi
printf "Installing signalfx collectd configuration to %s: " "${COLLECTD_CONFIG}"
sed -e "s#%%%TYPESDB%%%#${TYPESDB}#" \
-e "s#%%%SOURCENAMEINFO%%%#${SOURCE_NAME_INFO}#" \
-e "s#%%%WRITEQUEUECONFIG%%%#${WRITE_QUEUE_CONFIG}#" \
-e "s#%%%COLLECTDMANAGEDCONFIG%%%#${COLLECTD_MANAGED_CONFIG_DIR}#" \
"${BASE_DIR}/collectd.conf.tmpl" > "${COLLECTD_CONFIG}"
check_for_err "Success\n"
#install managed_configs
copy_configs
verify_configs
echo "Starting collectd"
${COLLECTD}
}
BASE_DIR=$(cd "$(dirname "$0")" && pwd 2>/dev/null)
MANAGED_CONF_DIR=${BASE_DIR}/managed_config
parse_args "$@"
main