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