xref: /openbmc/phosphor-debug-collector/tools/dreport.d/dreport (revision 971d1a747be7ffb0444bf1821fa345af5989fac1)
15ce15500SJayanth Othayoth#! /bin/bash
25ce15500SJayanth Othayoth
39d26e4faSPatrick Williamshelp=$(cat << EOF
45ce15500SJayanth Othayoth        dreport creates an archive(xz compressed) consisting of the following:
55ce15500SJayanth Othayoth                * Configuration information
65ce15500SJayanth Othayoth                * Debug information
75ce15500SJayanth Othayoth                * A summary report
85ce15500SJayanth Othayoth        The type parameter controls the content of the data. The generated
95ce15500SJayanth Othayoth        archive is stored in the user specified location.
105ce15500SJayanth Othayoth
115ce15500SJayanth Othayothusage: dreport [OPTION]
125ce15500SJayanth Othayoth
135ce15500SJayanth OthayothOptions:
145ce15500SJayanth Othayoth        -n, —-name <name>     Name to be used for the archive.
155ce15500SJayanth Othayoth                              Default name format obmcdump_<id>_<epochtime>
165ce15500SJayanth Othayoth        -d, —-dir <directory> Archive directory to copy the compressed report.
175ce15500SJayanth Othayoth                              Default output directory is /tmp
185ce15500SJayanth Othayoth        -i, —-id <id>         Dump identifier to associate with the archive.
195ce15500SJayanth Othayoth                              Identifiers include numeric characters.
205ce15500SJayanth Othayoth                              Default dump identifier is 0
215ce15500SJayanth Othayoth        -t, —-type <type>     Data collection type. Valid types are
225ce15500SJayanth Othayoth                              "user", "core", "elog".
235ce15500SJayanth Othayoth                              Default type is "user" initiated.
245ce15500SJayanth Othayoth        -p, —-path <path>     Optional contents to be included in the archive.
255ce15500SJayanth Othayoth                              Valid paths are absolute file path or d-bus path
265ce15500SJayanth Othayoth                              based on type parameter.
275ce15500SJayanth Othayoth                                 -Absolute file path for "core" type.
285ce15500SJayanth Othayoth                                 -elog d-bus object for "elog" type.
295ce15500SJayanth Othayoth        -s, --size <size>     Maximum allowed size(in KB) of the archive.
305ce15500SJayanth Othayoth                              Report will be truncated in case size exceeds
315ce15500SJayanth Othayoth                              this limit. Default size is unlimited.
325ce15500SJayanth Othayoth        -v, —-verbose         Increase logging verbosity.
335ce15500SJayanth Othayoth        -q, —-quiet           Only log fatal errors to stderr
345ce15500SJayanth Othayoth        -h, —-help            Display this help and exit.
359d26e4faSPatrick WilliamsEOF
369d26e4faSPatrick Williams)
375ce15500SJayanth Othayoth
385ce15500SJayanth Othayoth#CONSTANTS
395ce15500SJayanth Othayothdeclare -rx TRUE=1
405ce15500SJayanth Othayothdeclare -rx FALSE=0
415ce15500SJayanth Othayothdeclare -rx UNLIMITED="unlimited"
425ce15500SJayanth Othayothdeclare -rx SUMMARY_DUMP="summary"
435ce15500SJayanth Othayothdeclare -rx TYPE_USER="user"
445ce15500SJayanth Othayothdeclare -rx TYPE_CORE="core"
455ce15500SJayanth Othayothdeclare -rx TYPE_ELOG="elog"
460deb287cSMarri Devender Raodeclare -rx TYPE_CHECKSTOP="checkstop"
47ff92ffe2SGeorge Liudeclare -rx TYPE_RAMOOPS="ramoops"
485ce15500SJayanth Othayothdeclare -rx SUMMARY_LOG="summary.log"
495ce15500SJayanth Othayothdeclare -rx DREPORT_LOG="dreport.log"
505ce15500SJayanth Othayothdeclare -rx TMP_DIR="/tmp"
515ce15500SJayanth Othayothdeclare -rx EPOCHTIME=$(date +"%s")
525ce15500SJayanth Othayothdeclare -rx TIME_STAMP="date -u"
535ce15500SJayanth Othayothdeclare -rx PLUGIN="pl_"
545ce15500SJayanth Othayothdeclare -rx DREPORT_SOURCE="/usr/share/dreport.d"
555ce15500SJayanth Othayothdeclare -rx DREPORT_INCLUDE="$DREPORT_SOURCE/include.d"
56aa146d62SJayanth Othayothdeclare -rx ZERO="0"
57aa146d62SJayanth Othayothdeclare -rx JOURNAL_LINE_LIMIT="500"
58c237e3d8SChirag Sharmadeclare -rx HEADER_EXTENSION="$DREPORT_INCLUDE/gendumpheader"
595ce15500SJayanth Othayoth
605ce15500SJayanth Othayoth#Error Codes
615ce15500SJayanth Othayothdeclare -rx SUCCESS="0"
625ce15500SJayanth Othayothdeclare -rx INTERNAL_FAILURE="1"
635ce15500SJayanth Othayothdeclare -rx RESOURCE_UNAVAILABLE="2"
645ce15500SJayanth Othayoth
655ce15500SJayanth Othayoth#VARIABLES
665ce15500SJayanth Othayothdeclare -x name=""
675ce15500SJayanth Othayothdeclare -x dump_dir="/tmp"
685ce15500SJayanth Othayothdeclare -x dump_id="00000000"
695ce15500SJayanth Othayothdeclare -x dump_type=$TYPE_USER
705ce15500SJayanth Othayothdeclare -x verbose=$FALSE
715ce15500SJayanth Othayothdeclare -x quiet=$FALSE
725ce15500SJayanth Othayothdeclare -x dump_size="unlimited"
735ce15500SJayanth Othayothdeclare -x name_dir=""
745ce15500SJayanth Othayothdeclare -x optional_path=""
755ce15500SJayanth Othayothdeclare -x dreport_log=""
765ce15500SJayanth Othayothdeclare -x summary_log=""
775ce15500SJayanth Othayothdeclare -x cur_dump_size=0
78aa146d62SJayanth Othayothdeclare -x pid=$ZERO
795ce15500SJayanth Othayothdeclare -x elog_id=""
805ce15500SJayanth Othayoth
815ce15500SJayanth Othayoth#Source dreport common functions
825ce15500SJayanth Othayoth. $DREPORT_INCLUDE/functions
835ce15500SJayanth Othayoth
845ce15500SJayanth Othayoth# @brief Initiate data collection based on the type.
855ce15500SJayanth Othayoth# @return 0 on success, error code otherwise
865ce15500SJayanth Othayothfunction collect_data()
875ce15500SJayanth Othayoth{
885ce15500SJayanth Othayoth    case $dump_type in
895ce15500SJayanth Othayoth        $TYPE_USER)
905ce15500SJayanth Othayoth            ;;
915ce15500SJayanth Othayoth        $TYPE_CORE)
925ce15500SJayanth Othayoth            log_summary "Core: $optional_path"
935ce15500SJayanth Othayoth            set_core_pid
945ce15500SJayanth Othayoth            ;;
95ff92ffe2SGeorge Liu        $TYPE_RAMOOPS)
96ff92ffe2SGeorge Liu            log_summary "Ramoops: $optional_path"
97ff92ffe2SGeorge Liu            ;;
985ce15500SJayanth Othayoth        $TYPE_ELOG)
995ce15500SJayanth Othayoth            log_summary "ELOG: $optional_path"
1005ce15500SJayanth Othayoth            elog_id=$(basename "$optional_path")
1015ce15500SJayanth Othayoth            set_elog_pid
1025ce15500SJayanth Othayoth            ;;
1030deb287cSMarri Devender Rao        $TYPE_CHECKSTOP)
1040deb287cSMarri Devender Rao            log_summary "CHECKSTOP: $optional_path"
1050deb287cSMarri Devender Rao            elog_id=$(basename "$optional_path")
1060deb287cSMarri Devender Rao            set_elog_pid
1070deb287cSMarri Devender Rao            ;;
1085ce15500SJayanth Othayoth
1095ce15500SJayanth Othayoth        $SUMMARY_DUMP)
1105ce15500SJayanth Othayoth            #No data collection is required.
1115ce15500SJayanth Othayoth            return
1125ce15500SJayanth Othayoth            ;;
1135ce15500SJayanth Othayoth        *) # unknown option
1145ce15500SJayanth Othayoth            log_error "Skipping: Unknown dump type: $dump_type"
1155ce15500SJayanth Othayoth            return
1165ce15500SJayanth Othayoth            ;;
1175ce15500SJayanth Othayoth    esac
1185ce15500SJayanth Othayoth
1195ce15500SJayanth Othayoth    plugin_path=$DREPORT_SOURCE/$PLUGIN$dump_type.d
1205ce15500SJayanth Othayoth
1215ce15500SJayanth Othayoth    # check plugin directory for this dump type?
1225ce15500SJayanth Othayoth    if [ ! -d $plugin_path ]; then
1235ce15500SJayanth Othayoth        log_error "$plugin_path does not exist, skipping dump collection"
1245ce15500SJayanth Othayoth        return 0
1255ce15500SJayanth Othayoth    fi
1265ce15500SJayanth Othayoth
1275ce15500SJayanth Othayoth    #Executes plugins based on the type.
1285ce15500SJayanth Othayoth    for i in $plugin_path/* ; do
1295ce15500SJayanth Othayoth        $i
1305ce15500SJayanth Othayoth    done
1315ce15500SJayanth Othayoth}
1325ce15500SJayanth Othayoth
1335ce15500SJayanth Othayoth# @brief set pid by reading information from the optional path.
1345ce15500SJayanth Othayoth#        dreport "core" type user provides core file as optional path parameter.
1355ce15500SJayanth Othayoth#        As per coredump source code systemd-coredump uses below format
1365ce15500SJayanth Othayoth#        https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
1375ce15500SJayanth Othayoth#        /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “.
1385ce15500SJayanth Othayoth#        <process ID>.%s000000"
1395ce15500SJayanth Othayothfunction set_core_pid()
1405ce15500SJayanth Othayoth{
1415ce15500SJayanth Othayoth    #Escape bash characters in file name
1425ce15500SJayanth Othayoth    file=$(printf %q "$optional_path")
1435ce15500SJayanth Othayoth
1445ce15500SJayanth Othayoth    #matching systemd-coredump core file format.
1455ce15500SJayanth Othayoth    pid=$(echo $file | awk -F . '{ print $5}')
1465ce15500SJayanth Othayoth}
1475ce15500SJayanth Othayoth
1485ce15500SJayanth Othayoth# @brief set elog pid by reading _PID information from the elog d-bus object.
1495ce15500SJayanth Othayoth#        _PID information is stored  elog Additional data field
1505ce15500SJayanth Othayoth#        Data format  "_PID=<pid>"
1515ce15500SJayanth Othayothfunction set_elog_pid()
1525ce15500SJayanth Othayoth{
1535ce15500SJayanth Othayoth    additional_data=$(busctl get-property xyz.openbmc_project.Logging \
1545ce15500SJayanth Othayoth            $optional_path \
1555ce15500SJayanth Othayoth            xyz.openbmc_project.Logging.Entry \
1565ce15500SJayanth Othayoth        AdditionalData)
1575ce15500SJayanth Othayoth
1585ce15500SJayanth Othayoth    #read _PID data.
1595ce15500SJayanth Othayoth    if [ ! -z "$additional_data" ]; then
1605ce15500SJayanth Othayoth        pid=$(echo $additional_data | \
1615ce15500SJayanth Othayoth            awk -F _PID= '{ print ($2+0)}')
1625ce15500SJayanth Othayoth    fi
1635ce15500SJayanth Othayoth}
1645ce15500SJayanth Othayoth
1655ce15500SJayanth Othayoth# @brief Initial version of the summary log
1669d26e4faSPatrick Williamsfunction init_summary()
1675ce15500SJayanth Othayoth{
1685ce15500SJayanth Othayoth    log_summary "Name:          $name.tar.xz"
1695ce15500SJayanth Othayoth    log_summary "Epochtime:     $EPOCHTIME"
1705ce15500SJayanth Othayoth    log_summary "ID:            $dump_id"
1715ce15500SJayanth Othayoth    log_summary "Type:          $dump_type"
1725ce15500SJayanth Othayoth}
1735ce15500SJayanth Othayoth
1745ce15500SJayanth Othayoth# @brief Check the validity of user inputs and initialize global
1755ce15500SJayanth Othayoth#        variables. Create directory for temporary data collection
1765ce15500SJayanth Othayoth# @return 0 on success, error code otherwise
1775ce15500SJayanth Othayoth
1785ce15500SJayanth Othayothfunction initialize()
1795ce15500SJayanth Othayoth{
1805ce15500SJayanth Othayoth    #Dump file name
1815ce15500SJayanth Othayoth    if [ -z $name ]; then
1825ce15500SJayanth Othayoth        name=$"obmcdump_"$dump_id"_$EPOCHTIME"
1835ce15500SJayanth Othayoth    fi
1845ce15500SJayanth Othayoth
1855ce15500SJayanth Othayoth    #Create temporary data directory.
1865ce15500SJayanth Othayoth    mkdir -p "$TMP_DIR/$name"
1875ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
1885ce15500SJayanth Othayoth        echo "Error: Failed to create the temporary directory."
1895ce15500SJayanth Othayoth        return $RESOURCE_UNAVAILABLE;
1905ce15500SJayanth Othayoth    fi
1915ce15500SJayanth Othayoth
1925ce15500SJayanth Othayoth    #name directory
1935ce15500SJayanth Othayoth    name_dir="$TMP_DIR/$name"
1945ce15500SJayanth Othayoth
1955ce15500SJayanth Othayoth    #dreport log file
1965ce15500SJayanth Othayoth    dreport_log="$name_dir/$DREPORT_LOG"
1975ce15500SJayanth Othayoth
1985ce15500SJayanth Othayoth    #summary log file
1995ce15500SJayanth Othayoth    summary_log="$name_dir/$SUMMARY_LOG"
2005ce15500SJayanth Othayoth
2015ce15500SJayanth Othayoth    #Type
20240d67193SJayanth Othayoth    if ! { [[ $dump_type = "$TYPE_USER" ]] || \
20340d67193SJayanth Othayoth            [[ $dump_type = "$TYPE_CORE" ]] || \
20440d67193SJayanth Othayoth            [[ $dump_type = "$TYPE_ELOG" ]] || \
20540d67193SJayanth Othayoth            [[ $dump_type = "$TYPE_RAMOOPS" ]] || \
20640d67193SJayanth Othayoth            [[ $dump_type = "$TYPE_CHECKSTOP" ]]; }; then
2075ce15500SJayanth Othayoth        log_error "Invalid -type, Only summary log is available"
2085ce15500SJayanth Othayoth        dump_type=$SUMMARY_DUMP
2095ce15500SJayanth Othayoth    fi
2105ce15500SJayanth Othayoth
2115ce15500SJayanth Othayoth    #Size
2125ce15500SJayanth Othayoth    #Check the input is integer.
2135ce15500SJayanth Othayoth    if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then
2145ce15500SJayanth Othayoth        #Converts in to bytes.
2155ce15500SJayanth Othayoth        dump_size="$((dump_size * 1024))"
2165ce15500SJayanth Othayoth    else
2175ce15500SJayanth Othayoth        dump_size=$UNLIMITED
2185ce15500SJayanth Othayoth    fi
2195ce15500SJayanth Othayoth
2205ce15500SJayanth Othayoth    return $SUCCESS
2215ce15500SJayanth Othayoth}
2225ce15500SJayanth Othayoth
2235ce15500SJayanth Othayoth# @brief Packaging the dump and transferring to dump location.
2245ce15500SJayanth Othayothfunction package()
2255ce15500SJayanth Othayoth{
2265ce15500SJayanth Othayoth    mkdir -p "$dump_dir"
2275ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
2285ce15500SJayanth Othayoth        log_error "Could not create the destination directory $dump_dir"
2295ce15500SJayanth Othayoth        dest_dir=$TMP_DIR
2305ce15500SJayanth Othayoth    fi
2315ce15500SJayanth Othayoth
2325ce15500SJayanth Othayoth    #tar and compress the files.
233c237e3d8SChirag Sharma    if [ -f "$HEADER_EXTENSION" ]; then
2345ce15500SJayanth Othayoth        tar -Jcf "$name_dir.tar.xz" -C \
2355ce15500SJayanth Othayoth            $(dirname "$name_dir") $(basename "$name_dir")
236c237e3d8SChirag Sharma        echo "Adding Dump Header :"$HEADER_EXTENSION
237c237e3d8SChirag Sharma        ("$HEADER_EXTENSION")
238c237e3d8SChirag Sharma        cat "$name_dir.tar.xz" | tee -a "/tmp/dumpheader_$EPOCHTIME" > /dev/null
239c237e3d8SChirag Sharma        mv "/tmp/dumpheader_$EPOCHTIME" "$name_dir.tar.xz"
240c237e3d8SChirag Sharma    else
241c237e3d8SChirag Sharma        tar -Jcf "$name_dir.tar.xz" -C \
242c237e3d8SChirag Sharma            $(dirname "$name_dir") $(basename "$name_dir")
243c237e3d8SChirag Sharma    fi
2445ce15500SJayanth Othayoth
2455ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
2465ce15500SJayanth Othayoth        echo $($TIME_STAMP) "Could not create the compressed tar file"
2475ce15500SJayanth Othayoth        rm -r "$name_dir"
2485ce15500SJayanth Othayoth        return $INTERNAL_FAILURE
2495ce15500SJayanth Othayoth    fi
2505ce15500SJayanth Othayoth
2515ce15500SJayanth Othayoth    #remove the temporary name specific directory
2525ce15500SJayanth Othayoth    rm -r "$name_dir"
2535ce15500SJayanth Othayoth
2545ce15500SJayanth Othayoth    echo $($TIME_STAMP) "Report is available in $dump_dir"
2555ce15500SJayanth Othayoth
256a63f4a6fSChirag Sharma    if [ "$TMP_DIR" == "$dump_dir" ] || [ "$TMP_DIR/" == "$dump_dir" ]; then
2575ce15500SJayanth Othayoth        return $SUCCESS
2585ce15500SJayanth Othayoth    fi
2595ce15500SJayanth Othayoth
2605ce15500SJayanth Othayoth    #copy the compressed tar file into the destination
2615ce15500SJayanth Othayoth    cp "$name_dir.tar.xz" "$dump_dir"
2625ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
2635ce15500SJayanth Othayoth        echo "Failed to copy the $name_dir.tar.xz to $dump_dir"
2645ce15500SJayanth Othayoth        rm "$name_dir.tar.xz"
2655ce15500SJayanth Othayoth        return $INTERNAL_FAILURE
2665ce15500SJayanth Othayoth    fi
2675ce15500SJayanth Othayoth
2685ce15500SJayanth Othayoth    #Remove the temporary copy of the file
2695ce15500SJayanth Othayoth    rm "$name_dir.tar.xz"
2705ce15500SJayanth Othayoth}
2715ce15500SJayanth Othayoth
2725ce15500SJayanth Othayoth# @brief Main function
2735ce15500SJayanth Othayothfunction main()
2745ce15500SJayanth Othayoth{
2755ce15500SJayanth Othayoth    #initialize the global variables and
2765ce15500SJayanth Othayoth    #create temporary storage locations
2775ce15500SJayanth Othayoth    initialize
2785ce15500SJayanth Othayoth    result=$?
2795ce15500SJayanth Othayoth    if [[ ${result} -ne $SUCCESS ]]; then
2805ce15500SJayanth Othayoth        echo $($TIME_STAMP) "Error: Failed to initialize, Exiting"
2815ce15500SJayanth Othayoth        exit;
2825ce15500SJayanth Othayoth    fi
2835ce15500SJayanth Othayoth
28495a72983SGunnar Mills    #Initialize the summary log
2855ce15500SJayanth Othayoth    init_summary
2865ce15500SJayanth Othayoth
2875ce15500SJayanth Othayoth    #collect data based on the type.
2885ce15500SJayanth Othayoth    collect_data
2895ce15500SJayanth Othayoth
2905ce15500SJayanth Othayoth    package  #package the dump
2915ce15500SJayanth Othayoth    result=$?
2925ce15500SJayanth Othayoth    if [[ ${result} -ne $SUCCESS ]]; then
2935ce15500SJayanth Othayoth        echo $($TIME_STAMP) "Error: Failed to package, Exiting"
2945ce15500SJayanth Othayoth    else
29595a72983SGunnar Mills        echo $($TIME_STAMP) "Successfully completed"
2965ce15500SJayanth Othayoth        exit;
2975ce15500SJayanth Othayoth    fi
2985ce15500SJayanth Othayoth}
2995ce15500SJayanth Othayoth
300*971d1a74SMatt FischerTEMP=`getopt -o n:d:i:t:s:p:vqh \
301*971d1a74SMatt Fischer    --long name:,dir:,dumpid:,type:,size:,path:,verbose,quiet,help \
3025ce15500SJayanth Othayoth    -- "$@"`
3035ce15500SJayanth Othayoth
3045ce15500SJayanth Othayothif [ $? -ne 0 ]
3055ce15500SJayanth Othayoththen
3065ce15500SJayanth Othayoth    echo "Error: Invalid options"
3075ce15500SJayanth Othayoth    exit 1
3085ce15500SJayanth Othayothfi
3095ce15500SJayanth Othayoth
3105ce15500SJayanth Othayotheval set -- "$TEMP"
3115ce15500SJayanth Othayoth
3125ce15500SJayanth Othayothwhile [[ $# -gt 1 ]]; do
3135ce15500SJayanth Othayoth    key="$1"
3145ce15500SJayanth Othayoth    case $key in
3155ce15500SJayanth Othayoth        -n|--name)
3165ce15500SJayanth Othayoth            name=$2
3175ce15500SJayanth Othayoth            shift 2 ;;
3185ce15500SJayanth Othayoth        -d|--dir)
3195ce15500SJayanth Othayoth            dump_dir=$2
3205ce15500SJayanth Othayoth            shift 2 ;;
3215ce15500SJayanth Othayoth        -i|--dumpid)
3225ce15500SJayanth Othayoth            dump_id=$2
3235ce15500SJayanth Othayoth            shift 2 ;;
3245ce15500SJayanth Othayoth        -t|--type)
3255ce15500SJayanth Othayoth            dump_type=$2
3265ce15500SJayanth Othayoth            shift 2 ;;
3275ce15500SJayanth Othayoth        -s|--size)
3285ce15500SJayanth Othayoth            dump_size=$2
3295ce15500SJayanth Othayoth            shift 2 ;;
3305ce15500SJayanth Othayoth        -p|--path)
3315ce15500SJayanth Othayoth            optional_path=$2
3325ce15500SJayanth Othayoth            shift 2 ;;
333*971d1a74SMatt Fischer        -v|--verbose)
3345ce15500SJayanth Othayoth            verbose=$TRUE
3355ce15500SJayanth Othayoth            shift ;;
336*971d1a74SMatt Fischer        -q|--quiet)
3375ce15500SJayanth Othayoth            quiet=$TRUE
3385ce15500SJayanth Othayoth            shift ;;
3395ce15500SJayanth Othayoth        -h|--help)
3405ce15500SJayanth Othayoth            echo "$help"
3415ce15500SJayanth Othayoth            exit ;;
3425ce15500SJayanth Othayoth        *) # unknown option
3435ce15500SJayanth Othayoth            log_error "Unknown argument: $1"
3445ce15500SJayanth Othayoth            log_info "$help"
3455ce15500SJayanth Othayoth            exit 1 ;;
3465ce15500SJayanth Othayoth    esac
3475ce15500SJayanth Othayothdone
3485ce15500SJayanth Othayoth
3495ce15500SJayanth Othayothmain #main program
3505ce15500SJayanth Othayothexit $?
351