This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patheks-log-collector.sh
executable file
·529 lines (439 loc) · 15 KB
/
eks-log-collector.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
#!/usr/bin/env bash
# This script generates a file in go with the license contents as a constant
# Set language to C to make sorting consistent among different environments.
export LANG="C"
export LC_ALL="C"
# Global options
readonly PROGRAM_VERSION="0.6.2"
readonly PROGRAM_SOURCE="https://github.com/awslabs/amazon-eks-ami/blob/master/log-collector-script/"
readonly PROGRAM_NAME="$(basename "$0" .sh)"
readonly PROGRAM_DIR="/opt/log-collector"
readonly LOG_DIR="/var/log"
readonly COLLECT_DIR="/tmp/${PROGRAM_NAME}"
readonly CURRENT_TIME=$(date --utc +%Y-%m-%d_%H%M-%Z)
readonly DAYS_10=$(date -d "-10 days" '+%Y-%m-%d %H:%M')
INSTANCE_ID=""
INIT_TYPE=""
PACKAGE_TYPE=""
# Script run defaults
ignore_introspection='false'
ignore_metrics='false'
REQUIRED_UTILS=(
timeout
curl
tar
date
mkdir
iptables
iptables-save
grep
awk
df
sysctl
)
COMMON_DIRECTORIES=(
kernel
system
docker
storage
var_log
networking
ipamd # eks
sysctls # eks
kubelet # eks
cni # eks
)
COMMON_LOGS=(
syslog
messages
aws-routed-eni # eks
containers # eks
pods # eks
cloud-init.log
cloud-init-output.log
kube-proxy.log
)
# L-IPAMD introspection data points
IPAMD_DATA=(
enis
pods
networkutils-env-settings
ipamd-env-settings
eni-configs
)
help() {
echo ""
echo "USAGE: ${PROGRAM_NAME} --help [ --ignore_introspection=true|false --ignore_metrics=true|false ]"
echo ""
echo "OPTIONS:"
echo ""
echo " --ignore_introspection To ignore introspection of IPAMD; Pass this flag if DISABLE_INTROSPECTION is enabled on CNI"
echo ""
echo " --ignore_metrics Variable To ignore prometheus metrics collection; Pass this flag if DISABLE_METRICS enabled on CNI"
echo ""
echo " --help Show this help message."
echo ""
}
parse_options() {
local count="$#"
for i in $(seq "${count}"); do
eval arg="\$$i"
param="$(echo "${arg}" | awk -F '=' '{print $1}' | sed -e 's|--||')"
val="$(echo "${arg}" | awk -F '=' '{print $2}')"
case "${param}" in
ignore_introspection)
eval "${param}"="${val}"
;;
ignore_metrics)
eval "${param}"="${val}"
;;
help)
help && exit 0
;;
*)
echo "Parameter not found: '$param'"
help && exit 1
;;
esac
done
}
ok() {
echo
}
try() {
local action=$*
echo -n "Trying to $action... "
}
warning() {
local reason=$*
echo -e "\n\n\tWarning: $reason "
}
die() {
echo -e "\n\tFatal Error! $* Exiting!\n"
exit 1
}
is_root() {
if [[ "$(id -u)" -ne 0 ]]; then
die "This script must be run as root!"
fi
}
check_required_utils() {
for utils in ${REQUIRED_UTILS[*]}; do
# If exit code of "command -v" not equal to 0, fail
if ! command -v "${utils}" >/dev/null 2>&1; then
die "Application \"${utils}\" is missing, please install \"${utils}\" as this script requires it, and will not function without it."
fi
done
}
version_output() {
echo -e "\n\tThis is version ${PROGRAM_VERSION}. New versions can be found at ${PROGRAM_SOURCE}\n"
}
log_parameters() {
echo ignore_introspection: "${ignore_introspection}" >> "${COLLECT_DIR}"/system/script-params.txt
echo ignore_metrics: "${ignore_metrics}" >> "${COLLECT_DIR}"/system/script-params.txt
}
systemd_check() {
if command -v systemctl >/dev/null 2>&1; then
INIT_TYPE="systemd"
if command -v snap >/dev/null 2>&1; then
INIT_TYPE="snap"
fi
else
INIT_TYPE="other"
fi
}
create_directories() {
# Make sure the directory the script lives in is there. Not an issue if
# the EKS AMI is used, as it will have it.
mkdir -p "${PROGRAM_DIR}"
# Common directories creation
for directory in ${COMMON_DIRECTORIES[*]}; do
mkdir -p "${COLLECT_DIR}"/"${directory}"
done
}
get_instance_metadata() {
readonly INSTANCE_ID=$(curl --max-time 3 --silent http://169.254.169.254/latest/meta-data/instance-id 2>/dev/null)
echo "${INSTANCE_ID}" > "${COLLECT_DIR}"/system/instance-id.txt
}
is_diskfull() {
local threshold
local result
# 1.5GB in KB
threshold=1500000
result=$(df / | grep --invert-match "Filesystem" | awk '{ print $4 }')
# If "result" is less than or equal to "threshold", fail.
if [[ "${result}" -le "${threshold}" ]]; then
die "Free space on root volume is less than or equal to $((threshold>>10))MB, please ensure adequate disk space to collect and store the log files."
fi
}
cleanup() {
rm --recursive --force "${COLLECT_DIR}" >/dev/null 2>&1
}
init() {
check_required_utils
version_output
create_directories
# Log parameters passed when this script is invoked
log_parameters
is_root
systemd_check
get_pkgtype
}
collect() {
init
is_diskfull
get_instance_metadata
get_common_logs
get_kernel_info
get_mounts_info
get_selinux_info
get_iptables_info
get_pkglist
get_system_services
get_docker_info
get_k8s_info
get_ipamd_info
get_sysctls_info
get_networking_info
get_cni_config
get_docker_logs
}
pack() {
try "archive gathered information"
tar --create --verbose --gzip --file "${LOG_DIR}"/eks_"${INSTANCE_ID}"_"${CURRENT_TIME}"_"${PROGRAM_VERSION}".tar.gz --directory="${COLLECT_DIR}" . > /dev/null 2>&1
ok
}
finished() {
cleanup
echo -e "\n\tDone... your bundled logs are located in ${LOG_DIR}/eks_${INSTANCE_ID}_${CURRENT_TIME}_${PROGRAM_VERSION}.tar.gz\n"
}
get_mounts_info() {
try "collect mount points and volume information"
mount > "${COLLECT_DIR}"/storage/mounts.txt
echo >> "${COLLECT_DIR}"/storage/mounts.txt
df --human-readable >> "${COLLECT_DIR}"/storage/mounts.txt
lsblk > "${COLLECT_DIR}"/storage/lsblk.txt
lvs > "${COLLECT_DIR}"/storage/lvs.txt
pvs > "${COLLECT_DIR}"/storage/pvs.txt
vgs > "${COLLECT_DIR}"/storage/vgs.txt
ok
}
get_selinux_info() {
try "collect SELinux status"
if ! command -v getenforce >/dev/null 2>&1; then
echo -e "SELinux mode:\n\t Not installed" > "${COLLECT_DIR}"/system/selinux.txt
else
echo -e "SELinux mode:\n\t $(getenforce)" > "${COLLECT_DIR}"/system/selinux.txt
fi
ok
}
get_iptables_info() {
try "collect iptables information"
iptables --wait 1 --numeric --verbose --list --table mangle > "${COLLECT_DIR}"/networking/iptables-mangle.txt
iptables --wait 1 --numeric --verbose --list --table filter > "${COLLECT_DIR}"/networking/iptables-filter.txt
iptables --wait 1 --numeric --verbose --list --table nat > "${COLLECT_DIR}"/networking/iptables-nat.txt
iptables --wait 1 --numeric --verbose --list > "${COLLECT_DIR}"/networking/iptables.txt
iptables-save > "${COLLECT_DIR}"/networking/iptables-save.txt
ok
}
get_common_logs() {
try "collect common operating system logs"
for entry in ${COMMON_LOGS[*]}; do
if [[ -e "/var/log/${entry}" ]]; then
if [[ "${entry}" == "messages" ]]; then
tail -c 10M /var/log/messages > "${COLLECT_DIR}"/var_log/messages
continue
fi
if [[ "${entry}" == "containers" ]]; then
cp --force --dereference --recursive /var/log/containers/aws-node* "${COLLECT_DIR}"/var_log/ 2>/dev/null
cp --force --dereference --recursive /var/log/containers/kube-system_cni-metrics-helper* "${COLLECT_DIR}"/var_log/ 2>/dev/null
cp --force --dereference --recursive /var/log/containers/coredns-* "${COLLECT_DIR}"/var_log/ 2>/dev/null
cp --force --dereference --recursive /var/log/containers/kube-proxy* "${COLLECT_DIR}"/var_log/ 2>/dev/null
continue
fi
if [[ "${entry}" == "pods" ]]; then
cp --force --dereference --recursive /var/log/pods/kube-system_aws-node* "${COLLECT_DIR}"/var_log/ 2>/dev/null
cp --force --dereference --recursive /var/log/pods/kube-system_cni-metrics-helper* "${COLLECT_DIR}"/var_log/ 2>/dev/null
cp --force --dereference --recursive /var/log/pods/kube-system_coredns* "${COLLECT_DIR}"/var_log/ 2>/dev/null
cp --force --dereference --recursive /var/log/pods/kube-system_kube-proxy* "${COLLECT_DIR}"/var_log/ 2>/dev/null
continue
fi
cp --force --recursive --dereference /var/log/"${entry}" "${COLLECT_DIR}"/var_log/ 2>/dev/null
fi
done
ok
}
get_kernel_info() {
try "collect kernel logs"
if [[ -e "/var/log/dmesg" ]]; then
cp --force /var/log/dmesg "${COLLECT_DIR}/kernel/dmesg.boot"
fi
dmesg > "${COLLECT_DIR}/kernel/dmesg.current"
dmesg --ctime > "${COLLECT_DIR}/kernel/dmesg.human.current"
uname -a > "${COLLECT_DIR}/kernel/uname.txt"
ok
}
get_docker_logs() {
try "collect Docker daemon logs"
case "${INIT_TYPE}" in
systemd|snap)
journalctl --unit=docker --since "${DAYS_10}" > "${COLLECT_DIR}"/docker/docker.log
;;
other)
for entry in docker upstart/docker; do
if [[ -e "/var/log/${entry}" ]]; then
cp --force --recursive --dereference /var/log/"${entry}" "${COLLECT_DIR}"/docker/
fi
done
;;
*)
warning "The current operating system is not supported."
;;
esac
ok
}
get_k8s_info() {
try "collect kubelet information"
if [[ -n "${KUBECONFIG:-}" ]]; then
command -v kubectl > /dev/null && kubectl get --kubeconfig="${KUBECONFIG}" svc > "${COLLECT_DIR}"/kubelet/svc.log
command -v kubectl > /dev/null && kubectl --kubeconfig="${KUBECONFIG}" config view --output yaml > "${COLLECT_DIR}"/kubelet/kubeconfig.yaml
elif [[ -f /etc/eksctl/kubeconfig.yaml ]]; then
KUBECONFIG="/etc/eksctl/kubeconfig.yaml"
command -v kubectl > /dev/null && kubectl get --kubeconfig="${KUBECONFIG}" svc > "${COLLECT_DIR}"/kubelet/svc.log
command -v kubectl > /dev/null && kubectl --kubeconfig="${KUBECONFIG}" config view --output yaml > "${COLLECT_DIR}"/kubelet/kubeconfig.yaml
elif [[ -f /etc/systemd/system/kubelet.service ]]; then
KUBECONFIG=$(grep kubeconfig /etc/systemd/system/kubelet.service | awk '{print $2}')
command -v kubectl > /dev/null && kubectl get --kubeconfig="${KUBECONFIG}" svc > "${COLLECT_DIR}"/kubelet/svc.log
command -v kubectl > /dev/null && kubectl --kubeconfig="${KUBECONFIG}" config view --output yaml > "${COLLECT_DIR}"/kubelet/kubeconfig.yaml
elif [[ -f /var/lib/kubelet/kubeconfig ]]; then
KUBECONFIG="/var/lib/kubelet/kubeconfig"
command -v kubectl > /dev/null && kubectl get --kubeconfig=${KUBECONFIG} svc > "${COLLECT_DIR}"/kubelet/svc.log
command -v kubectl > /dev/null && kubectl --kubeconfig=${KUBECONFIG} config view --output yaml > "${COLLECT_DIR}"/kubelet/kubeconfig.yaml
else
echo "======== Unable to find KUBECONFIG, IGNORING POD DATA =========" >> "${COLLECT_DIR}"/kubelet/svc.log
fi
# Try to copy the kubeconfig file if kubectl command doesn't exist
[[ (! -f "${COLLECT_DIR}/kubelet/kubeconfig.yaml") && ( -n ${KUBECONFIG}) ]] && cp ${KUBECONFIG} "${COLLECT_DIR}"/kubelet/kubeconfig.yaml
case "${INIT_TYPE}" in
systemd)
timeout 75 journalctl --unit=kubelet --since "${DAYS_10}" > "${COLLECT_DIR}"/kubelet/kubelet.log
systemctl cat kubelet > "${COLLECT_DIR}"/kubelet/kubelet_service.txt 2>&1
;;
snap)
timeout 75 snap logs kubelet-eks -n all > "${COLLECT_DIR}"/kubelet/kubelet.log
timeout 75 snap get kubelet-eks > "${COLLECT_DIR}"/kubelet/kubelet-eks_service.txt 2>&1
;;
*)
warning "The current operating system is not supported."
;;
esac
ok
}
get_ipamd_info() {
if [[ "${ignore_introspection}" == "false" ]]; then
try "collect L-IPAMD introspection information"
for entry in ${IPAMD_DATA[*]}; do
curl --max-time 3 --silent http://localhost:61679/v1/"${entry}" >> "${COLLECT_DIR}"/ipamd/"${entry}".json
done
else
echo "Ignoring IPAM introspection stats as mentioned"| tee -a "${COLLECT_DIR}"/ipamd/ipam_introspection_ignore.txt
fi
if [[ "${ignore_metrics}" == "false" ]]; then
try "collect L-IPAMD prometheus metrics"
curl --max-time 3 --silent http://localhost:61678/metrics > "${COLLECT_DIR}"/ipamd/metrics.json 2>&1
else
echo "Ignoring Prometheus Metrics collection as mentioned"| tee -a "${COLLECT_DIR}"/ipamd/ipam_metrics_ignore.txt
fi
try "collect L-IPAMD checkpoint"
cp /var/run/aws-node/ipam.json "${COLLECT_DIR}"/ipamd/ipam.json
ok
}
get_sysctls_info() {
try "collect sysctls information"
# dump all sysctls
sysctl --all >> "${COLLECT_DIR}"/sysctls/sysctl_all.txt 2>/dev/null
ok
}
get_networking_info() {
try "collect networking infomation"
# conntrack info
echo "*** Output of conntrack -S *** " >> "${COLLECT_DIR}"/networking/conntrack.txt
timeout 75 conntrack -S >> "${COLLECT_DIR}"/networking/conntrack.txt
echo "*** Output of conntrack -L ***" >> "${COLLECT_DIR}"/networking/conntrack.txt
timeout 75 conntrack -L >> "${COLLECT_DIR}"/networking/conntrack.txt
# ifconfig
timeout 75 ifconfig > "${COLLECT_DIR}"/networking/ifconfig.txt
# ip rule show
timeout 75 ip rule show > "${COLLECT_DIR}"/networking/iprule.txt
timeout 75 ip route show table all >> "${COLLECT_DIR}"/networking/iproute.txt
ok
}
get_cni_config() {
try "collect CNI configuration information"
if [[ -e "/etc/cni/net.d/" ]]; then
cp --force --recursive --dereference /etc/cni/net.d/* "${COLLECT_DIR}"/cni/
fi
ok
}
get_pkgtype() {
if [[ "$(command -v rpm )" ]]; then
PACKAGE_TYPE=rpm
elif [[ "$(command -v dpkg )" ]]; then
PACKAGE_TYPE=deb
else
PACKAGE_TYPE='unknown'
fi
}
get_pkglist() {
try "collect installed packages"
case "${PACKAGE_TYPE}" in
rpm)
rpm -qa > "${COLLECT_DIR}"/system/pkglist.txt 2>&1
;;
deb)
dpkg --list > "${COLLECT_DIR}"/system/pkglist.txt 2>&1
;;
*)
warning "Unknown package type."
;;
esac
ok
}
get_system_services() {
try "collect active system services"
case "${INIT_TYPE}" in
systemd|snap)
systemctl list-units > "${COLLECT_DIR}"/system/services.txt 2>&1
;;
other)
initctl list | awk '{ print $1 }' | xargs -n1 initctl show-config > "${COLLECT_DIR}"/system/services.txt 2>&1
printf "\n\n\n\n" >> "${COLLECT_DIR}"/system/services.txt 2>&1
service --status-all >> "${COLLECT_DIR}"/system/services.txt 2>&1
;;
*)
warning "Unable to determine active services."
;;
esac
timeout 75 top -b -n 1 > "${COLLECT_DIR}"/system/top.txt 2>&1
timeout 75 ps fauxwww > "${COLLECT_DIR}"/system/ps.txt 2>&1
timeout 75 netstat -plant > "${COLLECT_DIR}"/system/netstat.txt 2>&1
ok
}
get_docker_info() {
try "collect Docker daemon information"
if [[ "$(pgrep -o dockerd)" -ne 0 ]]; then
timeout 75 docker info > "${COLLECT_DIR}"/docker/docker-info.txt 2>&1 || echo -e "\tTimed out, ignoring \"docker info output \" "
timeout 75 docker ps --all --no-trunc > "${COLLECT_DIR}"/docker/docker-ps.txt 2>&1 || echo -e "\tTimed out, ignoring \"docker ps --all --no-truc output \" "
timeout 75 docker images > "${COLLECT_DIR}"/docker/docker-images.txt 2>&1 || echo -e "\tTimed out, ignoring \"docker images output \" "
timeout 75 docker version > "${COLLECT_DIR}"/docker/docker-version.txt 2>&1 || echo -e "\tTimed out, ignoring \"docker version output \" "
else
warning "The Docker daemon is not running."
fi
ok
}
# -----------------------------------------------------------------------------
# Entrypoint
parse_options "$@"
collect
pack
finished