This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
collect-data.sh.in
executable file
·570 lines (443 loc) · 10.8 KB
/
collect-data.sh.in
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
#!/bin/bash
#
# Copyright (c) 2017-2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
script_name=${0##*/}
runtime_name="@RUNTIME_NAME@"
runtime=$(command -v "$runtime_name" 2>/dev/null)
issue_url="@PROJECT_BUG_URL@"
script_version="@VERSION@ (commit @COMMIT@)"
typeset -r unknown="unknown"
# Maximum number of errors to show for a single system component
# (such as runtime or proxy).
PROBLEM_LIMIT=${PROBLEM_LIMIT:-50}
# List of patterns used to detect problems in logfiles.
problem_pattern="("
problem_pattern+="\<abort|"
problem_pattern+="\<bug\>|"
problem_pattern+="\<cannot\>|"
problem_pattern+="\<catastrophic|"
problem_pattern+="\<could not\>|"
problem_pattern+="\<couldn\'t\>|"
problem_pattern+="\<critical|"
problem_pattern+="\<die\>|"
problem_pattern+="\<died\>|"
problem_pattern+="\<does.*not.*exist\>|"
problem_pattern+="\<dying\>|"
problem_pattern+="\<empty\>|"
problem_pattern+="\<erroneous|"
problem_pattern+="\<error|"
problem_pattern+="\<expected\>|"
problem_pattern+="\<fail|"
problem_pattern+="\<fatal|"
problem_pattern+="\<impossible\>|"
problem_pattern+="\<impossibly\>|"
problem_pattern+="\<incorrect|"
problem_pattern+="\<invalid\>|"
problem_pattern+="\<level=\"*error\"* |"
problem_pattern+="\<level=\"*fatal\"* |"
problem_pattern+="\<level=\"*panic\"* |"
problem_pattern+="\<level=\"*warning\"* |"
problem_pattern+="\<missing\>|"
problem_pattern+="\<need\>|"
problem_pattern+="\<no.*such.*file\>|"
problem_pattern+="\<not.*found\>|"
problem_pattern+="\<not.*supported\>|"
problem_pattern+="\<too many\>|"
problem_pattern+="\<unable\>|"
problem_pattern+="\<unavailable\>|"
problem_pattern+="\<unexpected|"
problem_pattern+="\<unknown\>|"
problem_pattern+="\<urgent|"
problem_pattern+="\<warn\>|"
problem_pattern+="\<warning\>|"
problem_pattern+="\<wrong\>"
problem_pattern+=")"
# List of patterns used to exclude messages that are not problems
problem_exclude_pattern="("
problem_exclude_pattern+="\<launching .* with:"
problem_exclude_pattern+=")"
usage()
{
cat <<EOT
Usage: $script_name [options]
Summary: Collect data about an installation of @PROJECT_NAME@.
Description: Run this script as root to obtain a markdown-formatted summary
of the environment of the @PROJECT_NAME@ installation. The output of this script
can be pasted directly into a github issue at the address below:
$issue_url
Options:
-h | --help : show this usage summary.
-v | --version : show program version.
EOT
}
version()
{
cat <<EOT
$script_name version $script_version
EOT
}
die()
{
local msg="$*"
echo >&2 "ERROR: $script_name: $msg"
exit 1
}
msg()
{
local msg="$*"
echo "$msg"
}
heading()
{
local name="$*"
echo -e "\n# $name\n"
}
subheading()
{
local name="$*"
echo -e "\n## $name\n"
}
separator()
{
echo -e '\n---\n'
}
have_cmd()
{
local cmd="$1"
command -v "$cmd" &>/dev/null
local ret=$?
if [ $ret -eq 0 ]; then
msg "Have \`$cmd\`"
else
msg "No \`$cmd\`"
fi
[ $ret -eq 0 ]
}
show_quoted_text()
{
local text="$*"
echo "\`\`\`"
echo "$text"
echo "\`\`\`"
}
run_cmd_and_show_quoted_output()
{
local cmd="$*"
local output
msg "Output of \"\`$cmd\`\":"
output=$(eval "$cmd" 2>&1)
show_quoted_text "$output"
}
show_runtime_configs()
{
local configs config
heading "Runtime config files"
configs=$($runtime --@PROJECT_TYPE@-show-default-config-paths)
if [ $? -ne 0 ]; then
version=$($runtime --version|tr '\n' ' ')
die "failed to check config files - runtime is probably too old ($version)"
fi
subheading "Runtime default config files"
show_quoted_text "$configs"
# add in the standard defaults for good measure "just in case"
configs+=" /etc/@PROJECT_TAG@/configuration.toml"
configs+=" /usr/share/defaults/@PROJECT_TAG@/configuration.toml"
configs+=" @DESTCONFIG@"
configs+=" @DESTSYSCONFIG@"
# create a unique list of config files
configs=$(echo $configs|tr ' ' '\n'|sort -u)
subheading "Runtime config file contents"
for config in $configs; do
if [ -e "$config" ]; then
run_cmd_and_show_quoted_output "cat \"$config\""
else
msg "Config file \`$config\` not found"
fi
done
separator
}
show_log_details()
{
heading "Logfiles"
show_runtime_log_details
show_proxy_log_details
show_shim_log_details
separator
}
show_runtime_log_details()
{
subheading "Runtime logs"
find_system_journal_problems "runtime" "@RUNTIME_NAME@" ""
}
find_system_journal_problems()
{
local name="$1"
local program="$2"
local unit="$3"
# select by identifier
local selector="-t"
local data_source="system journal"
local problems=$(journalctl -q -o cat -a "$selector" "$program" |\
grep "time=" |\
egrep -i "$problem_pattern" |\
egrep -iv "$problem_exclude_pattern" |\
tail -n ${PROBLEM_LIMIT})
if [ -n "$problems" ]; then
msg "Recent $name problems found in $data_source:"
show_quoted_text "$problems"
else
msg "No recent $name problems found in $data_source."
fi
}
show_proxy_log_details()
{
subheading "Proxy logs"
find_system_journal_problems "proxy" "@PROJECT_TYPE@-proxy" ""
}
show_shim_log_details()
{
subheading "Shim logs"
find_system_journal_problems "shim" "@PROJECT_TYPE@-shim" ""
}
show_package_versions()
{
heading "Packages"
local pattern="("
local project
# CC 2.x runtime. This shouldn't be installed but let's check anyway
pattern+="cc-oci-runtime"
# core components
for project in @PROJECT_TYPES@
do
pattern+="|${project}-proxy"
pattern+="|${project}-runtime"
pattern+="|${project}-shim"
done
# assets
pattern+="|clear-containers-image"
pattern+="|linux-container"
# optimised hypervisor
pattern+="|qemu-lite"
# default distro hypervisor
pattern+="|qemu-system-x86"
pattern+=")"
if have_cmd "dpkg"; then
run_cmd_and_show_quoted_output "dpkg -l|egrep \"$pattern\""
fi
if have_cmd "rpm"; then
run_cmd_and_show_quoted_output "rpm -qa|egrep \"$pattern\""
fi
separator
}
show_container_mgr_details()
{
heading "Container manager details"
if have_cmd "docker"; then
subheading "Docker"
run_cmd_and_show_quoted_output "docker version"
run_cmd_and_show_quoted_output "docker info"
run_cmd_and_show_quoted_output "systemctl show docker"
fi
if have_cmd "kubectl"; then
subheading "Kubernetes"
run_cmd_and_show_quoted_output "kubectl version"
run_cmd_and_show_quoted_output "kubectl config view"
run_cmd_and_show_quoted_output "systemctl show kubelet"
if have_cmd "crio"; then
run_cmd_and_show_quoted_output "crio --version"
run_cmd_and_show_quoted_output "systemctl show crio"
fi
fi
separator
}
show_meta()
{
local date
heading "Meta details"
date=$(date '+%Y-%m-%d.%H:%M:%S.%N%z')
msg "Running \`$script_name\` version \`$script_version\` at \`$date\`."
separator
}
show_runtime()
{
local cmd
msg "Runtime is \`$runtime\`."
cmd="@PROJECT_TYPE@-env"
heading "\`$cmd\`"
run_cmd_and_show_quoted_output "$runtime $cmd"
separator
}
# Parameter 1: Path to disk image file.
# Returns: Agent version string, or "$unknown" on error.
get_agent_version()
{
local img="$1"
[ -z "$img" ] && { echo "$unknown"; return;}
[ -e "$img" ] || { echo "$unknown"; return;}
local loop_device
local partition_path
local partitions
local partition
local count
local mountpoint
local version
local expected
loop_device=$(loopmount_image "$img")
if [ -z "$loop_device" ]; then
echo "$unknown"
return
fi
partitions=$(get_partitions "$loop_device")
count=$(echo "$partitions"|wc -l)
expected=1
if [ "$count" -ne "$expected" ]; then
release_device "$loop_device"
echo "$unknown"
return
fi
partition="$partitions"
partition_path="/dev/${partition}"
if [ ! -e "$partition_path" ]; then
release_device "$loop_device"
echo "$unknown"
return
fi
mountpoint=$(mount_partition "$partition_path")
agent="/bin/@PROJECT_TYPE@-agent"
version=$(chroot "$mountpoint" "$agent" --version 2>/dev/null)
unmount_partition "$mountpoint"
release_device "$loop_device"
[ -z "$version" ] && version="$unknown"
echo "$version"
}
# Returns: Full path to the image file.
get_image_file()
{
local cmd="@PROJECT_TYPE@-env"
local cmdline="$runtime $cmd"
image=$(eval "$cmdline" 2>/dev/null |\
grep -A 1 '\[Image\]' |\
egrep "\<Path\> =" |\
awk '{print $3}' |\
tr -d '"')
echo "$image"
}
# Parameter 1: Path to disk image file.
# Returns: Path to loop device.
loopmount_image()
{
local img="$1"
[ -n "$img" ] || die "need image file"
local device_path
losetup -fP "$img"
device_path=$(losetup -j "$img" |\
cut -d: -f1 |\
sort -k1,1 |\
tail -1)
echo "$device_path"
}
# Parameter 1: Path to loop device.
# Returns: Partition names.
get_partitions()
{
local device_path="$1"
[ -n "$device_path" ] || die "need device path"
local device
local partitions
device=${device_path/\/dev\//}
partitions=$(lsblk -nli -o NAME "${device_path}" |\
grep -v "^${device}$")
echo "$partitions"
}
# Parameter 1: Path to disk partition device.
# Returns: Mountpoint.
mount_partition()
{
local partition="$1"
[ -n "$partition" ] || die "need partition"
[ -e "$partition" ] || die "partition does not exist: $partition"
local mountpoint
mountpoint=$(mktemp -d)
mount -oro,noload "$partition" "$mountpoint"
echo "$mountpoint"
}
# Parameter 1: Mountpoint.
unmount_partition()
{
local mountpoint="$1"
[ -n "$mountpoint" ] || die "need mountpoint"
[ -n "$mountpoint" ] || die "mountpoint does not exist: $mountpoint"
umount "$mountpoint"
}
# Parameter 1: Loop device path.
release_device()
{
local device="$1"
[ -n "$device" ] || die "need device"
[ -e "$device" ] || die "device does not exist: $device"
losetup -d "$device"
}
show_agent_version()
{
local image
local version
image=$(get_image_file)
version=$(get_agent_version "$image")
heading "Agent"
msg "version:"
show_quoted_text "$version"
separator
}
main()
{
args=$(getopt \
-n "$script_name" \
-a \
--options="dhv" \
--longoptions="debug help version" \
-- "$@")
eval set -- "$args"
[ $? -ne 0 ] && { usage && exit 1; }
[ $# -eq 0 ] && { usage && exit 0; }
while [ $# -gt 1 ]
do
case "$1" in
-d|--debug)
set -x
;;
-h|--help)
usage && exit 0
;;
-v|--version)
version && exit 0
;;
--)
shift
break
;;
esac
shift
done
[ $(id -u) -eq 0 ] || die "Need to run as root"
[ -n "$runtime" ] || die "cannot find runtime '$runtime_name'"
show_meta
show_runtime
show_runtime_configs
show_agent_version
show_log_details
show_container_mgr_details
show_package_versions
}
main "$@"