6
6
7
7
set -u
8
8
9
+ ERROR_TAR_FAILED=5
10
+ ERROR_PROCFS_SAVE_FAILED=6
11
+ ERROR_INVALID_ARGUMENT=10
12
+
9
13
TAR=tar
10
14
MKDIR=mkdir
11
15
RM=rm
@@ -14,10 +18,13 @@ GZIP=gzip
14
18
CP=cp
15
19
MV=mv
16
20
GREP=grep
21
+ TOUCH=touch
17
22
V=
18
23
NOOP=false
19
24
DO_COMPRESS=true
20
25
CMD_PREFIX=
26
+ SINCE_DATE=" @0" # default is set to January 1, 1970 at 00:00:00 GMT
27
+ REFERENCE_FILE=/tmp/reference
21
28
BASE=sonic_dump_` hostname` _` date +%Y%m%d_%H%M%S`
22
29
DUMPDIR=/var/dump
23
30
TARDIR=$DUMPDIR /$BASE
@@ -72,7 +79,7 @@ save_cmd() {
72
79
fi
73
80
fi
74
81
($TAR $V -rhf $TARFILE -C $DUMPDIR " $tarpath " \
75
- || abort 5 " tar append operation failed. Aborting to prevent data loss." ) \
82
+ || abort " ${ERROR_TAR_FAILED} " " tar append operation failed. Aborting to prevent data loss." ) \
76
83
&& $RM $V -rf " $filepath "
77
84
}
78
85
@@ -151,7 +158,7 @@ save_proc() {
151
158
local procfiles=" $@ "
152
159
$MKDIR $V -p $TARDIR /proc \
153
160
&& $CP $V -r $procfiles $TARDIR /proc \
154
- && $TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+r $BASE /proc \
161
+ && $TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+rw $BASE /proc \
155
162
&& $RM $V -rf $TARDIR /proc
156
163
}
157
164
@@ -212,10 +219,54 @@ save_file() {
212
219
fi
213
220
fi
214
221
($TAR $V -rhf $TARFILE -C $DUMPDIR " $tar_path " \
215
- || abort 5 " tar append operation failed. Aborting to prevent data loss." ) \
222
+ || abort " ${ERROR_PROCFS_SAVE_FAILED} " " tar append operation failed. Aborting to prevent data loss." ) \
216
223
&& $RM $V -f " $gz_path "
217
224
}
218
225
226
+ # ##############################################################################
227
+ # find_files routine
228
+ # Globals:
229
+ # SINCE_DATE: list files only newer than given date
230
+ # REFERENCE_FILE: the file to be created as a reference to compare modification time
231
+ # Arguments:
232
+ # directory: directory to search files in
233
+ # Returns:
234
+ # None
235
+ # ##############################################################################
236
+ find_files () {
237
+ local -r directory=$1
238
+ $TOUCH --date=" ${SINCE_DATE} " " ${REFERENCE_FILE} "
239
+ local -r find_command=" find -L $directory -type f -newer ${REFERENCE_FILE} "
240
+
241
+ echo $( $find_command )
242
+ }
243
+
244
+ # ##############################################################################
245
+ # disable_logrotate routine
246
+ # Globals:
247
+ # None
248
+ # Arguments:
249
+ # None
250
+ # Returns:
251
+ # None
252
+ # ##############################################################################
253
+ disable_logrotate () {
254
+ sed -i ' /logrotate/s/^/#/g' /etc/cron.d/logrotate
255
+ }
256
+
257
+ # ##############################################################################
258
+ # enable_logrotate routine
259
+ # Globals:
260
+ # None
261
+ # Arguments:
262
+ # None
263
+ # Returns:
264
+ # None
265
+ # ##############################################################################
266
+ enable_logrotate () {
267
+ sed -i ' /logrotate/s/^#*//g' /etc/cron.d/logrotate
268
+ }
269
+
219
270
# ##############################################################################
220
271
# Main generate_dump routine
221
272
# Globals:
@@ -251,7 +302,7 @@ main() {
251
302
/proc/softirqs /proc/stat /proc/swaps /proc/sysvipc /proc/timer_list \
252
303
/proc/uptime /proc/version /proc/vmallocinfo /proc/vmstat \
253
304
/proc/zoneinfo \
254
- || abort 6 " Proc saving operation failed. Aborting for safety."
305
+ || abort " ${ERROR_PROCFS_SAVE_FAILED} " " Proc saving operation failed. Aborting for safety."
255
306
256
307
save_cmd " show version" " version"
257
308
save_cmd " show platform summary" " platform.summary"
@@ -303,9 +354,16 @@ main() {
303
354
local platform=" $( /usr/local/bin/sonic-cfggen -H -v DEVICE_METADATA.localhost.platform) "
304
355
if [[ $platform == * " mlnx" * ]]; then
305
356
local sai_dump_filename=" /tmp/sai_sdk_dump_$( date +" %m_%d_%Y_%I_%M_%p" ) "
306
- docker exec -it syncd saisdkdump -f $sai_dump_filename
307
- docker exec syncd tar Ccf $( dirname $sai_dump_filename ) - $( basename $sai_dump_filename ) | tar Cxf /tmp/ -
357
+ ${CMD_PREFIX} docker exec -it syncd saisdkdump -f $sai_dump_filename
358
+ ${CMD_PREFIX} docker exec syncd tar Ccf $( dirname $sai_dump_filename ) - $( basename $sai_dump_filename ) | tar Cxf /tmp/ -
308
359
save_file $sai_dump_filename sai_sdk_dump true
360
+
361
+ local mst_dump_filename=" /tmp/mstdump"
362
+ local max_dump_count=" 3"
363
+ for i in $( seq 1 $max_dump_count ) ; do
364
+ ${CMD_PREFIX} /usr/bin/mstdump /dev/mst/mt* conf0 > " ${mst_dump_filename}${i} "
365
+ save_file " ${mst_dump_filename}${i} " mstdump true
366
+ done
309
367
fi
310
368
311
369
local asic=" $( /usr/local/bin/sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type) "
@@ -326,7 +384,7 @@ main() {
326
384
$MKDIR $V -p $LOGDIR
327
385
$LN $V -s /etc $TARDIR /etc
328
386
329
- ($TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+r \
387
+ ($TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+rw \
330
388
--exclude=" etc/alternatives" \
331
389
--exclude=" */etc/passwd*" \
332
390
--exclude=" */etc/shadow*" \
@@ -335,12 +393,17 @@ main() {
335
393
--exclude=" */etc/ssh*" \
336
394
--exclude=" *get_creds*" \
337
395
--exclude=" *snmpd.conf*" \
396
+ --exclude=" /etc/mlnx" \
397
+ --exclude=" /etc/mft" \
338
398
$BASE /etc \
339
- || abort 5 " Tar append operation failed. Aborting for safety." ) \
399
+ || abort " ${ERROR_TAR_FAILED} " " Tar append operation failed. Aborting for safety." ) \
340
400
&& $RM $V -rf $TARDIR
341
401
402
+ disable_logrotate
403
+ trap enable_logrotate HUP INT QUIT TERM KILL ABRT ALRM
404
+
342
405
# gzip up all log files individually before placing them in the incremental tarball
343
- for file in $( find -L /var/log -type f ) ; do
406
+ for file in $( find_files " /var/log/ " ) ; do
344
407
# ignore the sparse file lastlog
345
408
if [ " $file " = " /var/log/lastlog" ]; then
346
409
continue
@@ -353,8 +416,10 @@ main() {
353
416
fi
354
417
done
355
418
419
+ enable_logrotate
420
+
356
421
# archive core dump files
357
- for file in $( find -L /var/core -type f ) ; do
422
+ for file in $( find_files " /var/core/ " ) ; do
358
423
# don't gzip already-gzipped log files :)
359
424
if [ -z " ${file##* .gz} " ]; then
360
425
save_file $file core false
@@ -422,10 +487,15 @@ OPTIONS
422
487
Noop mode. Don't actually create anything, just echo what would happen
423
488
-z
424
489
Don't compress the tar at the end.
490
+ -s DATE
491
+ Collect logs since DATE;
492
+ The argument is a mostly free format human readable string such as
493
+ "24 March", "yesterday", etc.
494
+
425
495
EOF
426
496
}
427
497
428
- while getopts " :xnvhz " opt; do
498
+ while getopts " :xnvhzs: " opt; do
429
499
case $opt in
430
500
x)
431
501
# enable bash debugging
@@ -450,11 +520,17 @@ while getopts ":xnvhz" opt; do
450
520
CMD_PREFIX=" echo "
451
521
MV=" echo mv"
452
522
CP=" echo cp"
523
+ TOUCH=" echo touch"
453
524
NOOP=true
454
525
;;
455
526
z)
456
527
DO_COMPRESS=false
457
528
;;
529
+ s)
530
+ SINCE_DATE=" ${OPTARG} "
531
+ # validate date expression
532
+ date --date=" ${SINCE_DATE} " & > /dev/null || abort " ${ERROR_INVALID_ARGUMENT} " " Invalid date expression passed: '${SINCE_DATE} '"
533
+ ;;
458
534
/? )
459
535
echo " Invalid option: -$OPTARG " >&2
460
536
exit 1
0 commit comments