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