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