xref: /openbmc/phosphor-debug-collector/tools/dreport.d/dreport (revision c237e3d8fec57d6cb57548f27b173ddbf77ffb34)
15ce15500SJayanth Othayoth#! /bin/bash
25ce15500SJayanth Othayoth
35ce15500SJayanth Othayothhelp=$"
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        -V, --version         Output version information.
345ce15500SJayanth Othayoth        -q, —-quiet           Only log fatal errors to stderr
355ce15500SJayanth Othayoth        -h, —-help            Display this help and exit.
365ce15500SJayanth Othayoth"
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"
475ce15500SJayanth Othayothdeclare -rx SUMMARY_LOG="summary.log"
485ce15500SJayanth Othayothdeclare -rx DREPORT_LOG="dreport.log"
495ce15500SJayanth Othayothdeclare -rx TMP_DIR="/tmp"
505ce15500SJayanth Othayothdeclare -rx EPOCHTIME=$(date +"%s")
515ce15500SJayanth Othayothdeclare -rx TIME_STAMP="date -u"
525ce15500SJayanth Othayothdeclare -rx PLUGIN="pl_"
535ce15500SJayanth Othayothdeclare -rx DREPORT_SOURCE="/usr/share/dreport.d"
545ce15500SJayanth Othayothdeclare -rx DREPORT_INCLUDE="$DREPORT_SOURCE/include.d"
55aa146d62SJayanth Othayothdeclare -rx ZERO="0"
56aa146d62SJayanth Othayothdeclare -rx JOURNAL_LINE_LIMIT="500"
57*c237e3d8SChirag Sharmadeclare -rx HEADER_EXTENSION="$DREPORT_INCLUDE/gendumpheader"
585ce15500SJayanth Othayoth
595ce15500SJayanth Othayoth#Error Codes
605ce15500SJayanth Othayothdeclare -rx SUCCESS="0"
615ce15500SJayanth Othayothdeclare -rx INTERNAL_FAILURE="1"
625ce15500SJayanth Othayothdeclare -rx RESOURCE_UNAVAILABLE="2"
635ce15500SJayanth Othayoth
645ce15500SJayanth Othayoth#VARIABLES
655ce15500SJayanth Othayothdeclare -x name=""
665ce15500SJayanth Othayothdeclare -x dump_dir="/tmp"
675ce15500SJayanth Othayothdeclare -x dump_id="00000000"
685ce15500SJayanth Othayothdeclare -x dump_type=$TYPE_USER
695ce15500SJayanth Othayothdeclare -x verbose=$FALSE
705ce15500SJayanth Othayothdeclare -x quiet=$FALSE
715ce15500SJayanth Othayothdeclare -x dump_size="unlimited"
725ce15500SJayanth Othayothdeclare -x name_dir=""
735ce15500SJayanth Othayothdeclare -x optional_path=""
745ce15500SJayanth Othayothdeclare -x dreport_log=""
755ce15500SJayanth Othayothdeclare -x summary_log=""
765ce15500SJayanth Othayothdeclare -x cur_dump_size=0
77aa146d62SJayanth Othayothdeclare -x pid=$ZERO
785ce15500SJayanth Othayothdeclare -x elog_id=""
795ce15500SJayanth Othayoth
805ce15500SJayanth Othayoth#Source dreport common functions
815ce15500SJayanth Othayoth. $DREPORT_INCLUDE/functions
825ce15500SJayanth Othayoth
835ce15500SJayanth Othayoth# @brief Initiate data collection based on the type.
845ce15500SJayanth Othayoth# @return 0 on success, error code otherwise
855ce15500SJayanth Othayothfunction collect_data()
865ce15500SJayanth Othayoth{
875ce15500SJayanth Othayoth    case $dump_type in
885ce15500SJayanth Othayoth        $TYPE_USER)
895ce15500SJayanth Othayoth            ;;
905ce15500SJayanth Othayoth        $TYPE_CORE)
915ce15500SJayanth Othayoth            log_summary "Core: $optional_path"
925ce15500SJayanth Othayoth            set_core_pid
935ce15500SJayanth Othayoth            ;;
945ce15500SJayanth Othayoth        $TYPE_ELOG)
955ce15500SJayanth Othayoth            log_summary "ELOG: $optional_path"
965ce15500SJayanth Othayoth            elog_id=$(basename "$optional_path")
975ce15500SJayanth Othayoth            set_elog_pid
985ce15500SJayanth Othayoth            ;;
990deb287cSMarri Devender Rao        $TYPE_CHECKSTOP)
1000deb287cSMarri Devender Rao            log_summary "CHECKSTOP: $optional_path"
1010deb287cSMarri Devender Rao            elog_id=$(basename "$optional_path")
1020deb287cSMarri Devender Rao            set_elog_pid
1030deb287cSMarri Devender Rao            ;;
1045ce15500SJayanth Othayoth
1055ce15500SJayanth Othayoth        $SUMMARY_DUMP)
1065ce15500SJayanth Othayoth            #No data collection is required.
1075ce15500SJayanth Othayoth            return
1085ce15500SJayanth Othayoth            ;;
1095ce15500SJayanth Othayoth        *) # unknown option
1105ce15500SJayanth Othayoth            log_error "Skipping: Unknown dump type: $dump_type"
1115ce15500SJayanth Othayoth            return
1125ce15500SJayanth Othayoth            ;;
1135ce15500SJayanth Othayoth    esac
1145ce15500SJayanth Othayoth
1155ce15500SJayanth Othayoth    plugin_path=$DREPORT_SOURCE/$PLUGIN$dump_type.d
1165ce15500SJayanth Othayoth
1175ce15500SJayanth Othayoth    # check plugin directory for this dump type?
1185ce15500SJayanth Othayoth    if [ ! -d $plugin_path ]; then
1195ce15500SJayanth Othayoth        log_error "$plugin_path does not exist, skipping dump collection"
1205ce15500SJayanth Othayoth        return 0
1215ce15500SJayanth Othayoth    fi
1225ce15500SJayanth Othayoth
1235ce15500SJayanth Othayoth    #Executes plugins based on the type.
1245ce15500SJayanth Othayoth    for i in $plugin_path/* ; do
1255ce15500SJayanth Othayoth       $i
1265ce15500SJayanth Othayoth    done
1275ce15500SJayanth Othayoth}
1285ce15500SJayanth Othayoth
1295ce15500SJayanth Othayoth# @brief set pid by reading information from the optional path.
1305ce15500SJayanth Othayoth#        dreport "core" type user provides core file as optional path parameter.
1315ce15500SJayanth Othayoth#        As per coredump source code systemd-coredump uses below format
1325ce15500SJayanth Othayoth#        https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
1335ce15500SJayanth Othayoth#        /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “.
1345ce15500SJayanth Othayoth#        <process ID>.%s000000"
1355ce15500SJayanth Othayothfunction set_core_pid()
1365ce15500SJayanth Othayoth{
1375ce15500SJayanth Othayoth    #Escape bash characters in file name
1385ce15500SJayanth Othayoth    file=$(printf %q "$optional_path")
1395ce15500SJayanth Othayoth
1405ce15500SJayanth Othayoth    #matching systemd-coredump core file format.
1415ce15500SJayanth Othayoth    pid=$(echo $file | awk -F . '{ print $5}')
1425ce15500SJayanth Othayoth}
1435ce15500SJayanth Othayoth
1445ce15500SJayanth Othayoth# @brief set elog pid by reading _PID information from the elog d-bus object.
1455ce15500SJayanth Othayoth#        _PID information is stored  elog Additional data field
1465ce15500SJayanth Othayoth#        Data format  "_PID=<pid>"
1475ce15500SJayanth Othayothfunction set_elog_pid()
1485ce15500SJayanth Othayoth{
1495ce15500SJayanth Othayoth    additional_data=$(busctl get-property xyz.openbmc_project.Logging \
1505ce15500SJayanth Othayoth                             $optional_path \
1515ce15500SJayanth Othayoth                             xyz.openbmc_project.Logging.Entry \
1525ce15500SJayanth Othayoth                             AdditionalData)
1535ce15500SJayanth Othayoth
1545ce15500SJayanth Othayoth    #read _PID data.
1555ce15500SJayanth Othayoth    if [ ! -z "$additional_data" ]; then
1565ce15500SJayanth Othayoth        pid=$(echo $additional_data | \
1575ce15500SJayanth Othayoth                   awk -F _PID= '{ print ($2+0)}')
1585ce15500SJayanth Othayoth    fi
1595ce15500SJayanth Othayoth}
1605ce15500SJayanth Othayoth
1615ce15500SJayanth Othayoth# @brief Initial version of the summary log
1625ce15500SJayanth Othayothinit_summary()
1635ce15500SJayanth Othayoth{
1645ce15500SJayanth Othayoth   log_summary "Name:          $name.tar.xz"
1655ce15500SJayanth Othayoth   log_summary "Epochtime:     $EPOCHTIME"
1665ce15500SJayanth Othayoth   log_summary "ID:            $dump_id"
1675ce15500SJayanth Othayoth   log_summary "Type:          $dump_type"
1685ce15500SJayanth Othayoth}
1695ce15500SJayanth Othayoth
1705ce15500SJayanth Othayoth# @brief Check the validity of user inputs and initialize global
1715ce15500SJayanth Othayoth#        variables. Create directory for temporary data collection
1725ce15500SJayanth Othayoth# @return 0 on success, error code otherwise
1735ce15500SJayanth Othayoth
1745ce15500SJayanth Othayothfunction initialize()
1755ce15500SJayanth Othayoth{
1765ce15500SJayanth Othayoth    #Dump file name
1775ce15500SJayanth Othayoth    if [ -z $name ]; then
1785ce15500SJayanth Othayoth        name=$"obmcdump_"$dump_id"_$EPOCHTIME"
1795ce15500SJayanth Othayoth    fi
1805ce15500SJayanth Othayoth
1815ce15500SJayanth Othayoth    #Create temporary data directory.
1825ce15500SJayanth Othayoth    mkdir -p "$TMP_DIR/$name"
1835ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
1845ce15500SJayanth Othayoth        echo "Error: Failed to create the temporary directory."
1855ce15500SJayanth Othayoth        return $RESOURCE_UNAVAILABLE;
1865ce15500SJayanth Othayoth    fi
1875ce15500SJayanth Othayoth
1885ce15500SJayanth Othayoth    #name directory
1895ce15500SJayanth Othayoth    name_dir="$TMP_DIR/$name"
1905ce15500SJayanth Othayoth
1915ce15500SJayanth Othayoth    #dreport log file
1925ce15500SJayanth Othayoth    dreport_log="$name_dir/$DREPORT_LOG"
1935ce15500SJayanth Othayoth
1945ce15500SJayanth Othayoth    #summary log file
1955ce15500SJayanth Othayoth    summary_log="$name_dir/$SUMMARY_LOG"
1965ce15500SJayanth Othayoth
1975ce15500SJayanth Othayoth    #Type
1985ce15500SJayanth Othayoth    if [[ ! ($dump_type = $TYPE_USER || \
1995ce15500SJayanth Othayoth             $dump_type = $TYPE_CORE || \
2000deb287cSMarri Devender Rao             $dump_type = $TYPE_ELOG || \
2010deb287cSMarri Devender Rao             $dump_type = $TYPE_CHECKSTOP) ]]; then
2025ce15500SJayanth Othayoth       log_error "Invalid -type, Only summary log is available"
2035ce15500SJayanth Othayoth       dump_type=$SUMMARY_DUMP
2045ce15500SJayanth Othayoth    fi
2055ce15500SJayanth Othayoth
2065ce15500SJayanth Othayoth    #Size
2075ce15500SJayanth Othayoth    #Check the input is integer.
2085ce15500SJayanth Othayoth    if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then
2095ce15500SJayanth Othayoth       #Converts in to bytes.
2105ce15500SJayanth Othayoth       dump_size="$((dump_size * 1024))"
2115ce15500SJayanth Othayoth    else
2125ce15500SJayanth Othayoth       dump_size=$UNLIMITED
2135ce15500SJayanth Othayoth    fi
2145ce15500SJayanth Othayoth
2155ce15500SJayanth Othayoth    return $SUCCESS
2165ce15500SJayanth Othayoth}
2175ce15500SJayanth Othayoth
2185ce15500SJayanth Othayoth# @brief Packaging the dump and transferring to dump location.
2195ce15500SJayanth Othayothfunction package()
2205ce15500SJayanth Othayoth{
2215ce15500SJayanth Othayoth    mkdir -p "$dump_dir"
2225ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
2235ce15500SJayanth Othayoth        log_error "Could not create the destination directory $dump_dir"
2245ce15500SJayanth Othayoth        dest_dir=$TMP_DIR
2255ce15500SJayanth Othayoth    fi
2265ce15500SJayanth Othayoth
2275ce15500SJayanth Othayoth    #tar and compress the files.
228*c237e3d8SChirag Sharma    if [ -f "$HEADER_EXTENSION" ]; then
2295ce15500SJayanth Othayoth        tar -Jcf "$name_dir.tar.xz" -C \
2305ce15500SJayanth Othayoth                 $(dirname "$name_dir") $(basename "$name_dir")
231*c237e3d8SChirag Sharma        echo "Adding Dump Header :"$HEADER_EXTENSION
232*c237e3d8SChirag Sharma        ("$HEADER_EXTENSION")
233*c237e3d8SChirag Sharma        cat "$name_dir.tar.xz" | tee -a "/tmp/dumpheader_$EPOCHTIME" > /dev/null
234*c237e3d8SChirag Sharma        mv "/tmp/dumpheader_$EPOCHTIME" "$name_dir.tar.xz"
235*c237e3d8SChirag Sharma    else
236*c237e3d8SChirag Sharma        tar -Jcf "$name_dir.tar.xz" -C \
237*c237e3d8SChirag Sharma                 $(dirname "$name_dir") $(basename "$name_dir")
238*c237e3d8SChirag Sharma    fi
2395ce15500SJayanth Othayoth
2405ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
2415ce15500SJayanth Othayoth        echo $($TIME_STAMP) "Could not create the compressed tar file"
2425ce15500SJayanth Othayoth        rm -r "$name_dir"
2435ce15500SJayanth Othayoth        return $INTERNAL_FAILURE
2445ce15500SJayanth Othayoth    fi
2455ce15500SJayanth Othayoth
2465ce15500SJayanth Othayoth    #remove the temporary name specific directory
2475ce15500SJayanth Othayoth    rm -r "$name_dir"
2485ce15500SJayanth Othayoth
2495ce15500SJayanth Othayoth    echo $($TIME_STAMP) "Report is available in $dump_dir"
2505ce15500SJayanth Othayoth
251a63f4a6fSChirag Sharma    if [ "$TMP_DIR" == "$dump_dir" ] || [ "$TMP_DIR/" == "$dump_dir" ]; then
2525ce15500SJayanth Othayoth       return $SUCCESS
2535ce15500SJayanth Othayoth    fi
2545ce15500SJayanth Othayoth
2555ce15500SJayanth Othayoth    #copy the compressed tar file into the destination
2565ce15500SJayanth Othayoth    cp "$name_dir.tar.xz" "$dump_dir"
2575ce15500SJayanth Othayoth    if [ $? -ne 0 ]; then
2585ce15500SJayanth Othayoth        echo "Failed to copy the $name_dir.tar.xz to $dump_dir"
2595ce15500SJayanth Othayoth        rm "$name_dir.tar.xz"
2605ce15500SJayanth Othayoth        return $INTERNAL_FAILURE
2615ce15500SJayanth Othayoth    fi
2625ce15500SJayanth Othayoth
2635ce15500SJayanth Othayoth    #Remove the temporary copy of the file
2645ce15500SJayanth Othayoth    rm "$name_dir.tar.xz"
2655ce15500SJayanth Othayoth}
2665ce15500SJayanth Othayoth
2675ce15500SJayanth Othayoth# @brief Main function
2685ce15500SJayanth Othayothfunction main()
2695ce15500SJayanth Othayoth{
2705ce15500SJayanth Othayoth    #initialize the global variables and
2715ce15500SJayanth Othayoth    #create temporary storage locations
2725ce15500SJayanth Othayoth    initialize
2735ce15500SJayanth Othayoth    result=$?
2745ce15500SJayanth Othayoth    if [[ ${result} -ne $SUCCESS ]]; then
2755ce15500SJayanth Othayoth        echo $($TIME_STAMP) "Error: Failed to initialize, Exiting"
2765ce15500SJayanth Othayoth        exit;
2775ce15500SJayanth Othayoth    fi
2785ce15500SJayanth Othayoth
27995a72983SGunnar Mills    #Initialize the summary log
2805ce15500SJayanth Othayoth    init_summary
2815ce15500SJayanth Othayoth
2825ce15500SJayanth Othayoth    #collect data based on the type.
2835ce15500SJayanth Othayoth    collect_data
2845ce15500SJayanth Othayoth
2855ce15500SJayanth Othayoth    package  #package the dump
2865ce15500SJayanth Othayoth    result=$?
2875ce15500SJayanth Othayoth    if [[ ${result} -ne $SUCCESS ]]; then
2885ce15500SJayanth Othayoth        echo $($TIME_STAMP) "Error: Failed to package, Exiting"
2895ce15500SJayanth Othayoth    else
29095a72983SGunnar Mills        echo $($TIME_STAMP) "Successfully completed"
2915ce15500SJayanth Othayoth        exit;
2925ce15500SJayanth Othayoth    fi
2935ce15500SJayanth Othayoth}
2945ce15500SJayanth Othayoth
2955ce15500SJayanth OthayothTEMP=`getopt -o n:d:i:t:s:p:vVqh \
2965ce15500SJayanth Othayoth      --long name:,dir:,dumpid:,type:,size:,path:,verbose,version,quiet,help \
2975ce15500SJayanth Othayoth      -- "$@"`
2985ce15500SJayanth Othayoth
2995ce15500SJayanth Othayothif [ $? -ne 0 ]
3005ce15500SJayanth Othayoththen
3015ce15500SJayanth Othayoth    echo "Error: Invalid options"
3025ce15500SJayanth Othayoth    exit 1
3035ce15500SJayanth Othayothfi
3045ce15500SJayanth Othayoth
3055ce15500SJayanth Othayotheval set -- "$TEMP"
3065ce15500SJayanth Othayoth
3075ce15500SJayanth Othayothwhile [[ $# -gt 1 ]]; do
3085ce15500SJayanth Othayoth    key="$1"
3095ce15500SJayanth Othayoth    case $key in
3105ce15500SJayanth Othayoth        -n|--name)
3115ce15500SJayanth Othayoth            name=$2
3125ce15500SJayanth Othayoth            shift 2;;
3135ce15500SJayanth Othayoth        -d|--dir)
3145ce15500SJayanth Othayoth            dump_dir=$2
3155ce15500SJayanth Othayoth            shift 2;;
3165ce15500SJayanth Othayoth        -i|--dumpid)
3175ce15500SJayanth Othayoth            dump_id=$2
3185ce15500SJayanth Othayoth            shift 2;;
3195ce15500SJayanth Othayoth        -t|--type)
3205ce15500SJayanth Othayoth            dump_type=$2
3215ce15500SJayanth Othayoth            shift 2;;
3225ce15500SJayanth Othayoth        -s|--size)
3235ce15500SJayanth Othayoth            dump_size=$2
3245ce15500SJayanth Othayoth            shift 2;;
3255ce15500SJayanth Othayoth        -p|--path)
3265ce15500SJayanth Othayoth            optional_path=$2
3275ce15500SJayanth Othayoth            shift 2;;
3285ce15500SJayanth Othayoth        -v|—-verbose)
3295ce15500SJayanth Othayoth            verbose=$TRUE
3305ce15500SJayanth Othayoth            shift;;
3315ce15500SJayanth Othayoth        -V|--version)
3325ce15500SJayanth Othayoth            shift;;
3335ce15500SJayanth Othayoth        -q|—-quiet)
3345ce15500SJayanth Othayoth            quiet=$TRUE
3355ce15500SJayanth Othayoth            shift;;
3365ce15500SJayanth Othayoth        -h|--help)
3375ce15500SJayanth Othayoth            echo "$help"
3385ce15500SJayanth Othayoth            exit;;
3395ce15500SJayanth Othayoth        *) # unknown option
3405ce15500SJayanth Othayoth            log_error "Unknown argument: $1"
3415ce15500SJayanth Othayoth            log_info "$help"
3425ce15500SJayanth Othayoth            exit 1;;
3435ce15500SJayanth Othayoth    esac
3445ce15500SJayanth Othayothdone
3455ce15500SJayanth Othayoth
3465ce15500SJayanth Othayothmain #main program
3475ce15500SJayanth Othayothexit $?
348