#!/bin/bash
# Copyright (c) Veeam Software Group GmbH
#
# Collect information about the used hardware."

if [ "$#" -lt 1 ]; then
    echo "Usage: $0 [output file]"
    OUT="/tmp/hardware.txt"
else
    OUT="$1"
fi

echo "Collect hardware information in ${OUT}"
date > "${OUT}"

collect_cmd() {
    echo "$1"
    printf "\n##### $2 #####\n " >> "${OUT}"
    $2 2>&1 >> "${OUT}"
}

collect_smartctl() {
    echo "Collect smartctl data (does not work with RAID controllers)"
    if which smartctl
    then
        printf "\n\n##### smartctl #####\n" >> "${OUT}"
        disks=$(lsblk --nodeps -n -o name)
        array=()
        while read -r element; do
            array+=("$element")
        done <<< "$disks"

        for i in "${array[@]}"
        do
            smartctl -i /dev/$i 2>&1 >> "${OUT}"
        done
    else
        echo "smartctl not found" >> "${OUT}"
    fi
}

while read -r LINE
do
    collect_cmd "Dumping ${LINE%%|*}" "${LINE##*|}"
done << EOF
system vendor|cat /sys/devices/virtual/dmi/id/sys_vendor
product name|cat /sys/devices/virtual/dmi/id/product_name
board name|cat /sys/devices/virtual/dmi/id/board_name
ip settings|ip a
dmidecode|dmidecode
lspci|lspci
lshw|lshw
cpuinfo|cat /proc/cpuinfo
EOF

collect_smartctl

while read -r LINE
do
    collect_cmd "Running ${LINE%%|*}" "${LINE##*|}"
done << EOF
tree|tree -J /sys/block/*
lsblk|lsblk -f
multipath -v|multipath -v 3 -ll
multipathd show paths|multipathd show paths
multipathd show maps|multipathd show maps
EOF

echo "Complete."
echo "See output in ${OUT}"
