forked from snappyflow/apm-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·520 lines (467 loc) · 14.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·520 lines (467 loc) · 14.2 KB
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
#!/bin/bash
#set -x
set -e
# 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"
TDAGENTCONFDIR="/etc/td-agent-bit"
ID=`cat /etc/os-release | grep -w "ID" | 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"
SYSTEM_TYPE=`ps --no-headers -o comm 1`
configure_logrotate_flb()
{
echo "Configure logrotate fluent-bit started"
if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
apt install -qy logrotate &>/dev/null
fi
if [ "$ID" = "centos" ]; then
yum install -y logrotate &>/dev/null
fi
cat > /etc/logrotate.d/td-agent-bit << EOF
/var/log/td-agent-bit.log {
daily
dateext
missingok
copytruncate
notifempty
compress
rotate 7
}
EOF
echo "Configure logrotate fluent-bit completed"
}
install_fluent_bit()
{
echo " "
echo "Install fluent-bit started "
if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
apt-get install -y -q wget curl
fi
if [ "$ID" = "centos" ]; then
yum install -y wget curl
fi
if [ "$SYSTEM_TYPE" = "systemd" ]; then
curl https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=100 \
| grep -w "browser_download_url"|grep fluentbit \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
else
wget $FLUENT_CENTOS_6_BUILD
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 ] && yes | mv -f url-normalizer /opt/td-agent-bit/bin/
[ -f uaparserserver ] && yes | mv -f uaparserserver /opt/td-agent-bit/bin/
mv -f td-agent-bit.conf /etc/td-agent-bit/
configure_logrotate_flb
echo "Install fluent-bit completed"
echo " "
}
install_sftrace_agent()
{
echo " "
echo "Install sftrace java-agent and python-agent started "
wget $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
echo "Install sftrace java-agent and python-agent completed"
echo " "
}
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
if [ "$SYSTEM_TYPE" = "systemd" ]; then
curl https://api.github.com/repos/snappyflow/apm-agent/releases?per_page=100 \
| grep -w "browser_download_url"|grep fluentbit \
| head -n 1 \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
else
wget $FLUENT_CENTOS_6_BUILD
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 ] && yes | mv -f url-normalizer /opt/td-agent-bit/bin/
[ -f uaparserserver ] && yes | mv -f uaparserserver /opt/td-agent-bit/bin/
mv -f td-agent-bit.conf /etc/td-agent-bit
echo "Upgrade fluent-bit binary completed "
}
install_eclipse_mat()
{ echo "Checking Eclipse MAT is already installed"
DIR="/opt/sfagent/Eclipse_Mat_File"
if [ -d "$DIR" ]; then
# Take action if $DIR exists. #
echo "Eclipse MAT is already installed in ${DIR}..."
else
echo "Downloading Eclipse MAT"
mkdir -p /opt/sfagent/Eclipse_Mat_File
wget -O /opt/sfagent/Eclipse_Mat_MemoryAnalyzer.zip https://github.com/snappyflow/apm-agent/raw/master/MemoryAnalyzer-1.10.0.20200225-linux.gtk.x86_64.zip && \
unzip /opt/sfagent/Eclipse_Mat_MemoryAnalyzer.zip -d /opt/sfagent/Eclipse_Mat_File/
echo "Eclipse MAT is successfully installed"
fi
}
upgrade_sftrace_agent()
{
wget $SFTRACE_AGENT_x86_64
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
mv -f elasticapm.properties /opt/sfagent/sftrace/java/
echo "Upgrade sftrace java-agent and python-agent completed"
}
upgrade_apm_agent()
{
if [ -d "$AGENTDIR" ]; then
if [ -f "$SERVICEFILE" ]; then
echo "Stop sfagent"
service sfagent stop
fi
ARCH=`uname -m`
echo "Backingup config.yaml and customer scripts"
cp -f $AGENTDIR/config.yaml _config_backup.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
[ -f $AGENTDIR/mappings/custom_logging_plugins.yaml ] && cp $AGENTDIR/mappings/custom_logging_plugins.yaml _custom_logging_backup.yaml
[ -f $AGENTDIR/scripts/custom_scripts.lua ] && cp $AGENTDIR/scripts/custom_scripts.lua _custom_script_backup.yaml
rm -rf checksum* sfagent* mappings normalization
curl -sL $RELEASEURL \
| grep -w "browser_download_url" \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
ls -l sfagent* checksum* >/dev/null
tar -zxvf sfagent*linux_$ARCH.tar.gz >/dev/null
mkdir -p $AGENTDIR/certs
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 normalization/* $AGENTDIR/normalization/
mv -f config.yaml.sample $AGENTDIR/config.yaml.sample
echo "Copying back config.yaml and customer scripts"
cp -f _config_backup.yaml $AGENTDIR/config.yaml
[ -f _norm_config_backup.yaml ] && yes | cp _norm_config_backup.yaml $AGENTDIR/normalization/config.yaml
[ -f _custom_logging_backup.yaml ] && yes | cp _custom_logging_backup.yaml $AGENTDIR/mappings/custom_logging_plugins.yaml
[ -f _custom_script_backup.yaml ] && yes | cp _custom_script_backup.yaml $AGENTDIR/scripts/custom_scripts.lua
chown -R root:root /opt/sfagent
if [ "$SYSTEM_TYPE" = "systemd" ]; then
create_sfagent_service
else
create_sfagent_init_script
fi
service sfagent restart
echo "Upgrading sfagent binaries completed"
else
echo "directory $AGENTDIR doesn't exists"
install_apm_agent
fi
}
install_apm_agent()
{
echo " "
echo "Install sfagent started"
ARCH=`uname -m`
rm -rf checksum* sfagent* mappings normalization $AGENTDIR
curl -sL $RELEASEURL \
| grep -w "browser_download_url" \
| cut -d":" -f 2,3 \
| tr -d '"' \
| xargs wget -q
ls -l sfagent* checksum* >/dev/null
tar -zxvf sfagent*linux_$ARCH.tar.gz >/dev/null
mkdir -p $AGENTDIR
mkdir -p $AGENTDIR/mappings
mkdir -p $AGENTDIR/scripts
mkdir -p $AGENTDIR/certs
mkdir -p $AGENTDIR/normalization
mv sfagent $AGENTDIR
mv jolokia.jar $AGENTDIR
mv mappings $AGENTDIR/.
mv scripts $AGENTDIR/.
mv certs $AGENTDIR/.
mv normalization $AGENTDIR/.
mv config.yaml.sample $AGENTDIR/config.yaml.sample
cat > $AGENTDIR/config.yaml <<EOF
agent:
metrics:
logging:
tags:
key:
EOF
chown -R root:root /opt/sfagent
if [ "$SYSTEM_TYPE" = "systemd" ]; then
create_sfagent_service
else
create_sfagent_init_script
fi
service sfagent restart
echo "Install sfagent completed"
echo " "
}
check_jcmd_installation()
{
echo " "
echo "Checking jcmd installation"
if ! [ -x "$(command -v jcmd)" ]; then
echo "Warning: jcmd is not installed. Java applications will not be detected automatically"
else
echo "jcmd is installed"
fi
}
create_env_file()
{
echo "Create $AGENTDIR/env.conf file"
touch $AGENTDIR/env.conf
if [ ! -z "$INCLUDE_PATHS" ];
then
echo "Extra paths to include in PATH - $INCLUDE_PATHS"
IFS=","
for v in $INCLUDE_PATHS
do
DEFAULTPATH="$DEFAULTPATH:$v"
done
fi
echo "Environment PATH=$DEFAULTPATH"
echo "PATH=$DEFAULTPATH" > $AGENTDIR/env.conf
if [ ! -z "$ENV_VARS" ]
then
echo "Append env vars to $AGENTDIR/env.conf"
IFS=","
for v in $ENV_VARS
do
echo $v >> $AGENTDIR/env.conf
done
fi
}
create_sfagent_service()
{
# create env file
create_env_file
echo "create sfagent service file"
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
systemctl enable sfagent
}
create_sfagent_init_script()
{
echo "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
;;
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"
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"
echo "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 [ ! -z "$UNKNOWN" ]
then
echo "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
echo "Need to have 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
echo "Upgrading fluent-bit binary"
upgrade_fluent_bit
echo "Upgrading sfagent binaries"
upgrade_apm_agent
echo "Upgrading sftrace agent"
upgrade_sftrace_agent
else
echo "Check jcmd installed"
check_jcmd_installation
echo "Installing fluent-bit binary"
install_fluent_bit
echo "Installing APM agent"
install_apm_agent
echo "Installing sftrace agent"
install_sftrace_agent
fi
cd $oldpath
rm -rf $tmp_dir
sleep 1
echo "Done"
exit 0