#!/bin/sh
##
## Show the UNIX OS information
## Copyright (c) 2006-2007 SATOH Fumiyasu @ OSS Technology Co., Japan
##                         <http://www.osstech.co.jp/>
##
## Date: 2007-07-18, since 2006-02-14
## License: GNU General Public License version 3
##

orig_PATH_exist="`env |grep '^PATH='`"
orig_PATH="$PATH"
orig_LC_ALL_exist="`env |grep '^LC_ALL='`"
orig_LC_ALL="$LC_ALL"
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH"
LC_ALL=C
export PATH LC_ALL

uname="`uname`"

flag_progress=""
cmd_usage="Usage: $0 [OPTIONS]

Options:
 -p
    Print running command to standard error
    e.g. \`$0 -p >unixinfo.txt\`
 -h
    Print this help message
"

while getopts ph opt; do
    case "$opt" in
    p)
	flag_progress="yes"
	;;
    h)
	echo "$cmd_usage"
	exit 0
	;;
    *)
	exit 1
	;;
    esac
done

echo_title()
{
    echo "==== UNIXINFO: `date '+%Y-%m-%d %H:%M:%S'`: $@"
    if [ -n "$flag_progress" ]; then
      echo "UNIXINFO: $@ ..." 1>&2
    fi
}

info_run()
{
    ## FIXME: type "$1"
    echo_title "$@"
    "$@" 2>&1
    echo
}

info_eval()
{
    echo_title "$1"
    sh -c "$1" 2>&1
    echo
}

info_cat()
{
    for _info_cat in "$@"; do
	echo_title "$_info_cat"
	cat  "$_info_cat" 2>&1
	echo
    done
}

echo_title version
sed -n '/^#.*Date: /{s/,.*$//;s/^.* //;p;q;}' "$0"

info_run date '+%Y-%m-%d %a %H:%M:%S %z'
info_run uname -a

echo_title env
env \
    ${orig_PATH_exist:+PATH="$orig_PATH"} \
    ${orig_LC_ALL_exist:+LC_ALL="$orig_LC_ALL"} \
    |sort

info_cat /etc/motd
case "$uname" in
Linux)
    if [ -f /etc/debian_version ]; then
	info_cat /etc/debian_version
    else
	info_cat /etc/*-release
    fi
    info_cat /proc/cpuinfo
    info_run dmidecode
    info_cat /proc/devices /proc/ioports /proc/iomem /proc/interrupts /proc/dma
    info_cat /proc/pci /proc/scsi/scsi
    info_run lspci -vv
    info_run lspci -vvn
    info_run lsusb -vv
    info_run lsmod
    info_cat /etc/modprobe.conf
    info_cat /etc/modules.conf
    if [ -f /etc/debian_version ]; then
	info_cat /etc/modules
	info_cat /etc/modprobe.d/*
    fi
    info_cat /proc/cmdline
    info_cat /etc/modprobe.conf /etc/modules
    ;;
SunOS)
    info_cat /etc/release
    info_run isainfo -v
    info_run hostid
    info_run eeprom
    info_run smbios
    info_run psrinfo -v
    info_run /usr/platform/`uname -i`/sbin/prtdiag -v
    info_run prtconf -D
    info_cat /etc/system
    ;;
*)
    FIXME
    exit 1
    ;;
esac

case "$uname" in
Linux)
    if [ -f /etc/debian_version ]; then
	info_run env COLUMNS=200 dpkg -l
    else
	info_run rpm -qa --last
	info_run chkconfig --list
    fi
    info_cat /etc/ld.so.conf /etc/ld.so.conf.d/*.conf
    ;;
*BSD)
    info_run pkg_info
    info_cat /etc/ld*.so.conf
    info_cat /etc/mail/mailer.conf
    ;;
SunOS)
    info_run pkginfo -l
    info_run showrev -p
    info_run /usr/sbin/named -v
    info_run /usr/lib/sendmail -bv -d0.10
    info_run crle
    info_run crle -64
    info_run svcs -apv
    info_run inetadm
    info_run inetadm -p
    ;;
esac

if [ -d /etc/rc0.d ]; then
    info_run ls /etc/rc?.d
elif [ -d /etc/rc.d/rc0.d ]; then
    info_run ls /etc/rc.d/rc?.d
fi
info_cat /etc/rc.local

for file in /etc/default/* /etc/sysconfig/*; do
    [ -f "$file" ] || continue
    info_cat "$file"
done

case "$uname" in
Linux)
    if [ -f /etc/debian_version ]; then
	info_cat /etc/locale.gen
	## Sarge and older
	info_cat /etc/environment
    fi
    info_run sysctl -a
    info_run free
    info_cat /proc/meminfo
    info_cat /proc/slabinfo
    info_run swapon -s
    info_run ipcs -l
    info_run ipcs -u
    ;;
*BSD)
    info_run sysctl -a
    info_cat /etc/auth.conf
    ;;
SunOS)
    info_run swap -s
    ;;
*)
    ;;
esac
ps -ef 2>/dev/null || ps auxwww
info_run ipcs -a

## FIXME: vmstat 1 5
## FIXME: mpstat 1 5

## CPU utilization
info_run sar -u
## Memory and swap utilization
info_run sar -r
## Status of process, i-node, file tables
info_run sar -v
## Queue length
info_run sar -q
## System switching activity (and swapping on Solaris)
info_run sar -w
case "$uname" in
Linux)
    ## System swapping statictics
    info_run sar -W
    ## Paging statistics
    info_run sar -B
    ;;
SunOS)
    ## Paging statictics
    info_run sar -p
    ;;
esac

info_run dmesg
info_run w
info_run last
case "$uname" in
SunOS)
    info_cat /var/adm/messages
    ;;
Linux)
    info_cat /var/log/dmesg
    info_cat /var/log/boot
    info_cat /var/log/messages
    ;;
*)
    info_cat /var/log/messages
    ;;
esac
info_cat /var/log/syslog

info_cat /etc/*fstab
info_run mount
info_run ls -la /boot
info_cat /boot/grub/menu.lst /boot/grub/device.map
info_cat /etc/lilo.conf
case "$uname" in
Linux)
    info_cat /proc/stat /proc/filesystems /proc/partitions /proc/mounts
    info_run fdisk -l
    info_run df -akl
    info_run df -ail
    ## Software RAID (md)
    info_cat /proc/mdstat
    info_cat /etc/raidtab /etc/mdadm.conf /etc/mdadm/mdadm.conf
    ## LVM
    info_run pvdisplay
    info_run vgdisplay
    info_run lvdisplay
    ;;
SunOS)
    ## Hard disks
    info_run iostat -En
    ## Hard disk paritioning
    iostat -En |grep 'Soft Errors:' |awk '{print $1}' |while read dev; do
	info_run prtvtoc "/dev/rdsk/${dev}s2"
    done
    info_run df -akl
    info_run df -o i
    ## DiskSuite
    info_run metastat
    ## ZFS
    if type zpool >/dev/null 2>&1; then
	info_run zpool list
	info_run zpool status -v
	info_run zpool iostat -v
	info_run zfs list
	zfs list -H |while read name dummy; do
	    info_run zfs get -o property,value,source all "$name"
	done
    fi
    ## Zone (Container)
    if type zoneadm >/dev/null 2>&1; then
	info_run zoneadm list -v
	zoneadm list |while read name; do
	    info_run zonecfg -z "$name" info
	done
    fi
    ;;
*)
    info_run df -k
    info_run df -i
    ;;
esac
info_cat /etc/auto*

info_run ifconfig -a
if type ethtool >/dev/null 2>&1; then
    ifconfig -a |sed -n 's/[: ].*//;/^[a-z]/p' \
    |egrep -v '^(lo[0-9]*|sit[0-9]|vmnet[0-9])$' \
    |while read if; do
	info_run ethtool "$if"
    done
fi
info_cat /etc/hosts /etc/resolv.conf /etc/nsswitch.conf /etc/host.conf /etc/nscd.conf
info_cat /etc/protocols /etc/networks /etc/netmasks /etc/services /etc/rpc
info_cat /etc/*ldap*.conf
info_cat /etc/pam.conf /etc/pam.d/*
info_cat /etc/krb5.conf /etc/krb5/krb5.conf
info_cat /etc/hosts.allow /etc/hosts.deny
info_cat /etc/inetd.conf
case "$uname" in
Linux)
    if [ -f /etc/debian_version ]; then
	info_cat /etc/network/interfaces
    else
	info_cat /etc/sysconfig/network
    fi
    info_run iptables -nLv -t filter
    info_run iptables -nLv -t nat
    info_run iptables -nLv -t mangle
    info_run iptables -nLv -t raw
    info_run netstat -anp
    ;;
SunOS)
    info_cat /etc/inet/ipnodes
    info_run ldapclient list
    info_run dladm show-link
    info_run dladm show-dev
    info_run netstat -an
    ## NIS/NIS+ domainname
    info_run domainname
    ## FIXME: Correct?
    info_run nisls -l org_dir
    ;;
*)
    info_run netstat -an
    ;;
esac
info_run netstat -rn
info_run netstat -in
info_run netstat -s
if type lsof >/dev/null 2>&1; then
    info_run lsof -b -n -l -R +c0 +M
fi

info_run rpcinfo -p
case "$uname" in
SunOS)
    info_run share
    ;;
*)
    info_run exportfs
    ;;
esac

## FIXME
info_eval 'getent passwd |wc -l'
info_eval 'getent group |wc -l'
info_run wc -l /etc/passwd /etc/group

## FIXME: Printer
info_run lpstat -t -l

## FIXME: NTP
info_run ntpq -p -n

info_run ntpq -p -n

echo_title done.

