#!/usr/bin/sh
# ============================================================
# LogDiag OS Inspection Script - HP-UX
# Applies to : HP-UX 11i v2 (11.23) / 11i v3 (11.31), PA-RISC and Itanium
# Usage      : run as root ->  sh hpux-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=`getconf MACHINE_SERIAL 2>/dev/null | tr -d ' /'`
if [ -z "$SERIAL" ]; then
    SERIAL=`machinfo 2>/dev/null | grep -i 'machine serial' | awk -F': *' '{print $2}' | 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/uname.log         "uname -a"
run system/machinfo.log      "machinfo"
run system/model.log         "model; getconf MACHINE_MODEL 2>/dev/null"
run system/uptime.log        "uptime; who -b"
run system/swlist.log        "swlist -l bundle"
run system/swlist-patch.log  "swlist -l patch 2>/dev/null | tail -200"
run system/kernel-tunables.log "kctune 2>/dev/null | head -200; kmtune 2>/dev/null | head -200"
run system/crontab.log       "crontab -l"
run system/setboot.log       "setboot"
run system/parstatus.log     "parstatus 2>/dev/null; vparstatus 2>/dev/null"

echo "Collecting hardware information ..."
run hardware/ioscan.log      "ioscan -fn"
run hardware/ioscan-disk.log "ioscan -fnC disk"
run hardware/memory.log      "machinfo 2>/dev/null | grep -i memory; dmesg 2>/dev/null | grep -i physical"
run hardware/cpu.log         "machinfo 2>/dev/null | grep -i -A2 cpu; ioscan -fnC processor"
run hardware/fru.log         "cstm_fru 2>/dev/null; echo 'selclass qualifier cpu;info;wait;infolog' | cstm 2>/dev/null | head -200"
run hardware/olrad.log       "olrad -q 2>/dev/null"

echo "Collecting storage information ..."
run storage/vgdisplay.log    "vgdisplay -v"
run storage/pvdisplay.log    "for PV in \`vgdisplay -v 2>/dev/null | grep 'PV Name' | awk '{print \$3}'\`; do pvdisplay \$PV; done"
run storage/lvlnboot.log     "lvlnboot -v 2>/dev/null"
run storage/bdf.log          "bdf"
run storage/mount.log        "mount -p"
run storage/fstab.log        "cat /etc/fstab"
run storage/swapinfo.log     "swapinfo -tam"
run storage/diskinfo.log     "for DSF in /dev/rdisk/disk* /dev/rdsk/c*t*d0; do [ -c \"\$DSF\" ] && diskinfo \$DSF 2>/dev/null; done | head -300"

echo "Collecting network information ..."
run network/lanscan.log      "lanscan"
run network/netconf.log      "cat /etc/rc.config.d/netconf 2>/dev/null | grep -v '^#' | grep -v '^\$'"
run network/ifconfig.log     "for LAN in \`lanscan 2>/dev/null | awk '/lan/{print \$5}'\`; do ifconfig \$LAN 2>/dev/null; done"
run network/netstat-in.log   "netstat -in"
run network/netstat-rn.log   "netstat -rn"
run network/netstat-an.log   "netstat -an | head -300"
run network/resolv.log       "cat /etc/resolv.conf; grep hosts /etc/nsswitch.conf"
run network/lanadmin.log     "for PPA in \`lanscan 2>/dev/null | awk '/lan/{print \$3}'\`; do lanadmin -x \$PPA 2>/dev/null; done"

echo "Collecting cluster information (if any) ..."
run cluster/cmviewcl.log     "cmviewcl -v 2>/dev/null"

echo "Collecting log information ..."
run logs/syslog.log          "tail -500 /var/adm/syslog/syslog.log"
run logs/oldsyslog.log       "tail -200 /var/adm/syslog/OLDsyslog.log 2>/dev/null"
run logs/dmesg.log           "dmesg"
run logs/rc.log              "tail -100 /etc/rc.log 2>/dev/null"
run logs/shutdownlog.log     "tail -50 /etc/shutdownlog 2>/dev/null"

echo "Collecting performance snapshot (about 30 seconds) ..."
run performance/vmstat.log   "vmstat 3 10"
run performance/iostat.log   "iostat 3 5"
run performance/sar.log      "sar 3 5 2>/dev/null"
run performance/top.log      "top -d 1 -f $CFGDIR/performance/top.raw 2>/dev/null; head -40 $CFGDIR/performance/top.raw 2>/dev/null"
run performance/top10cpu.log "UNIX95= ps -eo pcpu,pid,user,args | sort -rn | head -11"
run performance/top10mem.log "UNIX95= ps -eo vsz,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
