xref: /openbmc/openpower-debug-collector/dump/tools/opdump/opdreport (revision c14bf2c9518d8d2b20944d0f097db7d2e22ddc00)
135714cd2SDhruvaraj Subhashchandran#!/bin/bash
235714cd2SDhruvaraj Subhashchandran# shellcheck disable=SC2034  # Variable is used elsewhere
335714cd2SDhruvaraj Subhashchandran
435714cd2SDhruvaraj Subhashchandranhelp=$(cat << EOF
535714cd2SDhruvaraj Subhashchandran        opdreport creates an archive consisting of the following:
635714cd2SDhruvaraj Subhashchandran                * host dump files and header applied on top of it
735714cd2SDhruvaraj Subhashchandran        The type parameter controls the content of the data. The generated
835714cd2SDhruvaraj Subhashchandran        archive is stored in the user specified location.
935714cd2SDhruvaraj Subhashchandran
1035714cd2SDhruvaraj Subhashchandranusage: opdreport [OPTION]
1135714cd2SDhruvaraj Subhashchandran
1235714cd2SDhruvaraj SubhashchandranOptions:
1335714cd2SDhruvaraj Subhashchandran        -n, --name <name>     Name to be used for the archive.
1435714cd2SDhruvaraj Subhashchandran                              Default name format
1535714cd2SDhruvaraj Subhashchandran                              SYSDUMP.<serial number>.<dump_id>.<time>
1635714cd2SDhruvaraj Subhashchandran                              Optional parameter.
1735714cd2SDhruvaraj Subhashchandran        -d, --dir <directory> Archive directory to copy the compressed report.
1835714cd2SDhruvaraj Subhashchandran                              Default output directory is current working
1935714cd2SDhruvaraj Subhashchandran                              directory. Optional parameter.
2035714cd2SDhruvaraj Subhashchandran        -i, --dumpid <id>     Dump identifier to associate with the archive.
2135714cd2SDhruvaraj Subhashchandran                              Identifiers include numeric characters.
2235714cd2SDhruvaraj Subhashchandran                              Default dump identifier is 0
2335714cd2SDhruvaraj Subhashchandran        -s, --size <size>     Maximum allowed size (in KB) of the archive.
2435714cd2SDhruvaraj Subhashchandran                              Report will be truncated if size exceeds
2535714cd2SDhruvaraj Subhashchandran                              this limit. Default size is unlimited.
2635714cd2SDhruvaraj Subhashchandran        -f, --failingunit     The id of the failed unit
2735714cd2SDhruvaraj Subhashchandran        -e, --eid             Error log associated with the failure
2835714cd2SDhruvaraj Subhashchandran        -t, --type            Type of the dump to be collected
2935714cd2SDhruvaraj Subhashchandran                              1  -  Hardware dump
3035714cd2SDhruvaraj Subhashchandran                              3  -  Performance dump
3135714cd2SDhruvaraj Subhashchandran                              5  -  Hostboot dump
3235714cd2SDhruvaraj Subhashchandran                              10 -  SBE Dump
3335714cd2SDhruvaraj Subhashchandran        -h, --help            Display this help and exit.
3435714cd2SDhruvaraj SubhashchandranEOF
3535714cd2SDhruvaraj Subhashchandran)
3635714cd2SDhruvaraj Subhashchandran
3735714cd2SDhruvaraj Subhashchandran# Constants
3835714cd2SDhruvaraj Subhashchandranreadonly OP_DUMP="opdump"
3935714cd2SDhruvaraj Subhashchandranreadonly DREPORT_SOURCE="/usr/share/dreport.d"
4035714cd2SDhruvaraj Subhashchandranreadonly TRUE=1
4135714cd2SDhruvaraj Subhashchandranreadonly FALSE=0
4235714cd2SDhruvaraj Subhashchandranreadonly TIME_STAMP="date -u"
4335714cd2SDhruvaraj Subhashchandranreadonly UNLIMITED="unlimited"
4435714cd2SDhruvaraj Subhashchandranreadonly DREPORT_INCLUDE="$DREPORT_SOURCE/include.d"
4535714cd2SDhruvaraj Subhashchandranreadonly INVENTORY_MANAGER='xyz.openbmc_project.Inventory.Manager'
4635714cd2SDhruvaraj Subhashchandranreadonly INVENTORY_PATH='/xyz/openbmc_project/inventory/system'
4735714cd2SDhruvaraj Subhashchandranreadonly INVENTORY_ASSET_INT='xyz.openbmc_project.Inventory.Decorator.Asset'
4835714cd2SDhruvaraj Subhashchandranreadonly INVENTORY_BMC_BOARD='/xyz/openbmc_project/inventory/system/chassis/motherboard'
4935714cd2SDhruvaraj Subhashchandranreadonly HEADER_EXTENSION="$DREPORT_INCLUDE/gendumpheader"
5035714cd2SDhruvaraj Subhashchandranreadonly FILE_SCRIPT="$DREPORT_SOURCE/include.d/gendumpinfo"
5135714cd2SDhruvaraj Subhashchandran
5235714cd2SDhruvaraj Subhashchandran# Error Codes
5335714cd2SDhruvaraj Subhashchandranreadonly SUCCESS=0
5435714cd2SDhruvaraj Subhashchandranreadonly INTERNAL_FAILURE=1
5535714cd2SDhruvaraj Subhashchandranreadonly RESOURCE_UNAVAILABLE=2
5635714cd2SDhruvaraj Subhashchandran
5735714cd2SDhruvaraj Subhashchandran# Variables
5835714cd2SDhruvaraj Subhashchandrandeclare -x dump_type="$OP_DUMP"
5935714cd2SDhruvaraj Subhashchandrandeclare -x dump_sbe_type=100
6035714cd2SDhruvaraj Subhashchandrandeclare -x size_dump=""
6135714cd2SDhruvaraj Subhashchandrandeclare -x elog_id="00000000"
6235714cd2SDhruvaraj Subhashchandrandeclare -x EPOCHTIME
6335714cd2SDhruvaraj SubhashchandranEPOCHTIME=$(date +"%s")
6435714cd2SDhruvaraj Subhashchandrandeclare -x name=""
6535714cd2SDhruvaraj Subhashchandrandeclare -x dump_dir="/tmp"
6635714cd2SDhruvaraj Subhashchandrandeclare -x dump_id="00000000"
6735714cd2SDhruvaraj Subhashchandrandeclare -x dump_size="unlimited"
6835714cd2SDhruvaraj Subhashchandrandeclare -x content_path=""
6935714cd2SDhruvaraj Subhashchandrandeclare -x name_dir=""
7035714cd2SDhruvaraj Subhashchandrandeclare -x serialNo="0000000"
7135714cd2SDhruvaraj Subhashchandrandeclare -x dDay
7235714cd2SDhruvaraj SubhashchandrandDay=$(date -d @"$EPOCHTIME" +'%Y%m%d%H%M%S')
7335714cd2SDhruvaraj Subhashchandrandeclare -x dump_content_type=""
7435714cd2SDhruvaraj Subhashchandrandeclare -x FILE=""
7535714cd2SDhruvaraj Subhashchandran
7635714cd2SDhruvaraj Subhashchandran# @brief Get serial number property from inventory
7735714cd2SDhruvaraj Subhashchandranfunction fetch_serial_number() {
7835714cd2SDhruvaraj Subhashchandran    serialNo=$(busctl get-property "$INVENTORY_MANAGER" "$INVENTORY_PATH" \
7935714cd2SDhruvaraj Subhashchandran            "$INVENTORY_ASSET_INT" SerialNumber | cut -d " " -f 2 | \
8035714cd2SDhruvaraj Subhashchandran        sed 's/^"\(.*\)"$/\1/')
8135714cd2SDhruvaraj Subhashchandran
8235714cd2SDhruvaraj Subhashchandran    if [ -z "$serialNo" ]; then
8335714cd2SDhruvaraj Subhashchandran        serialNo="0000000"
8435714cd2SDhruvaraj Subhashchandran    fi
8535714cd2SDhruvaraj Subhashchandran}
8635714cd2SDhruvaraj Subhashchandran
8735714cd2SDhruvaraj Subhashchandran# @brief Check the validity of user inputs and initialize global variables
8835714cd2SDhruvaraj Subhashchandranfunction initialize() {
8935714cd2SDhruvaraj Subhashchandran    # shellcheck disable=SC2154 # name comes from elsewhere
9035714cd2SDhruvaraj Subhashchandran    if [ -z "$name" ]; then
9135714cd2SDhruvaraj Subhashchandran        name="SYSDUMP"
9235714cd2SDhruvaraj Subhashchandran    fi
9335714cd2SDhruvaraj Subhashchandran    fetch_serial_number
9435714cd2SDhruvaraj Subhashchandran    # shellcheck disable=SC2154 # dump_id comes from elsewhere
9535714cd2SDhruvaraj Subhashchandran    name="${name}.${serialNo}.${dump_id}.${dDay}"
9635714cd2SDhruvaraj Subhashchandran
9735714cd2SDhruvaraj Subhashchandran    if [ -z "$dump_sbe_type" ]; then
9835714cd2SDhruvaraj Subhashchandran        echo "Error: Dump type is not provided."
9935714cd2SDhruvaraj Subhashchandran        return "$RESOURCE_UNAVAILABLE"
10035714cd2SDhruvaraj Subhashchandran    fi
10135714cd2SDhruvaraj Subhashchandran
10235714cd2SDhruvaraj Subhashchandran    if [ -z "$dump_dir" ]; then
10335714cd2SDhruvaraj Subhashchandran        dump_dir=$PWD
10435714cd2SDhruvaraj Subhashchandran    fi
10535714cd2SDhruvaraj Subhashchandran
10635714cd2SDhruvaraj Subhashchandran    if [[ "$dump_size" =~ ^[0-9]+$ ]]; then
10735714cd2SDhruvaraj Subhashchandran        dump_size=$((dump_size * 1024))
10835714cd2SDhruvaraj Subhashchandran    else
10935714cd2SDhruvaraj Subhashchandran        dump_size=$UNLIMITED
11035714cd2SDhruvaraj Subhashchandran    fi
11135714cd2SDhruvaraj Subhashchandran
11235714cd2SDhruvaraj Subhashchandran    return "$SUCCESS"
11335714cd2SDhruvaraj Subhashchandran}
11435714cd2SDhruvaraj Subhashchandran
11535714cd2SDhruvaraj Subhashchandran# @brief Collect the dump
11635714cd2SDhruvaraj Subhashchandranfunction collect() {
11735714cd2SDhruvaraj Subhashchandran    content_path="/tmp/dump_${dump_id}_${EPOCHTIME}"
11835714cd2SDhruvaraj Subhashchandran    dump_outpath="$content_path/plat_dump"
11935714cd2SDhruvaraj Subhashchandran    if ! mkdir -p "$dump_outpath"; then
12035714cd2SDhruvaraj Subhashchandran        echo "Could not create the destination directory $dump_outpath"
12135714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
12235714cd2SDhruvaraj Subhashchandran    fi
12335714cd2SDhruvaraj Subhashchandran
12435714cd2SDhruvaraj Subhashchandran    dump-collect --type "$dump_sbe_type" --id "0x$dump_id" \
12535714cd2SDhruvaraj Subhashchandran        --failingunit "$failing_unit" --path "$dump_outpath"
12635714cd2SDhruvaraj Subhashchandran}
12735714cd2SDhruvaraj Subhashchandran
12835714cd2SDhruvaraj Subhashchandran# @brief Package the dump and transfer to dump location
12935714cd2SDhruvaraj Subhashchandranfunction package() {
13035714cd2SDhruvaraj Subhashchandran    FILE="/tmp/dumpheader_${dump_id}_${EPOCHTIME}"
13135714cd2SDhruvaraj Subhashchandran    if ! mkdir -p "$dump_dir"; then
13235714cd2SDhruvaraj Subhashchandran        echo "Could not create the destination directory $dump_dir"
13335714cd2SDhruvaraj Subhashchandran        dump_dir="/tmp"
13435714cd2SDhruvaraj Subhashchandran    fi
13535714cd2SDhruvaraj Subhashchandran
13635714cd2SDhruvaraj Subhashchandran    cd "$content_path" || exit "$INTERNAL_FAILURE"
13735714cd2SDhruvaraj Subhashchandran
138*c14bf2c9SSwethaParasa    dump_content_type=${dump_id:0:2}
13935714cd2SDhruvaraj Subhashchandran    "$FILE_SCRIPT"
14035714cd2SDhruvaraj Subhashchandran    elog_id=$eid
14135714cd2SDhruvaraj Subhashchandran
14235714cd2SDhruvaraj Subhashchandran    if ! tar -cvzf "$name" plat_dump/*Sbe* info.yaml; then
14335714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Could not create the compressed tar file"
14435714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
14535714cd2SDhruvaraj Subhashchandran    fi
14635714cd2SDhruvaraj Subhashchandran
14735714cd2SDhruvaraj Subhashchandran    size_dump=$(stat -c %s "$name")
14835714cd2SDhruvaraj Subhashchandran
14935714cd2SDhruvaraj Subhashchandran    if [ "$dump_size" != "$UNLIMITED" ] && \
15035714cd2SDhruvaraj Subhashchandran        [ "$size_dump" -gt "$dump_size" ]; then
15135714cd2SDhruvaraj Subhashchandran        rm "$name"
15235714cd2SDhruvaraj Subhashchandran        return "$RESOURCE_UNAVAILABLE"
15335714cd2SDhruvaraj Subhashchandran    fi
15435714cd2SDhruvaraj Subhashchandran
15535714cd2SDhruvaraj Subhashchandran    echo "Adding Dump Header: $HEADER_EXTENSION"
15635714cd2SDhruvaraj Subhashchandran    "$HEADER_EXTENSION"
15735714cd2SDhruvaraj Subhashchandran
15835714cd2SDhruvaraj Subhashchandran    if ! tee -a "$FILE" < "$name" > /dev/null; then
15935714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Could not create the compressed file"
16035714cd2SDhruvaraj Subhashchandran        rm -rf "$name" "$FILE"
16135714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
16235714cd2SDhruvaraj Subhashchandran    fi
16335714cd2SDhruvaraj Subhashchandran
16435714cd2SDhruvaraj Subhashchandran    if ! mv "$FILE" "$name"; then
16535714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Could not create the compressed file"
16635714cd2SDhruvaraj Subhashchandran        rm -rf "$name" "$FILE"
16735714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
16835714cd2SDhruvaraj Subhashchandran    fi
16935714cd2SDhruvaraj Subhashchandran
17035714cd2SDhruvaraj Subhashchandran    mv "$name" "$dump_dir"
17135714cd2SDhruvaraj Subhashchandran
17235714cd2SDhruvaraj Subhashchandran    rm -rf "$content_path"
17335714cd2SDhruvaraj Subhashchandran    rm -rf "$FILE" "$name"
17435714cd2SDhruvaraj Subhashchandran
17535714cd2SDhruvaraj Subhashchandran    return "$SUCCESS"
17635714cd2SDhruvaraj Subhashchandran}
17735714cd2SDhruvaraj Subhashchandran
17835714cd2SDhruvaraj Subhashchandran# @brief Initiate BMC dump
17935714cd2SDhruvaraj Subhashchandranfunction initiate_bmc_dump() {
18035714cd2SDhruvaraj Subhashchandran    bmcDumpPath=$(busctl call xyz.openbmc_project.Dump.Manager \
18135714cd2SDhruvaraj Subhashchandran            /xyz/openbmc_project/dump/bmc \
18235714cd2SDhruvaraj Subhashchandran        xyz.openbmc_project.Dump.Create CreateDump a\{sv\} 0)
18335714cd2SDhruvaraj Subhashchandran    result=$?
18435714cd2SDhruvaraj Subhashchandran    if [[ $result -ne $SUCCESS ]]; then
18535714cd2SDhruvaraj Subhashchandran        echo "Error in creating BMC dump associated with system dump"
18635714cd2SDhruvaraj Subhashchandran    else
18735714cd2SDhruvaraj Subhashchandran        echo "BMC dump initiated $bmcDumpPath"
18835714cd2SDhruvaraj Subhashchandran    fi
18935714cd2SDhruvaraj Subhashchandran}
19035714cd2SDhruvaraj Subhashchandran
19135714cd2SDhruvaraj Subhashchandran# @brief Main function
19235714cd2SDhruvaraj Subhashchandranfunction main() {
19335714cd2SDhruvaraj Subhashchandran    initialize
19435714cd2SDhruvaraj Subhashchandran    result=$?
19535714cd2SDhruvaraj Subhashchandran    if [[ $result -ne $SUCCESS ]]; then
19635714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Error: Failed to initialize, Exiting"
19735714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
19835714cd2SDhruvaraj Subhashchandran    fi
19935714cd2SDhruvaraj Subhashchandran
20035714cd2SDhruvaraj Subhashchandran    collect
20135714cd2SDhruvaraj Subhashchandran    result=$?
20235714cd2SDhruvaraj Subhashchandran    if [[ $result -ne $SUCCESS ]]; then
20335714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Error: Failed to collect dump, Exiting"
20435714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
20535714cd2SDhruvaraj Subhashchandran    fi
20635714cd2SDhruvaraj Subhashchandran
20735714cd2SDhruvaraj Subhashchandran    package
20835714cd2SDhruvaraj Subhashchandran    result=$?
20935714cd2SDhruvaraj Subhashchandran    if [[ $result -ne $SUCCESS ]]; then
21035714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Error: Failed to package, Exiting"
21135714cd2SDhruvaraj Subhashchandran        return "$INTERNAL_FAILURE"
21235714cd2SDhruvaraj Subhashchandran    else
21335714cd2SDhruvaraj Subhashchandran        echo "$($TIME_STAMP)" "Successfully completed"
21435714cd2SDhruvaraj Subhashchandran    fi
21535714cd2SDhruvaraj Subhashchandran
21635714cd2SDhruvaraj Subhashchandran    initiate_bmc_dump
21735714cd2SDhruvaraj Subhashchandran    return "$SUCCESS"
21835714cd2SDhruvaraj Subhashchandran}
21935714cd2SDhruvaraj Subhashchandran
22035714cd2SDhruvaraj Subhashchandranif ! TEMP=$(getopt -o n:d:i:s:t:e:f:h \
22135714cd2SDhruvaraj Subhashchandran        --long name:,dir:,dumpid:,size:,type:,eid:,failingunit:,help \
22235714cd2SDhruvaraj Subhashchandran        -- "$@"); then
22335714cd2SDhruvaraj Subhashchandran    echo "Error: Invalid options"
22435714cd2SDhruvaraj Subhashchandran    exit 1
22535714cd2SDhruvaraj Subhashchandranfi
22635714cd2SDhruvaraj Subhashchandran
22735714cd2SDhruvaraj Subhashchandraneval set -- "$TEMP"
22835714cd2SDhruvaraj Subhashchandran
22935714cd2SDhruvaraj Subhashchandranwhile [[ $# -gt 1 ]]; do
23035714cd2SDhruvaraj Subhashchandran    key="$1"
23135714cd2SDhruvaraj Subhashchandran    case $key in
23235714cd2SDhruvaraj Subhashchandran        -n|--name)
23335714cd2SDhruvaraj Subhashchandran            name=$2
23435714cd2SDhruvaraj Subhashchandran            shift 2 ;;
23535714cd2SDhruvaraj Subhashchandran        -d|--dir)
23635714cd2SDhruvaraj Subhashchandran            dump_dir=$2
23735714cd2SDhruvaraj Subhashchandran            shift 2 ;;
23835714cd2SDhruvaraj Subhashchandran        -i|--dumpid)
23935714cd2SDhruvaraj Subhashchandran            dump_id=$2
24035714cd2SDhruvaraj Subhashchandran            shift 2 ;;
24135714cd2SDhruvaraj Subhashchandran        -s|--size)
24235714cd2SDhruvaraj Subhashchandran            dump_size=$2
24335714cd2SDhruvaraj Subhashchandran            shift 2 ;;
24435714cd2SDhruvaraj Subhashchandran        -f|--failingunit)
24535714cd2SDhruvaraj Subhashchandran            failing_unit=$2
24635714cd2SDhruvaraj Subhashchandran            shift 2 ;;
24735714cd2SDhruvaraj Subhashchandran        -e|--eid)
24835714cd2SDhruvaraj Subhashchandran            eid=$2
24935714cd2SDhruvaraj Subhashchandran            shift 2 ;;
25035714cd2SDhruvaraj Subhashchandran        -t|--type)
25135714cd2SDhruvaraj Subhashchandran            dump_sbe_type=$2
25235714cd2SDhruvaraj Subhashchandran            shift 2 ;;
25335714cd2SDhruvaraj Subhashchandran        -h|--help)
25435714cd2SDhruvaraj Subhashchandran            echo "$help"
25535714cd2SDhruvaraj Subhashchandran            exit ;;
25635714cd2SDhruvaraj Subhashchandran        *) # unknown option
25735714cd2SDhruvaraj Subhashchandran            echo "Unknown argument: $1"
25835714cd2SDhruvaraj Subhashchandran            echo "$help"
25935714cd2SDhruvaraj Subhashchandran            exit 1 ;;
26035714cd2SDhruvaraj Subhashchandran    esac
26135714cd2SDhruvaraj Subhashchandrandone
26235714cd2SDhruvaraj Subhashchandran
26335714cd2SDhruvaraj Subhashchandranmain
26435714cd2SDhruvaraj Subhashchandranexit $?
265