forked from ptierno/signalfx-collectd-configs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·344 lines (310 loc) · 11.1 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd 2>/dev/null)
source "${SCRIPT_DIR}/install_helpers"
get_logfile() {
LOGTO="\"/var/log/signalfx-collectd.log\""
if [ -f /etc/os-release ]; then
. /etc/os-release
if [ "$NAME" == "CentOS Linux" -a "$VERSION_ID" == "7" ]; then
LOGTO="stdout";
fi
fi
}
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}")
USE_SERVICE_COLLECTD=0
if [ "$COLLECTD_ETC" == "/etc" ]; then
USE_SERVICE_COLLECTD=1
COLLECTD_ETC="/etc/collectd.d"
printf "Making /etc/collectd.d..."
mkdir -p ${COLLECTD_ETC};
check_for_err "Success\n";
elif [ "$COLLECTD_ETC" == "/etc/collectd" ]; then
USE_SERVICE_COLLECTD=1
fi
COLLECTD_MANAGED_CONFIG_DIR=${COLLECTD_ETC}/managed_config
COLLECTD_FILTERING_CONFIG_DIR=${COLLECTD_ETC}/filtering_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 two 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
read -p "How would you like to configure your Hostname? (dns or input): " SOURCE_TYPE < /dev/tty
while [ "$SOURCE_TYPE" != "dns" -a "$SOURCE_TYPE" != "input" ]; do
read -p "Invalid answer. How would you like to configure your Hostname? (dns or input): " SOURCE_TYPE < /dev/tty
done
fi
case $SOURCE_TYPE in
"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 "Installs the SignalFx collectd plugin on supported oses. If on an unknown os"
echo "and the plugin is already present, will configure it."
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 " 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(){
SFX_INGEST_URL="https://ingest.signalfx.com"
release_type=release
while getopts ":s:t:u:o:H:hbTa:i:" 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" ;;
a)
SFX_API="--url=$OPTARG" ;;
i)
SFX_INGEST_URL="$OPTARG" ;;
h)
usage 0; ;;
b)
release_type=beta
source "${SCRIPT_DIR}/install_helpers"
;;
T)
release_type=test
source "${SCRIPT_DIR}/install_helpers"
;;
\?) 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_filters() {
printf "Installing filtering configs\n"
for i in `ls -1 ${FILTERING_CONF_DIR}`
do
cp "${FILTERING_CONF_DIR}/$i" "${COLLECTD_FILTERING_CONFIG_DIR}/"
check_for_err "Instaiilng $i - Success\n"
done
}
check_for_aws() {
printf "Checking to see if this box is in AWS: "
AWS_UNIQUE_ID=$(${SCRIPT_DIR}/get_aws_unique_id)
status=$?
if [ $status -eq 0 ]; then
printf "Using AWSUniqueId: %s\n" "${AWS_UNIQUE_ID}"
EXTRA_DIMS="?sfxdim_AWSUniqueId=${AWS_UNIQUE_ID}"
elif [ $status -ne 28 -a $status -ne 7 ]; then
check_for_err "Unknown Error $status\n"
else
printf "Not IN AWS\n"
fi
}
install_plugin_common() {
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 --print_token_only --error_on_multiple ${SFX_API} ${SFX_ORG} "${SFX_USER}")
if [ -z "$API_TOKEN" ]; then
echo "Failed to get SignalFx API token";
exit 2;
fi
fi
check_for_aws
}
install_signalfx_plugin() {
if [ -n "$NO_PLUGIN" ]; then
return
fi
install_plugin_common
printf "Fixing SignalFX plugin configuration.."
sed -e "s#%%%API_TOKEN%%%#${API_TOKEN}#g" \
-e "s#%%%INGEST_HOST%%%#${SFX_INGEST_URL}#g" \
-e "s#%%%EXTRA_DIMS%%%#${EXTRA_DIMS}#g" \
"${MANAGED_CONF_DIR}/10-signalfx.conf" > "${COLLECTD_MANAGED_CONFIG_DIR}/10-signalfx.conf"
check_for_err "Success\n";
}
install_write_http_plugin(){
install_plugin_common
printf "Fixing write_http plugin configuration.."
sed -e "s#%%%API_TOKEN%%%#${API_TOKEN}#g" \
-e "s#%%%INGEST_HOST%%%#${SFX_INGEST_URL}#g" \
-e "s#%%%EXTRA_DIMS%%%#${EXTRA_DIMS}#g" \
"${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
install_filters
}
verify_configs(){
echo "Verifying config"
${COLLECTD} -t
echo "All good"
}
check_with_user_and_stop_other_collectd_instances(){
count_running_collectd_instances=$(pgrep -x collectd | wc -l)
if [ $count_running_collectd_instances -ne 0 ]; then
PROCEED_STATUS=0
printf "Currently, $count_running_collectds more instances of collectd are running on this machine\n"
printf "Do you want to\n"
printf "1. Stop here and check\n"
printf "2. Stop all running instances of collectd and start a new one\n"
printf "3. Start this along with others\n"
while [[ ! ( $PROCEED_STATUS -eq 1 || $PROCEED_STATUS -eq 2 || $PROCEED_STATUS -eq 3 ) ]]; do
read -p "Choose an option(1/2/3): " PROCEED_STATUS < /dev/tty
done
case $PROCEED_STATUS in
1)
echo "Check and come back. Exiting for now..."
exit 0;
;;
2)
echo "Stopping all running collectd instances..."
pkill -x collectdmon > /dev/null 2>&1
pkill -x collectd > /dev/null 2>&1 # centos does not have collectdmon
;;
esac
fi
}
main() {
get_collectd_config
get_source_config
get_logfile
okay_ver=$(vercomp "$COLLECTD_VER" 5.4.0)
if [ "$okay_ver" != 2 ]; then
WRITE_QUEUE_CONFIG="WriteQueueLimitHigh 2000000\\nWriteQueueLimitLow 1800000"
fi
okay_ver=$(vercomp "$COLLECTD_VER" 5.5.0)
if [ "$okay_ver" != 2 ]; then
WRITE_QUEUE_CONFIG="$WRITE_QUEUE_CONFIG\\nCollectInternalStats true\\nFlushInterval 10\\nFlushTimeout 2"
fi
printf "Making managed config dir %s ..." "${COLLECTD_MANAGED_CONFIG_DIR}"
mkdir -p "${COLLECTD_MANAGED_CONFIG_DIR}"
check_for_err "Success\n";
printf "Making managed filtering config dir %s ..." "${COLLECTD_FILTERING_CONFIG_DIR}"
mkdir -p "${COLLECTD_FILTERING_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}#" \
-e "s#%%%COLLECTDFILTERINGCONFIG%%%#${COLLECTD_FILTERING_CONFIG_DIR}#" \
-e "s#%%%LOGTO%%%#${LOGTO}#" \
"${BASE_DIR}/collectd.conf.tmpl" > "${COLLECTD_CONFIG}"
check_for_err "Success\n"
# Install Plugin
install_plugin
install_signalfx_plugin
# Install managed_configs
copy_configs
verify_configs
# Stop running Collectd
echo "Stopping collectd"
if [ ${USE_SERVICE_COLLECTD} -eq 1 ]; then
service collectd stop
else
pkill -nx collectd # stops the newest (most recently started) collectd similar to 'service collectd stop'
fi
check_with_user_and_stop_other_collectd_instances
echo "Starting collectd"
if [ ${USE_SERVICE_COLLECTD} -eq 1 ]; then
service collectd start
else
${COLLECTD}
fi
}
BASE_DIR=$(cd "$(dirname "$0")" && pwd 2>/dev/null)
MANAGED_CONF_DIR=${BASE_DIR}/managed_config
FILTERING_CONF_DIR=${BASE_DIR}/filtering_config
parse_args "$@"
main