-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinstall.sh
executable file
·713 lines (659 loc) · 23.4 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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#!/bin/bash
# set -x
set -e
set -o pipefail
# Default values of arguments
SHOULD_UPGRADE=0
SHOW_HELP=0
INCLUDE_PATHS=""
INSTALL_MAT=0
RELEASEURL="https://api.github.com/repos/snappyflow/apm-agent/releases/latest"
SFTRACE_AGENT_x86_64="https://github.com/snappyflow/apm-agent/releases/download/latest/sftrace-agent.tar.gz"
FLUENT_CENTOS_6_BUILD="https://github.com/snappyflow/apm-agent/releases/download/centos6-td-agent-bit/fluentbit.tar.gz"
AGENTDIR="/opt/sfagent"
AGENTDIR_BKP="/opt/sfagent_bkp"
TDAGENTCONFDIR="/etc/td-agent-bit"
ID=$(grep -w "ID" /etc/os-release | cut -d"=" -f2 | tr -d '"')
SERVICEFILE="/etc/systemd/system/sfagent.service"
DEFAULTPATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ENV_VARS=""
INITFILE="/etc/init.d/sfagent"
ARCH=$(uname -m)
SYSTEM_TYPE=$(ps --no-headers -o comm 1)
AGENT_CERT="$AGENTDIR/certs/sfagent.pem"
AGENT_CERT_KEY="$AGENTDIR/certs/sfagent-key.pem"
logit()
{
echo "[$(date +%d/%m/%Y-%T)] - ${*}"
}
check_nc_installation()
{
command -v "$1" >/dev/null 2>&1
}
ensure_system_packages()
{
logit "install required system packages"
if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
apt install -qy curl wget netcat logrotate sysstat &>/dev/null
if check_nc_installation "netcat"; then
logit "netcat (nc) command is installed."
else
logit "installing netcat command"
apt install netcat &>/dev/null
if check_nc_installation "netcat" ; then
logit "Unable to install netcat command. Please install it manually"
fi
fi
fi
if [ "$ID" = "centos" ] || [ "$ID" = "amzn" ]; then
yum install -y curl wget nc logrotate sysstat &>/dev/null
if check_nc_installation "nc"; then
logit "netcat (nc) command is installed."
else
logit "installing netcat command"
yum install -y nc &>/dev/null
if check_nc_installation "nc" ; then
logit "Unable to install nc command. Please install it manually"
fi
fi
fi
}
configure_logrotate_flb()
{
logit "configure logrotate for fluent-bit"
cat > /etc/logrotate.d/td-agent-bit << EOF
/var/log/td-agent-bit.log {
daily
dateext
missingok
copytruncate
notifempty
compress
rotate 7
}
EOF
logit "configure logrotate for fluent-bit completed"
}
install_fluent_bit()
{
if [ "$SYSTEM_TYPE" = "systemd" ] && [ "$ARCH" != "aarch64" ] ; then
logit "download latest fluent-bit release $ARCH"
curl -sL https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=500 \
| grep -w "browser_download_url"|grep "download/fluentbit" \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest fluent-bit release done"
elif [ "$SYSTEM_TYPE" = "systemd" ] && [ "$ARCH" = "aarch64" ]; then
source /etc/os-release
if [ "$VERSION_ID" = "22.04" ] ; then
curl -sL https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=500 \
| grep -w "browser_download_url"|grep "download/ubuntu22-fluent-bit-arm" \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest fluent-bit for ubuntu 22 release done"
else
logit "download latest fluent-bit release $ARCH"
curl -sL https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=500 \
| grep -w "browser_download_url"|grep "download/fluent-bit-arm" \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest arm64 fluent-bit release done"
fi
else
logit "download centos 6 fluent-bit release"
wget -q $FLUENT_CENTOS_6_BUILD
logit "download centos 6 fluent-bit release done"
fi
mkdir -p /opt/td-agent-bit/bin && mkdir -p /etc/td-agent-bit/
tar -zxvf fluentbit.tar.gz >/dev/null && mv -f fluent-bit /opt/td-agent-bit/bin/td-agent-bit && mv -f GeoLite2-City.mmdb $TDAGENTCONFDIR
[ -f url-normalizer ] && mv -f url-normalizer /opt/td-agent-bit/bin/
[ -f ldap-parser ] && mv -f ldap-parser /opt/td-agent-bit/bin/
[ -f uaparserserver ] && mv -f uaparserserver /opt/td-agent-bit/bin/
[ -f message-formatter ] && mv -f message-formatter /opt/td-agent-bit/bin/
[ -f airflow-goals-parser ] && mv -f airflow-goals-parser /opt/td-agent-bit/bin/
mv -f td-agent-bit.conf /etc/td-agent-bit/
configure_logrotate_flb
logit "install fluent-bit completed"
}
install_sftrace_agent()
{
logit "install sftrace java-agent and python-agent"
wget -q $SFTRACE_AGENT_x86_64
tar -zxvf sftrace-agent.tar.gz >/dev/null && mv -f sftrace /opt/sfagent && mv -f /opt/sfagent/sftrace/sftrace /bin && mv -f /opt/sfagent/sftrace/java/sftrace /opt/sfagent/sftrace
logit "install sftrace java-agent and python-agent completed"
}
upgrade_fluent_bit()
{
#td_agent_bit_status=$(systemctl show -p ActiveState td-agent-bit | cut -d'=' -f2)
#if [ "$td_agent_bit_status" = "active" ];
#then
# systemctl stop td-agent-bit
# systemctl disable td-agent-bit
#fi
[ -d /opt/td-agent-bit/bin_bkp ] && logit "remove old backup directories /opt/td-agent-bit/bin_bkp" && rm -rf /opt/td-agent-bit/bin_bkp
cp -R /opt/td-agent-bit/bin /opt/td-agent-bit/bin_bkp
if [ "$SYSTEM_TYPE" = "systemd" ] && [ "$ARCH" != "aarch64" ]; then
logit "download latest fluent-bit release for $ARCH"
curl -sL https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=500 \
| grep -w "browser_download_url"|grep "download/fluentbit" \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest fluent-bit release done"
elif [ "$SYSTEM_TYPE" = "systemd" ] && [ "$ARCH" = "aarch64" ]; then
source /etc/os-release
if [ "$VERSION_ID" = "22.04" ] ; then
curl -sL https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=500 \
| grep -w "browser_download_url"|grep "download/ubuntu22-fluent-bit-arm" \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest fluent-bit for ubuntu 22 arm release done"
else
logit "download latest fluent-bit release for $ARCH"
curl -sL https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=500 \
| grep -w "browser_download_url"|grep "download/fluent-bit-arm" \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest arm64 fluent-bit release done"
fi
else
logit "download centos 6 build for fluent-bit"
wget -q $FLUENT_CENTOS_6_BUILD
logit "download centos 6 fluent-bit release done"
fi
tar -zxvf fluentbit.tar.gz >/dev/null && mv -f fluent-bit /opt/td-agent-bit/bin/td-agent-bit && mv -f GeoLite2-City.mmdb $TDAGENTCONFDIR
[ -f url-normalizer ] && mv -f url-normalizer /opt/td-agent-bit/bin/
[ -f ldap-parser ] && mv -f ldap-parser /opt/td-agent-bit/bin/
[ -f uaparserserver ] && mv -f uaparserserver /opt/td-agent-bit/bin/
[ -f message-formatter ] && mv -f message-formatter /opt/td-agent-bit/bin/
[ -f airflow-goals-parser ] && mv -f airflow-goals-parser /opt/td-agent-bit/bin/
mv -f td-agent-bit.conf /etc/td-agent-bit
logit "upgrade fluent-bit completed"
}
install_eclipse_mat()
{
logit "checking Eclipse MAT installed"
DIR="/opt/sfagent/Eclipse_Mat_File"
MAT_URL="https://github.com/snappyflow/apm-agent/raw/master/MemoryAnalyzer-1.10.0.20200225-linux.gtk.x86_64.zip"
if [ -d "$DIR" ]; then
# Take action if $DIR exists. #
logit "eclipse MAT is already installed in ${DIR}..."
else
logit "downloading Eclipse MAT"
mkdir -p /opt/sfagent/Eclipse_Mat_File
wget -q -O /opt/sfagent/Eclipse_Mat_MemoryAnalyzer.zip $MAT_URL && unzip /opt/sfagent/Eclipse_Mat_MemoryAnalyzer.zip -d /opt/sfagent/Eclipse_Mat_File/
logit "Eclipse MAT is successfully installed"
fi
}
upgrade_sftrace_agent()
{
wget -q $SFTRACE_AGENT_x86_64
logit "download latest sftrace agent done"
[ -f $AGENTDIR/sftrace/java/elasticapm.properties ] && mv -f /opt/sfagent/sftrace/java/elasticapm.properties .
rm -rf /opt/sfagent/sftrace
rm -rf /bin/sftrace
tar -zxvf sftrace-agent.tar.gz >/dev/null && mv -f sftrace /opt/sfagent && mv -f /opt/sfagent/sftrace/sftrace /bin && mv -f /opt/sfagent/sftrace/java/sftrace /opt/sfagent/sftrace
[ -f elasticapm.properties ] && mv -f elasticapm.properties /opt/sfagent/sftrace/java/
logit "upgrade sftrace java-agent and python-agent completed"
}
upgrade_apm_agent()
{
buildinfo=$($AGENTDIR/sfagent --version | tr '\n' ',')
logit "existing buildinfo $buildinfo"
if [ -d "$AGENTDIR" ]; then
if [ -f "$SERVICEFILE" ]; then
logit "stop sfagent service"
systemctl stop sfagent.service
fi
[ -d $AGENTDIR_BKP ] && logit "remove old backup directories $AGENTDIR_BKP" && rm -rf $AGENTDIR_BKP
logit "backup existing build"
cp -R $AGENTDIR $AGENTDIR_BKP
logit "backup config.yaml, env.conf and customer scripts"
cp -f $AGENTDIR/config.yaml _config_backup.yaml && logit "backup $AGENTDIR/config.yaml"
#Creation of normalization dir to be removed in future once older agents are upgraded
mkdir -p $AGENTDIR/normalization
[ -f $AGENTDIR/normalization/config.yaml ] && cp $AGENTDIR/normalization/config.yaml _norm_config_backup.yaml && logit "backup $AGENTDIR/normalization/config.yaml"
[ -f $AGENTDIR/mappings/custom_logging_plugins.yaml ] && cp $AGENTDIR/mappings/custom_logging_plugins.yaml _custom_logging_backup.yaml && logit "backup $AGENTDIR/mappings/custom_logging_plugins.yaml"
[ -f $AGENTDIR/scripts/custom_scripts.lua ] && cp $AGENTDIR/scripts/custom_scripts.lua _custom_script_backup.yaml && logit "backup $AGENTDIR/scripts/custom_scripts.lua"
[ -f $AGENTDIR/env.conf ] && cp $AGENTDIR/env.conf _env.conf && logit "backup $AGENTDIR/env.conf"
rm -rf checksum* sfagent* mappings normalization
logit "download latest sfagent release"
curl -sL $RELEASEURL \
| grep -w "browser_download_url" \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest sfagent release done for $ARCH"
if [ $ARCH = "aarch64" ];then
CHECKSUM=$(grep "arm64" checksums.txt | sha256sum --check | grep OK)
else
CHECKSUM=$(grep "$ARCH" checksums.txt | sha256sum --check | grep OK)
fi
if [ ${#CHECKSUM} != 0 ]; then
logit "checksum verification $CHECKSUM"
ls -l sfagent* checksum* >/dev/null
if [ $ARCH = "aarch64" ]; then
tar -zxvf sfagent*linux_arm64.tar.gz >/dev/null
else
tar -zxvf sfagent*linux_$ARCH.tar.gz >/dev/null
fi
mkdir -p $AGENTDIR/certs
mkdir -p $AGENTDIR/statsd_rules
mv -f sfagent $AGENTDIR
mv -f jolokia.jar $AGENTDIR
mv -f mappings/* $AGENTDIR/mappings/
mv -f scripts/* $AGENTDIR/scripts/
mv -f certs/* $AGENTDIR/certs/
mv -f statsd/* $AGENTDIR/statsd_rules/
mv -f normalization/* $AGENTDIR/normalization/
mv -f config.yaml.sample $AGENTDIR/config.yaml.sample
logit "restore config.yaml, env.conf and customer scripts"
cp -f _config_backup.yaml $AGENTDIR/config.yaml && logit "restore $AGENTDIR/config.yaml"
[ -f _norm_config_backup.yaml ] && cp _norm_config_backup.yaml $AGENTDIR/normalization/config.yaml && logit "restore $AGENTDIR/normalization/config.yaml"
[ -f _custom_logging_backup.yaml ] && cp _custom_logging_backup.yaml $AGENTDIR/mappings/custom_logging_plugins.yaml && logit "restore $AGENTDIR/mappings/custom_logging_plugins.yaml"
[ -f _custom_script_backup.yaml ] && cp _custom_script_backup.yaml $AGENTDIR/scripts/custom_scripts.lua && logit "restore $AGENTDIR/scripts/custom_scripts.lua"
[ -f _env.conf ] && cp _env.conf $AGENTDIR/env.conf && logit "restore $AGENTDIR/env.conf"
chown -R root:root /opt/sfagent
# create service files
if [ "$SYSTEM_TYPE" = "systemd" ]; then
create_sfagent_service
else
create_sfagent_init_script
fi
# restart sfagent
logit "restart sfagent service"
if [ "$SYSTEM_TYPE" = "systemd" ]; then
systemctl restart sfagent.service
else
service sfagent restart
fi
# get agent status
if [ "$SYSTEM_TYPE" = "systemd" ]; then
status=$(systemctl status sfagent.service)
else
status=$(service sfagent status)
fi
# revert to old build if agent is in failed state after upgrade
if [[ $status == *"failed"* ]] ;then
logit "upgarde sfagent failed"
rm -rf $AGENTDIR
mv $AGENTDIR_BKP $AGENTDIR
rm -rf /opt/td-agent-bit/bin
mv /opt/td-agent-bit/bin_bkp /opt/td-agent-bit/bin
# restart sfagent
if [ "$SYSTEM_TYPE" = "systemd" ]; then
systemctl restart sfagent.service
else
service sfagent restart
fi
logit "upgrade sfagent failed reverted to old release"
check_and_send_status "failed"
else
rm -rf $AGENTDIR_BKP
rm -rf /opt/td-agent-bit/bin_bkp
logit "upgrade sfagent completed"
check_and_send_status "success"
fi
else
logit "checksum verification failed $CHECKSUM"
fi
else
logit "directory $AGENTDIR not found, installing agent"
install_apm_agent
fi
}
check_and_send_status()
{
if [ -e /tmp/upgrade_status.json ]
then
logit "automated upgrade sending upgarde status"
status=$1
logit "sfagent running response code $status"
if [ "$status" = "success" ]
then
buildinfo=$($AGENTDIR/sfagent --version | tr '\n' ',')
logit "upgraded buildinfo $buildinfo"
sed -i "s/#STATUS/$1/g" /tmp/upgrade_status.json
sed -i "s/#MESSAGE/$buildinfo/g" /tmp/upgrade_status.json
sed -i "s/111122223333/$(($(date +%s%N)/1000000))/g" /tmp/upgrade_status.json
sed -i "s/111122224444/$(($(date +%s%N)/1000000))/g" /tmp/upgrade_status.json
# send data to forwarder
while ! nc -z 127.0.0.1 8588; do
logit "wait for forwarder to accept connection"
sleep 5
done
response=$(curl -s --connect-timeout 10 -m 30 -XPOST -H "Accept: application/json" http://127.0.0.1:8588/ -d @/tmp/upgrade_status.json)
logit "upgrade command status sent $response"
else
logit "sfagent not running"
fi
fi
}
install_apm_agent()
{
rm -rf checksum* sfagent* mappings normalization $AGENTDIR
logit "download latest sfagent release"
curl -sL $RELEASEURL \
| grep -w "browser_download_url" \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
logit "download latest sfagent release done"
ls -l sfagent* checksum* >/dev/null
if [ $ARCH = "aarch64" ];then
CHECKSUM=$(grep "arm64" checksums.txt | sha256sum --check | grep OK)
else
CHECKSUM=$(grep $ARCH checksums.txt | sha256sum --check | grep OK)
fi
logit "checksum verification $CHECKSUM"
if [ $ARCH = "aarch64" ];then
tar -zxvf sfagent*linux_arm64.tar.gz >/dev/null
else
tar -zxvf sfagent*linux_$ARCH.tar.gz >/dev/null
fi
mkdir -p $AGENTDIR
mkdir -p $AGENTDIR/mappings
mkdir -p $AGENTDIR/scripts
mkdir -p $AGENTDIR/certs
mkdir -p $AGENTDIR/normalization
mkdir -p $AGENTDIR/statsd_rules
mv sfagent $AGENTDIR
mv jolokia.jar $AGENTDIR
mv mappings $AGENTDIR/.
mv scripts $AGENTDIR/.
mv certs $AGENTDIR/.
mv normalization $AGENTDIR/.
mv statsd/* $AGENTDIR/statsd_rules
mv config.yaml.sample $AGENTDIR/config.yaml.sample
cat > $AGENTDIR/config.yaml <<EOF
agent:
metrics:
logging:
tags:
key:
EOF
# create env file
create_env_file
chown -R root:root /opt/sfagent
if [ "$SYSTEM_TYPE" = "systemd" ]; then
create_sfagent_service
else
create_sfagent_init_script
fi
# restart sfagent
if [ "$SYSTEM_TYPE" = "systemd" ]; then
systemctl restart sfagent.service
else
service sfagent restart
fi
logit "install sfagent completed"
}
check_jcmd_installation()
{
logit "checking jcmd installation"
if ! [ -x "$(command -v jcmd)" ]; then
logit "Warning: jcmd is not installed. Java applications will not be detected automatically"
else
logit "jcmd is installed"
fi
}
create_env_file()
{
logit "create $AGENTDIR/env.conf file"
touch $AGENTDIR/env.conf
if [ -n "$INCLUDE_PATHS" ];
then
logit "extra paths to include in PATH - $INCLUDE_PATHS"
IFS=","
for v in $INCLUDE_PATHS
do
DEFAULTPATH="$DEFAULTPATH:$v"
done
fi
logit "environment PATH=$DEFAULTPATH"
echo "PATH=$DEFAULTPATH" > $AGENTDIR/env.conf
if [ -n "$ENV_VARS" ]
then
logit "append env vars to $AGENTDIR/env.conf"
IFS=","
for v in $ENV_VARS
do
echo $v >> $AGENTDIR/env.conf
done
fi
}
create_sfagent_service()
{
logit "create sfagent service"
cat > "$SERVICEFILE" <<EOF
[Unit]
Description=snappyflow apm agent service
ConditionPathExists=$AGENTDIR/sfagent
After=network.target
[Service]
Type=simple
Restart=on-failure
RestartSec=10
WorkingDirectory=$AGENTDIR
EnvironmentFile=-$AGENTDIR/env.conf
ExecStartPre=/bin/mkdir -p /var/log/sfagent
ExecStartPre=/bin/chmod 755 /var/log/sfagent
ExecStartPre=/bin/bash -c -e "/opt/sfagent/sfagent -config-file /opt/sfagent/config.yaml -check-config"
ExecStart=/bin/bash -c -e "/opt/sfagent/sfagent -config-file /opt/sfagent/config.yaml"
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sfagent
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
logit "enable sfagent service"
systemctl enable sfagent
}
create_sfagent_init_script()
{
logit "create sfagent init.d file"
cat > "$INITFILE" <<'EOF'
#!/bin/bash
# sfagent daemon
# chkconfig: - 20 80
# description: EC2 instance SnappyFlow agent
# processname: sfagent
DAEMON_PATH="/opt/sfagent"
NAME=sfagent
DESC="My daemon description"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/sfagent
LOG_PATH=/var/log/sfagent/sfagent.log
DAEMON=/opt/sfagent/sfagent
DAEMONOPTS="-config /opt/sfagent/config.yaml"
if [[ $# -eq 0 ]]; then
echo "Usage: $0 {start|stop|restart}"
exit 1
else
COMMAND="$1"
fi
case $COMMAND in
start)
printf "%-50s" "Starting $NAME..."
if [[ -f $PIDFILE ]]; then
PID=`cat $PIDFILE`
if [[ "`ps axf | grep ${PID} | grep -v grep`" ]]; then
echo "sfagent is already running"
exit 0
fi
fi
cd $DAEMON_PATH
CMD="$DAEMON $DAEMONOPTS > /dev/null 2>&1"
echo $CMD
$CMD &
if [ $? -eq 0 ]; then
printf "%s\n" "Ok"
echo $! > $PIDFILE
else
printf "%s\n" "Fail. Check logs $LOG_PATH"
exit 1
fi
CMD1="update-rc.d sfagent defaults"
echo $CMD1
$CMD1 &
if [ $? -eq 0 ]; then
printf "\n" "symbolic link added for sfagent"
else
printf "%s\n" "Failed while adding symbolic link for sfagent. Check logs $LOG_PATH"
exit 1
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [[ -f $PIDFILE ]]; then
PID=`cat $PIDFILE`
if [[ -z "`ps axf | grep ${PID} | grep -v grep`" ]]; then
printf "%s\n" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%s\n" "Service not running failed"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
if [[ -f $PIDFILE ]]; then
PID=`cat $PIDFILE`
kill -HUP $PID
killall -w td-agent-bit
printf "%s\n" "Ok"
rm -f $PIDFILE
exit 0
else
printf "%s\n" "already stopped"
exit 0
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
EOF
chmod +x "$INITFILE"
logit "sfagent init script created"
}
print_usage()
{
echo ""
echo "usage of install.sh"
echo " ./install.sh [-h|--help][-u|--upgrade][--paths \"path1,path2\"][--env \"ENV_VAR1=value1,ENV_VAR2=value2\"]"
echo ""
echo " -h|--help show usage information"
echo " -u|--upgrade upgrade installed sfagent"
echo " --paths comma seperated list of paths to include in PATH of sfagent service"
echo " ex: \"/opt/jdk1.8.0_211/bin,/opt/jdk1.8.0_211/jre/bin\""
echo " --env comma seperated list of Environemt variables"
echo " ex: \"HTTP_PROXY=http://proxy.example.com,HTTPS_PROXY=https://proxy.example.com\""
echo ""
echo "examples:"
echo " ./install.sh"
echo " ./install.sh --install-mat"
echo " ./install.sh --paths \"/opt/jdk1.8.0_211/bin,/opt/jdk1.8.0_211/jre/bin\""
echo " ./install.sh --upgrade"
echo " ./install.sh --upgrade --install-mat"
echo " ./install.sh --upgrade --paths \"/opt/jdk1.8.0_211/bin,/opt/jdk1.8.0_211/jre/bin\""
echo " ./install.sh --env \"HTTP_PROXY=http://proxy.example.com,HTTPS_PROXY=https://proxy.example.com\""
echo ""
}
UNKNOWN=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--paths)
INCLUDE_PATHS="$2"
shift
shift
;;
--env)
ENV_VARS="$2"
shift
shift
;;
-h|--help)
SHOW_HELP=1
shift
;;
-u|--upgrade)
SHOULD_UPGRADE=1
shift
;;
--install-mat)
INSTALL_MAT=1
shift
;;
*)
UNKNOWN+=("$1")
shift
;;
esac
done
if [ -n "$UNKNOWN" ]
then
logit "ERROR: unknown arguments: $UNKNOWN"
print_usage
exit 128
fi
if [ "$SHOW_HELP" -eq 1 ];
then
print_usage
exit 0
fi
oldpath=$(pwd)
tmp_dir=$(mktemp -d -t installsfagent-XXXXXXXXXX)
cd "$tmp_dir"
if [ "$EUID" -ne 0 ]; then
logit "Need root previlege to proceed with installation."
exit 0
fi
if [ "$INSTALL_MAT" -eq 1 ];
then
install_eclipse_mat
fi
if [ "$SHOULD_UPGRADE" -eq 1 ];
then
ensure_system_packages
logit "upgrading fluent-bit"
upgrade_fluent_bit
logit "upgrading sfagent"
upgrade_apm_agent
logit "upgrading sftrace agent"
upgrade_sftrace_agent
else
ensure_system_packages
logit "check jcmd installed"
check_jcmd_installation
logit "installing fluent-bit"
install_fluent_bit
logit "installing sfagent"
install_apm_agent
logit "installing sftrace agent"
install_sftrace_agent
fi
cd "$oldpath"
rm -rf "$tmp_dir"
sleep 1
logit "Done"
exit 0