#!/usr/bin/ksh
# ============================================================
# LogDiag OS Inspection Script - Oracle Solaris
# Applies to : Solaris 10 / 11 (SPARC and x86, global zone recommended)
# Usage      : run as root ->  ksh solaris-pm.sh
# Output     : /var/tmp/<hostname>.<serial>.pm-<timestamp>.tar.gz
#              (falls back to .tar.Z when gzip is unavailable)
# Note       : read-only collection, no system configuration is changed.
# ============================================================

HOSTNAME_STR=`hostname 2>/dev/null || echo unknown`
# Serial: sneep (if installed) > prtconf banner; may be empty on some x86 boxes
SERIAL=`sneep 2>/dev/null | head -1 | tr -d ' /'`
if [ -z "$SERIAL" ]; then
    SERIAL=`prtconf -pv 2>/dev/null | grep -i 'chassis-sn' | head -1 | sed "s/.*'\(.*\)'.*/\1/" | tr -d ' /'`
fi
[ -z "$SERIAL" ] && SERIAL=NotAvailable
STAMP=`date +%Y%m%d_%H%M%S`
DIRNAME="${HOSTNAME_STR}.${SERIAL}.pm-${STAMP}"
CFGDIR="/var/tmp/${DIRNAME}"
mkdir -p "$CFGDIR"

# run <outfile> <command line>
run() {
    OUT="$CFGDIR/$1"; shift
    OUTDIR=`dirname "$OUT"`
    [ -d "$OUTDIR" ] || mkdir -p "$OUTDIR"
    {
        echo "### $*"
        sh -c "$*" 2>&1 || echo "[command failed or not available]"
        echo ""
    } >> "$OUT"
}

echo "Collecting system information ..."
date > "$CFGDIR/date.log"
run system/release.log        "cat /etc/release"
run system/uname.log          "uname -a"
run system/isainfo.log        "isainfo -v"
run system/uptime.log         "uptime; who -b"
run system/eeprom.log         "eeprom"
run system/crontab.log        "crontab -l"
run system/zones.log          "zoneadm list -cv"
run system/patches.log        "showrev -p 2>/dev/null | tail -100; pkg list 2>/dev/null | head -200"

echo "Collecting hardware information ..."
run hardware/prtconf.log      "prtconf -v | head -800"
run hardware/prtdiag.log      "prtdiag -v"
run hardware/psrinfo.log      "psrinfo -v; psrinfo -pv"
run hardware/memory.log       "prtconf 2>/dev/null | grep -i memory"
run hardware/cfgadm.log       "cfgadm -al"
run hardware/fmadm.log        "fmadm faulty -a"
run hardware/fmdump.log       "fmdump | tail -100"
run hardware/ipmitool.log     "ipmitool sel elist 2>/dev/null | tail -100"

echo "Collecting storage information ..."
run storage/format.log        "echo | format 2>/dev/null"
run storage/iostat-e.log      "iostat -En"
run storage/zpool-status.log  "zpool status -v"
run storage/zpool-list.log    "zpool list; zfs list"
run storage/metastat.log      "metastat -c 2>/dev/null; metastat 2>/dev/null | head -200"
run storage/df.log            "df -h"
run storage/mount.log         "mount -p"
run storage/vfstab.log        "cat /etc/vfstab"
run storage/swap.log          "swap -l; swap -s"
run storage/mpathadm.log      "mpathadm list lu 2>/dev/null"
run storage/vxdisk.log        "vxdisk -o alldgs list 2>/dev/null"

echo "Collecting network information ..."
run network/ifconfig.log      "ifconfig -a"
run network/dladm.log         "dladm show-phys 2>/dev/null; dladm show-link 2>/dev/null; dladm show-aggr 2>/dev/null"
run network/ipadm.log         "ipadm show-addr 2>/dev/null"
run network/netstat-rn.log    "netstat -rn"
run network/netstat-in.log    "netstat -in"
run network/netstat-an.log    "netstat -an | head -300"
run network/resolv.log        "cat /etc/resolv.conf; cat /etc/nsswitch.conf | grep hosts"

echo "Collecting service and log information ..."
run services/svcs-failed.log  "svcs -xv"
run services/svcs-all.log     "svcs -a | head -300"
run logs/messages.log         "tail -500 /var/adm/messages"
run logs/sulog.log            "tail -100 /var/adm/sulog"
run logs/dmesg.log            "dmesg | tail -300"

echo "Collecting performance snapshot (about 30 seconds) ..."
run performance/vmstat.log    "vmstat 3 10"
run performance/iostat.log    "iostat -xnz 3 5"
run performance/mpstat.log    "mpstat 3 3"
run performance/sar.log       "sar 3 5 2>/dev/null"
run performance/prstat.log    "prstat -n 15 3 2"
run performance/top10cpu.log  "ps -eo pcpu,pid,user,args | sort -rn | head -11"
run performance/top10mem.log  "ps -eo pmem,pid,user,args | sort -rn | head -11"

echo "Compressing inspection data ..."
cd /var/tmp || exit 1
tar -cf "${DIRNAME}.tar" "${DIRNAME}" && rm -rf "$CFGDIR"
if command -v gzip >/dev/null 2>&1; then
    gzip "${DIRNAME}.tar"
    echo "Inspection data saved to /var/tmp/${DIRNAME}.tar.gz"
else
    compress "${DIRNAME}.tar"
    echo "Inspection data saved to /var/tmp/${DIRNAME}.tar.Z"
fi
exit 0
