summaryrefslogtreecommitdiffstats
path: root/tools/dreport
blob: d37131c94a171589b54bf390007c09e9b08772b8 (plain)
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
#! /bin/bash

help=$"
        dreport creates an archive(xz compressed) consisting of the following:
                * Configuration information
                * Debug information
                * A summary report
        The type parameter controls the content of the data. The generated
        archive is stored in the user specified location.

usage: dreport [OPTION]

Options:
        -n, —-name <name>     Name to be used for the archive.
                              Default name format obmcdump_<id>_<epochtime>
        -d, —-dir <directory> Archive directory to copy the compressed report.
                              Default output directory is /tmp
        -i, —-id <id>         Dump identifier to associate with the archive.
                              Identifiers include numeric characters.
                              Default dump identifier is 0
        -t, —-type <type>     Data collection type. Valid types are
                              "user", "core", "elog".
                              Default type is "user" initiated.
        -p, —-path <path>     Optional contents to be included in the archive.
                              Valid paths are absolute file path or d-bus path
                              based on type parameter.
                                 -Absolute file path for "core" type.
                                 -elog d-bus object for "elog" type.
        -s, --size <size>     Maximum allowed size(in KB) of the archive.
                              Report will be truncated in case size exceeds
                              this limit. Default size is unlimited.
        -v, —-verbose         Increase logging verbosity.
        -V, --version         Output version information.
        -q, —-quiet           Only log fatal errors to stderr
        -h, —-help            Display this help and exit.
"

#CONSTANTS
declare -r TRUE=1
declare -r FALSE=0
declare -r UNLIMITED="unlimited"
declare -r SUMMARY_DUMP="summary"
declare -r TYPE_USER="user"
declare -r TYPE_CORE="core"
declare -r TYPE_ELOG="elog"
declare -r SUMMARY_LOG="summary.log"
declare -r DREPORT_LOG="dreport.log"
declare -r TMP_DIR="/tmp"
declare -r EPOCHTIME=$(date +"%s")
declare -r TIME_STAMP="date -u"

#Error Codes
declare -r SUCCESS="0"
declare -r INTERNAL_FAILURE="1"
declare -r RESOURCE_UNAVAILABLE="2"

#VARIABLES
declare -x name=""
declare -x dump_dir="/tmp"
declare -x dump_id="00000000"
declare -x dump_type=$TYPE_USER
declare -x verbose=$FALSE
declare -x quiet=$FALSE
declare -x dump_size="unlimited"
declare -x name_dir=""
declare -x optional_path=""
declare -x dreport_log=""
declare -x summary_log=""
declare -x cur_dump_size=0
declare -a command_list=("")
declare -x pid="0"
declare -x elog_id=""

# @brief Initialize general command array
#        Set of commands, which provide general debug information
function runlevel_general()
{
command_list=(
    get_fw_level
    bmc_state
    host_state
    chassis_state
    get_host_info
    get_uname
    get_uptime
    get_disk_usage
    get_host_info
    get_cpuinfo
    get_meminfo
    get_top
    )
}

# @brief Initialize user type command array
function runlevel_user()
{
command_list=(
    get_journal
    get_elog_all
    get_obmc_console
    get_failed_services
    )
}

# @brief Initialize core type command array
function runlevel_core()
{
command_list=(
    move_optional_file
    get_proc_journal
    get_failed_services
    get_obmc_console
    )
}

# @brief Initialize elog type command array
function runlevel_elog()
{
command_list=(
    get_proc_journal
    get_elog
    )
}

function get_fw_level()
{
    desc="Firmware Release"
    command="cat /etc/os-release"
    copy_loc="firmware_release.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_uname()
{
    desc="uname"
    command="uname -a"
    copy_loc="uname.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_uptime()
{
    desc="uptime"
    command="uptime"
    copy_loc="uptime.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_disk_usage()
{
    desc="Disk Usage"
    command="df -hT"
    copy_loc="disk_usage.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_journal()
{
    desc="Journal log"
    command="journalctl -o json-pretty -r"
    copy_loc="journal.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_proc_journal()
{
    desc="Process Journal log"
    command="journalctl -o verbose _PID=$pid"
    copy_loc="proc_journal.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_host_info()
{
    desc="Host information"
    command="hostnamectl status"
    copy_loc="hostnamectl.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_failed_services()
{
    desc="Failed Services"
    command="systemctl --failed"
    copy_loc="failed_services.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_obmc_console()
{
    desc="OBMC Console"
    command="cat /var/log/obmc-console.log"
    copy_loc="obmc_console.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_cpuinfo()
{
    desc="cpuinfo"
    command="cat /proc/cpuinfo"
    copy_loc="cpuinfo.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_meminfo()
{
    desc="meminfo"
    command="cat /proc/meminfo"
    copy_loc="meminfo.log"
    run_command "$command" "$copy_loc" "$desc"
}

function get_top()
{
    desc="top"
    command="top -n 1 -b"
    copy_loc="top.log"
    run_command "$command" "$copy_loc" "$desc"
}

function bmc_state()
{
    desc="BMC State"
    command="busctl get-property \
                    xyz.openbmc_project.State.BMC \
                    /xyz/openbmc_project/state/bmc0 \
                    xyz.openbmc_project.State.BMC \
                    CurrentBMCState"
    copy_loc="BMCState.log"
    run_command "$command" "$copy_loc" "$desc"
}

function host_state()
{
    desc="Host State"
    command="busctl get-property \
                    xyz.openbmc_project.State.Host \
                    /xyz/openbmc_project/state/host0 \
                    xyz.openbmc_project.State.Host \
                    CurrentHostState"
    copy_loc="HostState.log"
    run_command "$command" "$copy_loc" "$desc"
}

function chassis_state()
{
    desc="Chassis State"
    command="busctl get-property \
                    xyz.openbmc_project.State.Chassis \
                    /xyz/openbmc_project/state/chassis0 \
                    xyz.openbmc_project.State.Chassis \
                    CurrentPowerState"
    copy_loc="ChassisState.log"
    run_command "$command" "$copy_loc" "$desc"
}

# @brief get all available elogs in BMC.
function get_elog_all()
{
    desc="elog"

    entries=$(busctl --list --no-pager tree \
                     xyz.openbmc_project.Logging | grep \
                     '/xyz/openbmc_project/logging/entry/')

    #check for eSEL entries.
    if [ -z "$entries" ]; then
        log_info "No $desc entries"
        return 0
    fi

    command="busctl --list --no-pager tree \
                     xyz.openbmc_project.Logging | grep \
                     '/xyz/openbmc_project/logging/entry/' \
                     | xargs -I {} busctl --verbose --no-pager \
                     call xyz.openbmc_project.Logging {} \
                     org.freedesktop.DBus.Properties GetAll s \
                     xyz.openbmc_project.Logging.Entry"
    copy_loc="elog.log"
    run_command "$command" "$copy_loc" "$desc"
}

# @brief get elog specific to global elog_id
function get_elog()
{
    desc="elog id:$elog_id"

    command="busctl --verbose --no-pager \
                     call xyz.openbmc_project.Logging \
                     $optional_path \
                     org.freedesktop.DBus.Properties GetAll s \
                     xyz.openbmc_project.Logging.Entry"
    copy_loc="elog_$elog_id.log"
    run_command "$command" "$copy_loc" "$desc"
}

function move_optional_file()
{
    desc="Move Optional file"

    mv $optional_path $name_dir
    if [ $? -ne 0 ]; then
        log_error "Failed to move file $optional_path"
        return 1
    fi
    if check_size "$name_dir/$(basename "$optional_path")"; then
        log_info "Moving file $file_name"
    else
        log_warning "Skipping $file_name move"
    fi
}

# @brief Run the requested command and save the output
#        into temporary location for successful size check

function run_command()
{
    command="$1"
    copy_loc="$2"
    desc="$3"

    eval $command >> "$name_dir/$copy_loc"
    if [ $? -ne 0 ]; then
        log_error "Failed to collect $desc"
        return 1
    fi

    if check_size "$name_dir/$copy_loc"; then
        log_info "Collected $desc"
    else
        log_warning "Skipping $desc"
    fi
}

# @brief Initiate data collection based on the type.
# @return 0 on success, error code otherwise
function collect_data()
{
    runlevel_general
    capture_data "${command_list[@]}"

    case $dump_type in
        $TYPE_USER)
            runlevel_user
            capture_data "${command_list[@]}"
            ;;
        $TYPE_CORE)
            set_core_pid
            runlevel_core
            capture_data "${command_list[@]}"
            ;;
        $TYPE_ELOG)
            elog_id=$(basename "$optional_path")
            set_elog_pid
            runlevel_elog
            capture_data "${command_list[@]}"
            ;;

        $SUMMARY_DUMP)
            #No data collection is required.
            ;;
        *) # unknown option
            log_error "Skipping: Unknown dump type: $dump_type"
            ;;
    esac
}

# @brief set pid by reading information from the optional path.
#        dreport "core" type user provides core file as optional path parameter.
#        As per coredump source code systemd-coredump uses below format
#        https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
#        /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “.
#        <process ID>.%s000000"
function set_core_pid()
{
    #Escape bash characters in file name
    file=$(printf %q "$optional_path")

    #matching systemd-coredump core file format.
    pid=$(echo $file | awk -F . '{ print $5}')
}

# @brief set elog pid by reading _PID information from the elog d-bus object.
#        _PID information is stored  elog Additional data field
#        Data format  "_PID=<pid>"
function set_elog_pid()
{
    additional_data=$(busctl get-property xyz.openbmc_project.Logging \
                             $optional_path \
                             xyz.openbmc_project.Logging.Entry \
                             AdditionalData)

    #read _PID data.
    if [ ! -z "$additional_data" ]; then
        pid=$(echo $additional_data | \
                   awk -F _PID= '{ print ($2+0)}')
    fi
}

# @brief Capture debug data based on the input command array.
#        and stores in to global temporary name specific location.
# @param $1 Source array
# @return 0 on success, error code otherwise
function capture_data()
{
    source=("$@")

    for ((i=0;i<${#source[@]};i+=1)); do
        ${source[i]}
    done

    return 0
}

# @brief Calculate file or directory compressed size based on input
#        and check whether the size in the the allowed size limit.
#        Remove the file or directory from the name_dir
#        if the check fails.
# @param $1 Source file or directory
# @return 0 on success, error code if size exceeds the limit.
# Limitation: compress and tar will have few bytes size difference
function check_size()
{
    source=$1

    #No size check required incase dump_size is set to unlimited
    if [ $dump_size = $UNLIMITED ]; then
        return 0
    fi

    #get the file or directory size
    if [[ -d $source ]] && [[ -n $source ]]; then
        tar -cf "$source.tar" -C \
                 $(dirname "$source") $(basename "$source")
        size=$(stat -c%s "$source.tar")
        rm "$source.tar"
    else
        size=$(stat -c%s "$source")
    fi

    if [ $((size + cur_dump_size)) -gt $dump_size ]; then
        #Exceed the allowed limit,
        #tar and compress the files and check the size
        tar -Jcf "$name_dir.tar.xz" -C \
                  $(dirname "$name_dir") $(basename "$name_dir")
        size=$(stat -c%s "$name_dir.tar.xz")
        if [ $size -gt $dump_size ]; then
            #Remove the the specific data from the name_dir and contniue
            rm "$source" "$name_dir.tar.xz"
            return $RESOURCE_UNAVAILABLE
        else
            rm "$name_dir.tar.xz"
        fi
    fi

    #Remove the compressed file from the name directory
    cur_dump_size=$((size + cur_dump_size))
    return $SUCCESS
}

# @brief Initial version of the summary log
init_summary()
{
   log_summary "Name:          $name.tar.xz"
   log_summary "Epochtime:     $EPOCHTIME"
   log_summary "ID:            $dump_id"
   log_summary "Type:          $dump_type"
   log_summary "Optional path: $optional_path"
}

# @brief Check the validity of user inputs and initialize global
#        variables. Create directory for temporary data collection
# @return 0 on success, error code otherwise

function initialize()
{
    #Dump file name
    if [ -z $name ]; then
        name=$"obmcdump_"$dump_id"_$EPOCHTIME"
    fi

    #Create temporary data directory.
    mkdir -p "$TMP_DIR/$name"
    if [ $? -ne 0 ]; then
        echo "Error: Failed to create the temporary directory."
        return $RESOURCE_UNAVAILABLE;
    fi

    #name directory
    name_dir="$TMP_DIR/$name"

    #dreport log file
    dreport_log="$name_dir/$DREPORT_LOG"

    #summary log file
    summary_log="$name_dir/$SUMMARY_LOG"

    #Type
    if [[ !($dump_type = $TYPE_USER || \
            $dump_type = $TYPE_CORE || \
            $dump_type = $TYPE_ELOG) ]]; then
       log_error "Invalid -type, Only summary log is available"
       dump_type=$SUMMARY_DUMP
    fi

    #Size
    #Check the input is integer.
    if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then
       #Converts in to bytes.
       dump_size="$((dump_size * 1024))"
    else
       dump_size=$UNLIMITED
    fi

    return $SUCCESS
}

# @brief Packaging the dump and transferring to dump location.
function package()
{
    mkdir -p "$dump_dir"
    if [ $? -ne 0 ]; then
        log_error "Could not create the destination directory $dump_dir"
        dest_dir=$TMP_DIR
    fi

    #tar and compress the files.
    tar -Jcf "$name_dir.tar.xz" -C \
             $(dirname "$name_dir") $(basename "$name_dir")

    if [ $? -ne 0 ]; then
        echo $($TIME_STAMP) "Could not create the compressed tar file"
        rm -r "$name_dir"
        return $INTERNAL_FAILURE
    fi

    #remove the temporary name specific directory
    rm -r "$name_dir"

    echo $($TIME_STAMP) "Report is available in $dump_dir"

    if [ "$TMP_DIR" == "$dump_dir" ]; then
       return $SUCCESS
    fi

    #copy the compressed tar file into the destination
    cp "$name_dir.tar.xz" "$dump_dir"
    if [ $? -ne 0 ]; then
        echo "Failed to copy the $name_dir.tar.xz to $dump_dir"
        rm "$name_dir.tar.xz"
        return $INTERNAL_FAILURE
    fi

    #Remove the temporary copy of the file
    rm "$name_dir.tar.xz"
}

# @brief log the error message
# @param error message
function log_error()
{
   echo $($TIME_STAMP) "ERROR: $@" >> $dreport_log
   if ((quiet != TRUE)); then
      echo $($TIME_STAMP) "ERROR: $@" >&2
   fi
}

# @brief log warning message
# @param warning message
function log_warning()
{
    if ((verbose == TRUE)); then
        echo $($TIME_STAMP) "WARNING: $@" >> $dreport_log
        if ((quiet != TRUE)); then
            echo $($TIME_STAMP) "WARNING: $@" >&2
        fi
    fi
}

# @brief log info message
# @param info message
function log_info()
{
    if ((verbose == TRUE)); then
        echo $($TIME_STAMP) "INFO: $@" >> $dreport_log
        if ((quiet != TRUE)); then
            echo $($TIME_STAMP) "INFO: $@" >&1
        fi
    fi
}

# @brief log summary message
# @param message
function log_summary()
{
    echo $($TIME_STAMP) "$@" >> $summary_log
    if ((quiet != TRUE)); then
        echo $($TIME_STAMP) "$@" >&1
    fi
}

# @brief Main function
function main()
{
    #initialize the global variables and
    #create temporary storage locations
    initialize
    result=$?
    if [[ ${result} -ne $SUCCESS ]]; then
        echo $($TIME_STAMP) "Error: Failed to initialize, Exiting"
        exit;
    fi

    #Initilize the summary log
    init_summary

    #collect data based on the type.
    collect_data

    package  #package the dump
    result=$?
    if [[ ${result} -ne $SUCCESS ]]; then
        echo $($TIME_STAMP) "Error: Failed to package, Exiting"
    else
        echo $($TIME_STAMP) "Sucessfully completed"
        exit;
    fi
}

TEMP=`getopt -o n:d:i:t:s:p:vVqh \
      --long name:,dir:,dumpid:,type:,size:,path:,verbose,version,quiet,help \
      -- "$@"`

if [ $? -ne 0 ]
then
    echo "Error: Invalid options"
    exit 1
fi

eval set -- "$TEMP"

while [[ $# -gt 1 ]]; do
    key="$1"
    case $key in
        -n|--name)
            name=$2
            shift 2;;
        -d|--dir)
            dump_dir=$2
            shift 2;;
        -i|--dumpid)
            dump_id=$2
            shift 2;;
        -t|--type)
            dump_type=$2
            shift 2;;
        -s|--size)
            dump_size=$2
            shift 2;;
        -p|--path)
            optional_path=$2
            shift 2;;
        -v|—-verbose)
            verbose=$TRUE
            shift;;
        -V|--version)
            shift;;
        -q|—-quiet)
            quiet=$TRUE
            shift;;
        -h|--help)
            echo "$help"
            exit;;
        *) # unknown option
            log_error "Unknown argument: $1"
            log_info "$help"
            exit 1;;
    esac
done

main #main program
exit $?
OpenPOWER on IntegriCloud