-
Notifications
You must be signed in to change notification settings - Fork 1
/
trafikito_install.sh
592 lines (546 loc) · 18.9 KB
/
trafikito_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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# /*
# * Copyright (C) Trafikito.com
# * All rights reserved.
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions
# * are met:
# * 1. Redistributions of source code must retain the above copyright
# * notice, this list of conditions and the following disclaimer.
# * 2. Redistributions in binary form must reproduce the above copyright
# * notice, this list of conditions and the following disclaimer in the
# * documentation and/or other materials provided with the distribution.
# *
# * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# * SUCH DAMAGE.
# */
echo ""
echo ""
echo " _____ __ _ _ _ _"
echo " |_ _| __ __ _ / _(_) | _(_) |_ ___"
echo " | || '__/ _\` | |_| | |/ / | __/ _ \\"
echo " | || | | (_| | _| | <| | || (_) |"
echo " |_||_| \__,_|_| |_|_|\_\_|\__\___/"
echo ""
echo ""
echo " Trafikito agent installation"
echo ""
echo ""
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
# use printf (a shell builtin) here because echo is *very* distro dependant
fn_prompt() {
default=$1
mesg=$2
# if not running with a tty returns $default
if [ ! "`tty`" ]; then
return 1
fi
while true; do
printf "$mesg "; read x
if [ -z "$x" ]; then
answer="$default"
else
case "$x" in
y*|Y*) answer=Y ;;
n*|N*) answer=N ;;
*) echo "Please reply y or n"
continue
esac
fi
if [ "$answer" = "$default" ]; then
return 1
else
return 0
fi
done
}
usage() {
(
echo
echo "Usage: sh $0 --user_api_key=<api_key> --workspace_id=<workspace_id> [--servername=<servername>]"
echo
echo "To install Trafikito agent, please follow these steps:"
echo " 1. Visit https://trafikito.com/servers"
echo " 2. Find your server on servers list or add new one"
echo " 3. Click 3 dots button to open menu and select: How to install?"
echo " 4. Follow instructions on the dashboard"
) 1>&2
exit 1
}
# parse arguments
for x in $*; do
option=`echo "$x" | sed -e 's#=.*##'`
arg=`echo "$x" | sed -e 's#.*=##'`
case "$option" in
--user_api_key) USER_API_KEY="$arg" ;;
--workspace_id) WORKSPACE_ID="$arg" ;;
--servername) SERVER_NAME="$arg" ;;
*) echo "Bad option '$option'" 1>&2
usage
esac
done
test -z "$USER_API_KEY" && echo "Option '--user_api_key' with an argument is required" 1>&2 && usage
test -z "$WORKSPACE_ID" && echo "Option '--workspace_id' with an argument is required" 1>&2 && usage
if [ -z "$SERVER_NAME" ]; then
SERVER_NAME=`hostname -f`
"echo" -n "Name this Trafikito instance [${SERVER_NAME}]: "; read x
if [ "$x" ]; then
SERVER_NAME="$x"
fi
fi
# running as root or user ?
RUNAS=`whoami`
WHOAMI=`whoami`
if [ "$WHOAMI" != "root" ]; then
echo "If possible, run installation as root user."
echo "To install as root either log in as root and execute the script or use:"
echo
echo " sudo sh $0 --user_api_key=$USER_API_KEY --workspace_id=$WORKSPACE_ID --servername=$SERVER_NAME"
echo
fn_prompt "N" "Continue as $WHOAMI [yN]: " || exit 1
RUNAS="$WHOAMI"
fi
# get BASEDIR
export BASEDIR="/opt/trafikito"
while true; do
fn_prompt "Y" "Going to install Trafikito in $BASEDIR [Yn]: "
if [ $? -eq 0 ]; then
printf " Enter directory for installation: "; read BASEDIR
# test for starting /
echo $BASEDIR | grep -q '^\/'
if [ $? -ne 0 ]; then
echo " Directory for installation must be an absolute path!"
BASEDIR="/opt/trafikito"
continue
fi
# test for spaces in path
echo $BASEDIR | grep -vq ' '
if [ $? -ne 0 ]; then
echo " Directory name for installation must not contain spaces!"
BASEDIR="/opt/trafikito"
continue
fi
fi
if [ -d $BASEDIR ]; then
fn_prompt "Y" " Found existing $BASEDIR: okay to remove it? [Yn]: "
if [ $? -eq 1 ]; then
if [ -f $BASEDIR/lib/remove_startup.sh ]; then
. $BASEDIR/lib/remove_startup.sh
fi
reason=`rm -rf $BASEDIR 2>&1`
if [ $? -ne 0 ]; then
echo " Remove failed: $reason - please try again"
continue
fi
else
continue
fi
fi
break
done
mkdir -p $BASEDIR 2>/dev/null
if [ $? -ne 0 ]; then
fn_prompt "Y" "Found existing $BASEDIR: okay to remove it? [Yn]: "
rm -rf $BASEDIR
mkdir -p $BASEDIR || exit 1
fi
# create std subdirs
mkdir -p $BASEDIR/etc
mkdir -p $BASEDIR/lib
mkdir -p $BASEDIR/var
# build config and source it
CONFIG=$BASEDIR/etc/trafikito.cfg
(
echo export RUNAS=\"$RUNAS\"
echo export USER_API_KEY=\"$USER_API_KEY\"
echo export WORKSPACE_ID=\"$WORKSPACE_ID\"
echo export SERVER_NAME=\"$SERVER_NAME\"
echo export TMP_FILE=\"$BASEDIR/var/trafikito.tmp\"
) >$CONFIG
. $CONFIG
# function to install tools
fn_install_tools() {
TOOLS=$*
if [ `which apt-get` ]; then # Debian
apt-get install $TOOLS
elif [ `which yum` ]; then # RedHat
yum -y install $TOOLS
elif [ `which apk` ]; then # alpine
apk --no-cache add $TOOLS
elif [ `which pacman` ]; then # arch
pacman -S add $TOOLS
elif [ `which zypper` ]; then # SuSE
zypper install $TOOLS
else
echo " ERROR: your system's package manager is not supported"
echo " Supported package managers: apt-get, yum, apk, pacman, zypper"
return 1
fi
}
echo "* Looking for required commands..."
TOOLS=''
set "curl" "transfer an url (essential)"\
"df" "report file system disk space usage"\
"free" "report amount of free and used memory in the system"\
"egrep" "print lines matching a pattern"\
"pgrep" "look up or signal processes based on name and other attributes"\
"sed" "stream editor for filtering and transforming text"\
"su" "change user ID or become superuser"\
"top" "display processes"\
"uptime" "tell how long the system has been running"\
"vmstat" "report virtual memory statistics"
while [ $# -ne 0 ]; do
tool=$1
help=$2
shift 2
printf " $tool: $help..."
x=`which $tool` 2>/dev/null
if [ -z $x ]; then
TOOLS="$TOOLS$tool "
echo NOT FOUND
else
echo found $x
fi
done
if [ -z $TOOLS ]; then
echo
echo " Found all required commands"
else
if [ $WHOAMI = 'root' ]; then
fn_prompt "Y" "Shall I install missing tools? [Yn]: "
if [ $? -eq 1 ]; then
fn_install_tools $TOOLS
else
echo
echo " Please install the following tools as root: $TOOLS"
fi
else
echo
echo " Please install the following tools as root: $TOOLS"
fi
fi
echo
echo "* Installing agent..."
# select which edge to use and which use as a fallback
API_EDGE="https://api.trafikito.com";
fn_select_edge ()
{
API_EDGE_1="https://ap-southeast-1.api.trafikito.com"
API_EDGE_2="https://eu-west-1.api.trafikito.com"
API_EDGE_3="https://us-east-1.api.trafikito.com"
# big range to avoid unequal random value distribution on some systems
DEFAULT_EDGE=`awk 'BEGIN{srand();print int(rand()*(12000-1))+1 }'`
if [ "$DEFAULT_EDGE" -gt 4000 ]; then
API_EDGE_1="https://eu-west-1.api.trafikito.com"
API_EDGE_2="https://us-east-1.api.trafikito.com"
API_EDGE_3="https://ap-southeast-1.api.trafikito.com"
fi
if [ "$DEFAULT_EDGE" -gt 8000 ]; then
API_EDGE_1="https://us-east-1.api.trafikito.com"
API_EDGE_2="https://ap-southeast-1.api.trafikito.com"
API_EDGE_3="https://eu-west-1.api.trafikito.com"
fi
testData=`curl -X POST --silent --retry 3 --retry-delay 1 --max-time 30 -H 'Cache-Control: no-cache' -H 'Content-Type: text/plain' "$API_EDGE_1/v2/ping"`
if [ "$testData" = "OK" ]; then
API_EDGE="$API_EDGE_1"
else
testData=`curl -X POST --silent --retry 3 --retry-delay 1 --max-time 30 -H 'Cache-Control: no-cache' -H 'Content-Type: text/plain' "$API_EDGE_2/v2/ping"`
if [ "$testData" = "OK" ]; then
API_EDGE="$API_EDGE_2"
else
testData=`curl -X POST --silent --retry 3 --retry-delay 1 --max-time 30 -H 'Cache-Control: no-cache' -H 'Content-Type: text/plain' "$API_EDGE_3/v2/ping"`
if [ "$testData" = "OK" ]; then
API_EDGE="$API_EDGE_3"
else
echo "Network issues? Try again."
fi
fi
fi
}
fn_select_edge
echo "Edge selected: $API_EDGE"
fn_download ()
{
count=$1
file=$2
url="$API_EDGE/v2/agent/get_agent_file?file=$file"
curl -X POST --silent --retry 3 --retry-delay 1 --max-time 30 --output "$BASEDIR/$file" -H 'Cache-Control: no-cache' -H 'Content-Type: text/plain' "$url" > /dev/null
if [ ! -f "$BASEDIR/$file" ]; then
echo "*** $count/5 Failed to download. Retrying."
curl -X POST --silent --retry 3 --retry-delay 1 --max-time 60 --output "$BASEDIR/$file" -H 'Cache-Control: no-cache' -H 'Content-Type: text/plain' "$url" > /dev/null
if [ ! -f "$BASEDIR/$file" ]; then
echo "*** $count/5 Failed to download. Retrying."
curl -X POST --silent --retry 3 --retry-delay 1 --max-time 60 --output "$BASEDIR/$file" -H 'Cache-Control: no-cache' -H 'Content-Type: text/plain' "$url" > /dev/null
if [ ! -f "$BASEDIR/$file" ]; then
echo "*** $count/5 Failed to download: $file"
exit 1;
fi
fi
else
echo "*** $count/5 done"
fi
}
echo "*** Starting to download agent files"
# during download - set which edge to use for future requests during installation
fn_download 1 trafikito
fn_download 2 uninstall.sh
fn_download 3 lib/trafikito_wrapper.sh
fn_download 4 lib/trafikito_agent.sh
fn_download 5 lib/set_os.sh
echo
chmod +x $BASEDIR/trafikito $BASEDIR/uninstall.sh $BASEDIR/lib/*
# get os facts
. $BASEDIR/lib/set_os.sh
fn_set_os
echo "* Create server and get config file"
curl -X POST --silent --retry 3 --retry-delay 1 --max-time 30 "$API_EDGE/v2/agent/get_agent_file?file=trafikito.conf" \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d "{ \
\"workspaceId\" : \"$WORKSPACE_ID\", \
\"userApiKey\": \"$USER_API_KEY\", \
\"serverName\": \"$SERVER_NAME\", \
\"tmpFilePath\": \"$TMP_FILE\" \
}" >$TMP_FILE
grep -q server_id $TMP_FILE
if [ $? -ne 0 ]; then
cat $TMP_FILE
echo
echo "Can not get configuration file. Network issue? Please try again."
exit 1
fi
export SERVER_ID=`grep server_id $TMP_FILE | sed -e 's/.*= //'`
export API_KEY=`grep api_key $TMP_FILE | sed -e 's/.*= //'`
echo export SERVER_ID=$SERVER_ID >>$CONFIG
echo export API_KEY=$API_KEY >>$CONFIG
echo "* Generating initial settings"
>$TMP_FILE
(
cat <<STOP
trafikito_free="free"
trafikito_cpu_info_full="cat /proc/cpuinfo | sed '/^\s*$/q'"
trafikito_cpu_info="cat /proc/cpuinfo | sed '/^\s*$/q' | grep -i 'cache\|core\|model\|mhz\|sibling\|vendor\|family'"
trafikito_uptime="uptime"
trafikito_cpu_processors_count="cat /proc/cpuinfo 2>&1 | grep processor | wc -l"
trafikito_vmstat="vmstat"
trafikito_df_p="df -P"
trafikito_hostname="hostname"
trafikito_hostname_full="hostname -f"
trafikito_df_h="df -h"
trafikito_lsof_count_network_connections="lsof -i | grep -- '->' | wc -l"
trafikito_lsof_count_open_files="lsof | wc -l"
trafikito_netstat_i="netstat -i"
trafikito_vmstat_s="vmstat -s"
trafikito_top="top -bcn1 -o %MEM | sed -e '1,/^\s*$/ d' | head -n 7"
trafikito_ps="ps aux --sort=-%mem,-%cpu --width 200 | head -n 7"
STOP
) | while read line; do
command=`echo "$line" | sed -e 's#^[^=]*=##' -e 's#^"##' -e 's#"$##'` > /dev/null 2>&1
echo " executing $command..."
echo "*-*-*-*------------ Trafikito command: $command" >>$TMP_FILE
eval "$command" >>$TMP_FILE 2>&1
done
echo "* Getting available commands file & setting default dashboard"
curl --request POST --silent --retry 3 --retry-delay 1 --max-time 30 \
--url "$API_EDGE/v2/agent/get_agent_file?file=available_commands.sh" \
--header "content-type: multipart/form-data" \
--form "output=@$TMP_FILE" \
--form "userApiKey=$USER_API_KEY" \
--form "workspaceId=$WORKSPACE_ID" \
--form "serverId=$SERVER_ID" \
--form "os=$os" \
--form "osCodename=$os_codename" \
--form "osRelease=$os_release" \
--form "centosFlavor=$centos_flavor" \
--output "$BASEDIR/available_commands.sh"
echo
echo " done"
echo
# now everything will be owned by $RUNAS
chown -R "$RUNAS" $BASEDIR
# configure restart
if [ "$WHOAMI" != "root" ]; then
echo "Script was not installed as root: cannot configure startup"
echo "You can control the script manually with:"
echo
echo
echo
echo "Manual control with:"
echo " $BASEDIR/trafikito {start|stop|restart|status}"
echo
echo "Uninstall with:"
echo " sh $BASEDIR/uninstall"
echo
exit 0
fi
# kill any running instances
kill $(ps aux | awk '/trafikito_wrapper.sh/ {print $2}') >/dev/null 2>&1
#####################################
# systemd: test for useable systemctl
#####################################
x=`which systemctl 2>/dev/null`
if [ $? -eq 0 ]; then
echo "You are running systemd..."
echo "Configuring, enabling and starting the agent service..."
(
echo "[Unit]"
echo "Description=Trafikito Agent"
echo "After=network.target"
echo "[Service]"
echo "Type=simple"
echo "ExecStart=$BASEDIR/lib/trafikito_wrapper.sh $SERVER_ID $BASEDIR"
echo "[Install]"
echo "WantedBy=multi-user.target"
) >/etc/systemd/system/trafikito.service
(
echo "echo Disabling and removing systemd"
echo "systemctl stop trafikito"
echo "systemctl disable trafikito"
echo "rm -f /etc/systemd/system/trafikito.service"
) >$BASEDIR/lib/remove_startup.sh
chown $RUNAS $BASEDIR/lib/remove_startup.sh
systemctl enable trafikito
systemctl start trafikito
systemctl status trafikito --no-pager
# remove script to manually control trafikito
# rm $BASEDIR/trafikito
echo
echo "Done. You will see data at dashboard after a minute."
echo
echo
echo
echo "Manual control with:"
echo " $BASEDIR/trafikito {start|stop|restart|status}"
echo
echo "Uninstall with:"
echo " sh $BASEDIR/uninstall"
echo
exit 0
fi
#################################################################
# System V startup
#################################################################
control=`which update-rc.d 2>/dev/null` # debian/ubuntu
if [ $? -ne 0 ]; then
control=`which chkconfig 2>/dev/null` # mostly everything else
fi
if [ ! -z "$control" ]; then
echo "System V using $control is available on this server..."
echo "Configuring, enabling and starting the agent service..."
(
echo "#!/bin/sh"
echo "#"
echo "# chkconfig: 345 56 50"
echo "#"
echo "### BEGIN INIT INFO"
echo "# Provides: trafikito"
echo "# Required-Start:"
echo "# Required-Stop:"
echo "# Should-Start:"
echo "# Should-Stop:"
echo "# Default-Start:"
echo "# Default-Stop:"
echo "# Short-Description: Starts or stops the trafikito agent"
echo "# Description: Starts and stops the trafikito agent"
echo "### END INIT INFO"
echo
# remove hash bang and redefine BASEDIR
grep -v '#!' $BASEDIR/trafikito | sed -e "s#export BASEDIR.*#export BASEDIR=$BASEDIR#"
) >/etc/init.d/trafikito
chmod +x /etc/init.d/trafikito
case $control in
*update-rc.d)
(
echo "echo Removing System V startup"
echo "service trafikito stop"
echo "update-rc.d -f trafikito remove"
echo "rm -f /etc/init.d/trafikito"
) >$BASEDIR/lib/remove_startup.sh
chown $RUNAS $BASEDIR/lib/remove_startup.sh
update-rc.d trafikito defaults 99
update-rc.d trafikito enable
service trafikito start
;;
*chkconfig)
(
echo "echo Removing System V startup"
echo "service trafikito stop"
echo "chkconfig --del trafikito"
echo "rm -f /etc/init.d/trafikito"
) > $BASEDIR/lib/remove_startup.sh
chown $RUNAS $BASEDIR/lib/remove_startup.sh
chkconfig --add trafikito
chkconfig trafikito on
service trafikito start
;;
esac
# remove script to manually control trafikito
# rm $BASEDIR/trafikito
echo
echo "Done. You will see data at dashboard after a minute."
echo
echo
echo
echo "Manual control with:"
echo " $BASEDIR/trafikito {start|stop|restart|status}"
echo
echo "Uninstall with:"
echo " sh $BASEDIR/uninstall"
echo
exit 0
fi
#################################################################
# openRC: Arch + Gentoo
#################################################################
control=`which rc-update`
if [ ! -z "$control" ]; then
echo "openRC is available on this server..."
echo "Configuring, enabling and starting the agent service..."
(
# remove hash bang and redefine BASEDIR
cat $BASEDIR/trafikito | sed -e "s#export BASEDIR.*#export BASEDIR=$BASEDIR#"
) >/etc/init.d/trafikito
chmod +x /etc/init.d/trafikito
(
echo "echo Removing openRC startup"
echo "rc-service trafikito stop"
echo "rc-update del trafikito"
echo "rm -f /etc/init.d/trafikito"
) >$BASEDIR/lib/remove_startup.sh
chown $RUNAS $BASEDIR/lib/remove_startup.sh
rc-update add trafikito
rc-service trafikito start
# remove script to manually control trafikito
# rm $BASEDIR/trafikito
echo
echo "Done. You will see data at dashboard after a minute."
echo
echo
echo
echo "Manual control with:"
echo " $BASEDIR/trafikito {start|stop|restart|status}"
echo
echo "Uninstall with:"
echo " sh $BASEDIR/uninstall"
echo
exit 0
fi
echo "Could not determine the startup method on this server"
echo
echo
echo
echo "Manual control with:"
echo " $BASEDIR/trafikito {start|stop|restart|status}"
echo
echo "Uninstall with:"
echo " sh $BASEDIR/uninstall"
echo