#!/bin/sh

# nate at coraid dot com
# robin at coraid dot com
# srowe at coraid dot com
# clayton at coraid dot com

VERSION=1366202829

if [ `uname -r` = "5.10" ] ; then
    echo "Solaris 10 not supported at this time."
    exit 99
fi

now=$(date +%s)

tmpdir=/tmp/coraid-$(hostname)-${now}
outfile=/tmp/coraid-$(hostname)-${now}.txt

if [ -t 1 -a -t 0 ] ; then # running in a terminal?
    MODE=file
    GRABMSGS=0
    EXTENDED=1 #extended is now the default. -E turns it off.
    HEADERS=1
    while getopts Eehlmtvx: flag
    do
        case $flag in
            E)
                EXTENDED=0
                ;;
            e)
                EXTENDED=1
                echo "Please be patient. Extended diagnostics may take several minutes to collect."
                ;;
            h)
                echo Usage:
                echo -e "${0} [-h] | [-e] [-m] [-t]"
                echo -e "\t-h : Show this help and exit"
                echo -e "\t-E : Disable extended diagnostics"
                echo -e "\t-e : Deprecated. left for backwards compatibility"
                echo -e "\t-m : Capture a copy of OS's messages file"
                echo -e "\t-t : Capture diagnostics in tarball format"
                exit 0
                ;;
            l)
                MODE=local
                ;;
            m)
                GRABMSGS=1
                ;;
            t)
                MODE=tar
                ;;
            v)
                echo Version: ${VERSION}
                exit 0
                ;;
            x)
                EXEX="${OPTARG}"
                HEADERS=0
                ;;
        esac
    done
else # running in a pipeline?
    [ -z "${MODE}" ] && MODE=file
    [ -z "${GRABMSGS}" ] && GRABMSGS=0
    [ -z "${EXTENDED}" ] && EXTENDED=1 #extended is now the default. -E turns it off.
    [ -z "${HEADERS}" ] && HEADERS=1
fi
if [ "${MODE}" = tar ] ; then
    mkdir -p ${tmpdir}
    cd ${tmpdir}
elif [ -z "${EXEX}" -a "${MODE}" = file ] ; then
    exec >${outfile}
fi

merooty(){
    if [ $(id -u) != 0 ]; then
        echo "please run as root" >&2
        exit 1
    fi
}

cleanup(){
    cd /tmp
    [ "${MODE}" = "tar" ] && rm -rf ${tmpdir}
}
trap "cleanup" EXIT
sepuku(){
    cleanup
    exit
}
trap "sepuku" INT

have(){
    unset -v have;
    type $1 > /dev/null 2>&1 && have='yes'
}

header(){
    if [ ${HEADERS} = 1 ] ; then
        echo
        echo ==========================================
        echo ${1}
        echo ==========================================
    fi
}

run(){
    run=$1
    label=$2
    if [ -z "${EXEX}" ] ; then
        printf . >&2
    fi
    if [ "${MODE}" = "tar" ] ; then
        eval ${run} > ${label} 2>&1
    else
        header ${label}
        eval ${run} 2>&1
    fi
}

grab(){
    if [ "${MODE}" = "tar" ] ; then
        grep -h . ${1} > $(basename ${1})
    else
        header ${1}
        grep -h . ${1}
    fi
}

columnt(){
    python -c 'import sys;from copy import deepcopy;a=[x.strip().split() for x in sys.stdin.readlines()];sys.exit(0) if len(a) == 0 else None;qq=deepcopy(a);longest=len(max(a,key=len));b=[i for i in a if len(i)==longest];z=[[row[i] for row in b] for i in range(len(b[0]))];l=[len(max(x, key=len))+2 for x in z]; o=[[ v.ljust(l[i]) for i,v in enumerate(x)] for x in b]; print "\n".join(["".join(y) for y in o]);print("TRUNCATED!TRUNCATED!TRUNCATED!TRUNCATED!TRUNCATED!") if len(b) != len(qq) else None;'
}

have column && unset -f columnt
have column && alias columnt='column -t'

driver(){
    if [ "${MODE}" = "tar" ] ; then
        mkdir driver
        cd driver
    fi
    DRVLOC=uninstalled
    if [ -e /proc/scsi/ethdrv ] ; then
        DRVLOC= "/proc/scsi/ethdrv/ethdrv."
        loopdriver $DRVLOC
    fi
    if [ -e /proc/ethdrv ] ; then
        DRVLOC=/proc/ethdrv/
        loopdriver $DRVLOC
        case ${me} in
            Linux)
                run "ethdrv-stat" ethdrv-stat
                ;;
        esac
    fi
    if [ "${me}" = SunOS -a -e /dev/ethdrv ] ; then
        DRVLOC=/dev/ethdrv/
        loopdriver $DRVLOC
        run "ethdrvadm list-devices" ethdrvadm-list-devices
    fi
    if [ -e /proc/ethdrv.release ] ; then
        DRVLOC="/proc/ethdrv."
        loopdriver $DRVLOC
    fi
    if [ ! -e ${DRVLOC} -o "${DRVLOC}" = "uninstalled" ] ; then
        run echo "HBA Driver either too old or not installed." 'nodriver'
    fi
    [ "${MODE}" = "tar" ] && cd ${tmpdir}
}

loopdriver(){
    printf .
    DRVLOC=${1}
    for f in ${DRVLOC}* ; do
        if [ "${f}" = "${DRVLOC}cec" ] ; then continue ; fi
        if [ "${f}" = ${DRVLOC}ifstats -o ${f} = ${DRVLOC}corestats -o ${f} = ${DRVLOC}config ] ; then
            grab ${f}
        else
            DHEADERS=''
            if [ "${f}" = ${DRVLOC}acbs ] ; then
                DHEADERS='ln 4 s.s cwrk mxwn cscsi caoe cmds rtx unre'
            fi
            if [ "${f}" = ${DRVLOC}ca ] ; then
                DHEADERS='ln s.s cwrk clamp mxwn ssthresh rttavg rttdev'
            fi
            if [ "${MODE}" = tar ] ; then
                cat ${f} > $(basename ${f}) #original
                DDEST=$(basename ${f})-t    #column-t'ed
                if [ ! -z "${DHEADERS}" ] ; then
                    (echo ${DHEADERS} ; cat ${f}) | columnt >> ${DDEST}
                else
                    grep -h . ${f} | columnt >> ${DDEST}
                fi
            else
                header ${f}
                if [ ! -z "${DHEADERS}" ] ; then
                    (echo ${DHEADERS} ; cat ${f}) | columnt
                else
                    grep -h . ${f} | columnt
                fi
            fi
        fi
    done
}

targeted(){
    if [ "${me}" = "SunOS" ]; then
        if [ "${MODE}" = local ] ; then
            # :(
            DRVLOC=/dev/ethdrv/
        fi
        targets=$(for zpool in $(zpool list -H -o name |egrep -v 'rpool|syspool') ; do for tgt in $(zpool status ${zpool} |awk '{print $1}' |egrep '^c[0-9]+t' |sort -n |uniq |sed -e 's/^c[0-9]*t//' -e 's/d/\./') ; do echo $tgt; done; done;)
    fi
    if [ "${me}" = "VMkernel" ]; then
        if [ "${MODE}" = local ] ; then
            DRVLOC=/proc/ethdrv/
        fi
        targets=$(mapnaatoctd | awk '{print $2}')
    fi
    if [ -n "${targets}" ] ; then
        if [ "${MODE}" = local ] ; then
            diagconcatenator $targets | columnt
        else
            run "diagconcatenator $targets | columnt" targeted-diags
        fi
    fi
}

diagconcatenator(){
    if [ ! -e "${DRVLOC}acbs" ] ; then
        return ""
    fi
    grep=grep
    if [ "${me}" = "SunOS" ]; then
        grep=/usr/xpg4/bin/grep
    fi
    echo 's.s 4 cwrk mxwn clamp cscsi caoe cmds rtx unre ssthresh rttavg rttdelt'
    for t in ${*} ; do
        $grep -hE "^[[:digit:]]+ 4*[[:space:]]*$t " ${DRVLOC}acbs ${DRVLOC}ca | sed -e 'H;$!d;x;s/\n/ /g' -e 's/^ //' | awk '{print $3 " " $2 " " $4 " " $5 " " $14 " " $6 " " $7 " " $8 " " $9 " " $10 " " $16 " " $17 " " $18}'
    done
}


#esx
esxversion(){
    run "vmware -v" vmware-v
}

esxmpath(){ run "esxcfg-mpath -l" esxcfg-mpath-l
}

esxmodinfo(){
    run "esxcfg-module -i ethdrv" esxcfg-module-i-ethdrv
}

esxvolume(){
    run "esxcfg-volume -l" esxcfg-volume-l
}

esxscsidevs(){
    run "esxcfg-scsidevs -m" esxcfg-scsidevs-m
}

lsvms(){
    run 'vim-cmd vmsvc/getallvms' vim-cmd-vmsvc-getallvms
}
mapnaatoctd(){
    esxcfg-mpath -L | sort | awk '{print $1 " " $3}' |  awk " BEGIN { n=1; while (getline < \"${DRVLOC}devices\" > 0){ target[\$1] = n; aoe[n] = \$2; n++; } } { if (target[\$1]){ mtarget=\$1; mnaa=\$2; print mtarget \" \" aoe[target[mtarget]] \" \" mnaa }else{ ignore } }" | sort -k2 | columnt
}
wrapmap(){
    run mapnaatoctd "naa-map"
}
#end ESX

#solaris
whatsol(){
    run 'uname -a' uname-a
    [ -e /etc/release ] && run 'cat /etc/release' release
    have sneep && run 'sneep' sneep
    have hostid && run 'hostid' hostid
    have pkg && run 'pkg info entire' pkg-info-entire
    #have pkg && run 'pkg info kernel' pkg-info-kernel
    #have pkg && run 'pkg info zfs' pkg-info-zfs
    have pkg && run "for idr in $(pkg info |grep "Name: idr" |awk '{print $2}') ; do pkg info \${idr} ; done" pkg-info-idr

}

cscanpci(){
    have scanpci && run 'scanpci | grep -v ^$' scanpci
}

fmtopo(){
    have fmtopo && run 'fmtopo -Vp' fmtopo-Vp
}

lsiscan(){
    have sas2flash && for c in $(sas2flash -listall 2>/dev/null |egrep ^[0-9] |awk '{print $1}') ; do
        run "sas2flash -c ${c} -list" LSI-firmware-c${c}
    done
}

lsidisks(){
    have sas2ircu && for c in $(sas2ircu LIST 2>/dev/null |awk '{print $1}' |egrep ^[0-9]) ; do
        run "sas2ircu ${c} DISPLAY" sas2ircu-${c}-DISPLAY
    done
}

solipmitool(){
    have ipmitool && run "ipmitool fru" ipmitool-fru
}

solfw(){
    have fwupdate && run "fwupdate list all" fwupdate-list-all
}

solintrstat(){
    have intrstat && run "intrstat 3 1" intrstat
}

soltuning(){
    for tunable in /etc/system /lib/svc/method/coraid-tuning.sh ; do
    if [ -f ${tunable} ] ; then
        run "egrep -v '\#|\*' ${tunable} |sort |uniq" ${tunable}
    fi
    done
}

cvmstat(){
    have vmstat && run 'vmstat' vmstat
    have vmstat && run 'vmstat -s' vmstat-s
}

fmstatus(){
    have fmstat && run 'fmstat' fmstat
}

fmadmconfig(){
    have fmadm && run 'fmadm config' fmadm-config
}

cprtdiag(){
    have prtdiag && run 'prtdiag -v' prtdiag
}

cfmdump(){
    have fmdump && run 'fmdump -t 7d' fmdump
    for uuid in $(fmdump -t 7d |egrep -v '^TIME|Repaired|Resolved' |awk '{print $4}') ; do
        run "fmdump -m -u ${uuid}" fmdump-m-${uuid}
        run "fmdump -eV -u ${uuid}" fmdump-eV-${uuid}
    done
    if [ ${GRABMSGS} -eq 1 ] ; then
        run "fmdump -eV -t 7d" fmdump-eV-t7d${ena}
    fi
}

cfmfaulty(){
    have fmadm && run 'fmadm faulty -av' fmadm-faulty-av
}

csig(){
    have machinesig && run 'machinesig' machinesig
}

csvcs(){
    have svcs && run 'svcs -xv' svcs-xv
}

zpoollistH(){
    run 'zpool list -H' zpool-list
}

zfslistsnapshots(){
    run 'zfs list -H -r -t snapshot |columnt' zfs-list-snapshots
    run 'zfs list -H -r -t snapshot -o snapshot:type,name |columnt' zfs-list-snapshots-type
}

solshare(){
    uname -v | grep Nexenta > /dev/null 2>&1
    if [ ${?} -eq 0 ] ; then
        have sharemgr && run 'sharemgr show -pv' sharemgr-show-pv
    else
        if [ $(uname -a |awk '{print $4}') == "11.1" ] ; then
            run "zfs get -H share.all |columnt" zfs-get-share-all
        fi
        have share && run 'share -A |columnt' share-A
    fi
    have sharectl && run 'sharectl get nfs |sort' sharectl-nfs
    have sharectl && run 'sharectl get smb |sort' sharectl-smb
}

solcifsmode(){
    have smbadm && run 'smbadm list' smbadm-list
}

solsidmap(){
    have svccfg && run 'svccfg -s idmap listprop config/unresolvable_sid_mapping' idmap-sid-mapping
}

unslash(){
    echo "$1" | sed -e 's/\//_/g'
}

zpoolgetall(){
    for zpool in $(zpool list -H -o name); do
        uname -v | grep Nexenta > /dev/null 2>&1
        run "zpool status -Dv ${zpool}" zpool-status-Dv-${zpool}
        run "zfs list -r ${zpool} |grep -v ^NAME |columnt" zfs-list-${zpool}
        run "zpool get all ${zpool} |grep -v ^NAME |sort -k2" zpool-get-all-${zpool}
        run "iostat -xen $(zpool status ${zpool} |awk '{print $1}' |egrep '^c[0-9]+t') 3 1" iostat-${zpool}
        for zvol in $(zfs list -Hr |grep ^${zpool} |awk '{print $1}' |sort) ; do
            nszv=$(unslash ${zvol})
            run "zfs list -o space ${zvol} |grep -v ^NAME |columnt" zfs-list-space-${nszv}
            run "zfs get all ${zvol} |grep -v ^NAME |sort -k2" zfs-get-all-${nszv}
            if [ $(uname -a |awk '{print $4}') == "11.1" ] ; then
                run "zfs get share.nfs.all ${zvol} |grep -v ^NAME " zfs-get-share-nfs-all-${nszv}
                run "zfs get share.smb.all ${zvol} |grep -v ^NAME " zfs-get-share-smb-all-${nszv}
            fi
        done
        run "zpool history -il ${zpool}" zpool-history-il-${zpool}
    done
}

solmapdevices(){
    python -c 'import os;devs=[y.split()[0] for y in open("/dev/ethdrv/devices").readlines()];x=[y.split()[0] for y in os.popen("iostat -x").readlines()];xn=[y.split()[-1] for y in os.popen("iostat -xn")];c=dict(zip(x,xn)); print "\n".join(["%s = %s" % (y,c.get(y)) for y in devs])'
}

solcpuinfo(){
    have psrinfo && run 'psrinfo -pv' psrinfo-pv
}

solcron(){
    if [ -d /var/spool/cron/crontabs ] ; then
        for c in $(find /var/spool/cron/crontabs -type f) ; do
            grab ${c}
        done
    fi
}

solcfgsas(){
    sasinfo hba-port |grep "HBA Port Name" |awk -F"/" '{print $4}'|xargs -i cfgadm -alv {} |egrep -v "Ap_Id|^When"
}

soldiskinfo(){
    have diskinfo && run 'diskinfo -v' diskinfo-v
    have sasinfo && run "sasinfo hba -v" sasinfo-hba-v
    have sasinfo && run "sasinfo hba-port -v" sasinfo-hba-port-v
    have sasinfo && run "sasinfo expander -v" sasinfo-expander-v
    have sasinfo && run "sasinfo lu -v" sasinfo-lu-v
    run solcfgsas "cfgadm-sas"
    for rdsk in $(mpathadm list lu|grep "/dev/rdsk/") ; do
        run "mpathadm show lu ${rdsk}" mpathadm-show-lu-${rdsk}
    done
    for conf in /etc/driver/drv/scsi_vhci.conf /kernel/drv/scsi_vhci.conf ; do
        [ -f ${conf} ] && run "egrep -v '^\#|^$' ${conf}" ${conf}
    done
    have stmsboot && run "stmsboot -L" stmsboot-L
}

solmemstat(){
    run 'echo "::memstat" |mdb -k 2> /dev/null' mdb-k-memstat
}

solirq(){
    run 'echo "::interrupts -d" |mdb -k 2> /dev/null' mdb-k-interrupts
}

solarcstat(){
    run 'echo "::arc" |mdb -k 2> /dev/null |sort' mdb-k-arcstat
}

solzfsparams(){
    params1=$(echo "::zfs_params" |mdb -k 2>/dev/null |awk '{print $1}')
    params2="swapfs_minfree freemem zfs_resilver_delay zfs_scrub_delay zfs_top_maxinflight zfs_resilver_min_time_ms"
    params="${params1} ${params2}"
    run "for param in $(echo \${params} |tr " " "\n" |sort) ;do echo "\${param}/D0t" |mdb -k 2>/dev/null |grep -v :$ ;done |awk -F: '{print \$1\" \"\$2}' |columnt" mdb-k-zfsparams
}
solarcsummary(){
    have arc_summary.pl && run 'arc_summary.pl' arc_summary
}

solnetwork(){
    have dladm && run 'dladm show-phys |grep -v ^LINK |sort' dladm-show-phys
    have dladm && run 'dladm show-phys -m |grep -v ^LINK |sort' dladm-show-phys-m
    uname -v | grep Nexenta > /dev/null 2>&1
    if [ ${?} -eq 0 ] ; then
        have dladm && run 'dladm show-phys |grep -v ^LINK |sort' dladm-show-phys
    else
        have dladm && run 'dladm show-phys -L |grep -v ^LINK |sort' dladm-show-phys-L
    fi
    have dladm && run 'dladm show-link |grep -v ^LINK |sort' dladm-show-link
    have dladm && run 'dladm show-ether |grep -v ^LINK |sort' dladm-show-ether
    have ifconfig && run 'ifconfig -a' ifconfig-a
    have ipadm && run 'ipadm show-addr' ipadm-show-addr
    have ipadm && run 'ipadm show-if' ipadm-show-if
    have dladm && run 'dladm show-aggr' dladm-show-aggr
    have dladm && run 'dladm show-aggr -x' dladm-show-aggr-x
    have dladm && run "for aggr in $(dladm show-aggr |sort |grep -v ^LINK |awk '{print $1}') ; do dladm show-aggr -L \${aggr} ; done" dladm-show-aggr-L
    have dladm && run 'dladm show-aggr -s' dladm-show-aggr-s
    have dlstat && run 'dlstat' dlstat
    have dlstat && run 'dlstat show-aggr' dlstat-show-aggr
    have dladm && run 'dladm show-vlan' dladm-show-vlan
    have ipmpstat && run 'ipmpstat -i' ipmpstat-i
    have ipmpstat && run 'ipmpstat -an' ipmpstat-an
    have netstat && run 'netstat -nr' netstat-nr
    [ -f /etc/inet/static_routes-DefaultFixed ] && grab /etc/inet/static_routes-DefaultFixed
    have ipadm && run 'ipadm show-prop -p hostmodel ipv4' ipadm-show-prop-hostmodel
    have ipf && run 'ipfstat -hion' ipfstat-hion
    if [ ${GRABMSGS} -eq 1 ] ; then
        have dladm && run "for net in $(dladm show-link |sort |grep -v ^LINK |awk '{print $1}') ; do dladm show-linkprop \${net} 2>/dev/null |grep -v ^LINK ; echo ; done" dladm-show-linkprop
        have ipadm && run "ipadm show-prop" ipadm-show-prop
        #kstat -c net
    fi
    [ -f /etc/hosts ] && grab /etc/hosts
    [ -f /etc/resolv.conf ] && grab /etc/resolv.conf
    [ -f /etc/netmasks ] && grab /etc/netmasks
    [ -f /etc/static_routes ] && grab /etc/static_routes
}
solcluster() {
    if [ -d /usr/cluster/bin ] ; then
        run 'cluster show' cluster-show
        run 'clrg status' clrg-status
        run 'clrs status' clrs-status
        run 'clq status'  clq-status
        run 'clinterconnect show -v' clinterconnect-show-v
        run 'solmapdevices' solmapdevices
        grab /etc/cluster/ccr/global/infrastructure
        #clrg show
        #clrs show
        #cldevice list -v
    fi
}

rsfdiags(){
    pkginfo -l rsf-1  > /dev/null 2>&1
    if [ ${?} -eq 0 ] ; then
        have pkginfo && run 'pkginfo -l rsf-1' rsf-1-version
        rsfpath=/opt/HAC/RSF-1
        rsfconf=${rsfpath}/etc/config
        if [ -f ${rsfpath}/bin/rsfcli ] ; then
            run "${rsfpath}/bin/rsfmon -v 2>/dev/null" rsfmon-v
            run "${rsfpath}/bin/rsfcli -i0 -v list 2>/dev/null" rsfcli-list
            run "${rsfpath}/bin/rsfcli -i0 -v heartbeats 2>/dev/null" rsfcli-hearbeat
            run "${rsfpath}/bin/rsfcli -i0 status 2>/dev/null" rsfcli-status
        fi
        if [ -f ${rsfconf} ] ; then
            grab ${rsfconf}
        fi
        if [ ${GRABMSGS} -eq 1 ] ; then
            rsflogs=${rsfpath}/log
            for log in config_dist.log mapmgr.log rsfmon.log ${rsflogs}/*.panic.log ; do
                if [ -f "${rsflogs}/${log}" ] ; then
                    grab "${rsflogs}/${log}"
                fi
                if [ -f "${log}" ] ; then
                    grab "${log}"
                fi
            done
        fi
    fi
}
solprstat(){
    run 'echo |prstat -amv -n 999999 0 1' top
}
cpackies(){
    have pkginfo && pkginfo |grep -i coraid > /dev/null 2>&1
    if [ ${?} -eq 0 ] ; then
        run "pkginfo |grep -i coraid |awk '{print \$2}' |xargs pkginfo -l" coraid-pkgs
    fi
}

nexall(){
    # this is massive -- probably a better way
    uname -v | grep Nexenta > /dev/null 2>&1
    if [ ${?} -eq 0 ] ; then
        if [ ${GRABMSGS} -eq 1 ] ; then
        have nmc && run 'echo "show all"|nmc' nmc-show-all
        for log in nms.log nmv.log nms-down.log nmcd.log ; do
            if [ -f /var/log/${log} ] ; then
                grab /var/log/${log}
            fi
        done
        fi
    fi

}

#end solaris
#begin linux
liniostat(){
    have iostat && run "iostat -xdh" iostat-xdh
}
whatlinux(){
    run "uname -a" uname-a
    run 'grep . /etc/*release* /etc/*version* | sed 50q' version
}
linmodinfo(){
    run "find /sys/module/aoe -name version | xargs cat" module-name
}
linmounts(){
    grab /proc/mounts
}
linaoe(){
    have aoe-version && run aoe-version aoe-version
    linnetwork
    have aoe-interfaces && run aoe-interfaces aoe-interfaces
    for ko in $(find /lib/modules/ -type f -name "aoe.ko") ; do
        run "strings ${ko} |grep -i ^version=" ${ko}
    done
    lsmod | grep aoe 2>&1 > /dev/null
    if [ ${?} -eq 0 ] ; then
        have aoe-stat && run aoe-stat aoe-stat
        linmodinfo
        aoedebugfiles
    fi
}
linmdstat(){
    grab /proc/mdstat
}
linpvs(){
    have pvs && run pvs pvs
}
linnetwork(){
    run 'ifconfig -a' ifconfig-a
    have ethtool && linethtool
}
linethtool(){
    for nic in $(find /sys/class/net/*  -type l ! -name lo |awk -F"/" '{print $5}'); do
        run "ethtool -i ${nic};ethtool -k ${nic}; ethtool -g ${nic} ; ethtool -S ${nic}" ethtool-${nic}
    done
}
linfree(){
    run free free
}
lindmesg(){
    run 'dmesg | tail -n 100' dmesg
}
linsysctl(){
    run "sysctl -a |sort" sysctl-asort
}
aoedebugfiles(){
    for f in /sys/block/etherd*/debug ; do
        name=$(echo ${f}|sed -e 's/.*\!//' -e 's!/!-!')
        if [ "${MODE}" = "tar" ] ; then
            cat ${1} > ${name}
        else
            header ${name}
            cat ${f}
        fi
    done
    [ -e /sys/module/aoe/parameters ] && run 'grep . /sys/module/aoe/parameters/*' module-parameters
}
cdmidecode(){
    have dmidecode && run dmidecode dmidecode
}
lintop(){
    run 'top -b -n1' top
}
chostname(){
    have hostname && run hostname hostname
}

whoisit(){
    have check-hostname && run check-hostname check-hostname
}

#end linux

clspci(){
    have lspci && run "lspci -vM 2>/dev/null" lspci-vM
}

cuptime(){
    run uptime uptime
}

grablog(){
    printf .
    if [ -e /var/log/vmkernel.log ] ; then
        grab /var/log/vmkernel.log
    fi
    if [ -f /var/log/messages ] ; then
        grab /var/log/messages
    fi
    if [ "${me}" = "Linux" ] ; then
        if [ -f /var/log/syslog ] ; then
            grab /var/log/syslog
        fi
    fi
    if [ -f /var/adm/messages ] ; then
        grab /var/adm/messages
    fi
    if [ "${me}" = "VMkernel" ] ; then
        run "ls -l /var/log/" "ls-l-var-log"
    fi
}

package(){
    echo Complete. >&2
    if [ "${MODE}" = "tar" ] ; then
        cd /tmp
        b=$(basename ${tmpdir})
        tar cf ${b}.tar ${b}
        echo Output file is /tmp/${b}.tar >&2
    else
        echo Output file is ${outfile} >&2
    fi
}

stamp(){
    run "echo 'Coraid Diags Version: ${VERSION}'" coraid-diags-version
    run "date -u +%Y-%m-%d\ %H:%M\ %Z" date
    have ntpq && run "ntpq -p" ntpq-p
}

esxdiags(){
    HEADERS=1
    esxversion
    whoisit
    cuptime
    driver
    if [ ${EXTENDED} -eq 1 ] ; then
        targeted
        esxvolume
        esxmpath
        clspci
        wrapmap
        esxscsidevs
        lsvms
        esxmodinfo
    fi
}

soldiags(){
    HEADERS=1
    chostname
    whoisit
    cuptime
    whatsol
    cpackies
    csig
    driver
    if [ ${EXTENDED} -eq 1 ] ; then
        targeted
        soldiskinfo
        zpoollistH
        zfslistsnapshots
        zpoolgetall
        solshare
        solcifsmode
        solsidmap
        solcpuinfo
        solprstat
        cvmstat
        solmemstat
        solirq
        solzfsparams
        solarcstat
        solarcsummary
        soltuning
        solintrstat
        csvcs
        solnetwork
        solcron
        cscanpci
        clspci
        #fmtopo
        cdmidecode
        cprtdiag
        solipmitool
        solfw
        lsiscan
        lsidisks
        solcluster
        rsfdiags
        fmstatus
        fmadmconfig
        cfmfaulty
        cfmdump
        nexall
    fi
}

lindiags(){
    HEADERS=1
    chostname
    whatlinux
    whoisit
    cuptime
    if [ -e /proc/ethdrv ] ; then
        driver
        #targeted
    fi
    linpvs
    linaoe
    linfree
    linmounts
    linmdstat
    if [ ${EXTENDED} -eq 1 ] ; then
        lintop
        cvmstat
        liniostat
        clspci
        lsiscan
        linsysctl
        cdmidecode
    fi
}

me=$(uname)

[ -e "/bin/vmware" -o -e "/usr/bin/vmware" ] && me="VMkernel"

export $me

case ${me} in
    VMkernel)
        PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
        ;;
    SunOS)
        PATH=/usr/cluster/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/coraid/bin:/opt/HAC/RSF-1/bin
        [ -d /usr/gnu/bin ] && export PATH=/usr/gnu/bin:${PATH}
        for d in /usr/lib/fm/fmd /usr/cluster/bin /opt/HAC/RSF-1/bin /opt/coraid/bin; do
            [ -d ${d} ] && export PATH=${PATH}:${d}:
        done
        ;;
esac

if [ -n "${EXEX}" ] ; then
    stamp
    driver 1>/dev/null
    eval "${EXEX}"
    exit 0
fi

if [ ! "${MODE}" = local ] ; then
    stamp

    case ${me} in
        VMkernel)
            esxdiags
            ;;
        SunOS)
            PATH=/usr/cluster/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/coraid/bin:/opt/HAC/RSF-1/bin
            [ -d /usr/gnu/bin ] && export PATH=/usr/gnu/bin:${PATH}
            merooty
            soldiags
            ;;
        Linux)
            merooty
            lindiags
            ;;
    esac
    if [ ${GRABMSGS} -eq 1 ] ; then
        grablog
    fi

    package
else
    targeted
fi

