#!/bin/sh
##
## Show the UNIX OS information
## Copyright (c) 2006-2015 SATOH Fumiyasu @ OSS Technology Corp., Japan
##               <http://www.OSSTech.co.jp/>
##
## License: GNU General Public License version 2 or later
## Date: 2015-05-12, since 2006-02-14
##

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

renice 1 "$$" >/dev/null 2>&1 || :
ionice -c 2 -n 5 -p "$$" >/dev/null 2>&1 || :

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()
{
  echo_title "$@"
  echo "---- command type: `type "$1" 2>&1`"
  "$@" 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 lscpu
  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 -b
  info_run prtconf -D
  info_run prtconf -p
  info_run cfgadm -alv
  info_run modinfo
  info_run psrset
  info_run sysdef
  info_cat /etc/system
  ;;
AIX)
  info_run oslevel
  info_run oslevel -rq
  info_run oslevel -sq
  info_run lscfg -vp
  lscfg |while read indicator resource dummy; do
    [ X"$indicator" = X"+" ] && info_run lsattr -E -l "$resource"
  done
  info_run prtconf
  info_run prtconf -v
  info_run smtctl
  info_run vmo -a
  info_run ioo -a
  info_run schedo -a
  info_run raso -a
  info_run lvmo -a
  ;;
*)
  FIXME
  exit 1
  ;;
esac

info_run rpm -qa --last
case "$uname" in
Linux)
  if [ -f /etc/debian_version ]; then
    info_run env COLUMNS=200 dpkg -l
  else
    info_run chkconfig --list
  fi
  if type systemctl >/dev/null 2>&1; then
    info_run systemctl --all --no-pager
    info_run systemctl list-unit-files --no-pager
    info_run systemd-cgls --all --full --no-pager
    info_run journalctl --boot --catalog --no-pager
  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 -a
  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
  ;;
AIX)
  info_run lssrc -a
  info_run lslpp -L all
  info_run lslpp -h all
  info_run /usr/sbin/sendmail -bv -d0.10
  ;;
esac

info_run who -r
info_cat /etc/inittab
info_cat /etc/rc.local
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

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

case "$uname" in
Linux)
  info_run sestatus
  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
  info_run pstree -acpu
  ;;
*BSD)
  info_run sysctl -a
  info_cat /etc/auth.conf
  ;;
SunOS)
  info_run kstat
  info_run swap -s
  info_eval 'echo ::kmastat |mdb -k'
  ;;
AIX)
  info_run svmon -O unit=KB -@ ALL
  info_run swap -l
  info_run swap -s
  info_run lsps -a
  info_run lsps -s
  ;;
*)
  ;;
esac
info_run getconf -a

find /etc/cron* /var/spool/cron -type f -print 2>/dev/null \
  |while read cronfile; do
    info_cat "$cronfile"
  done

info_run ipcs -a
if ps a >/dev/null 2>&1; then
  ## BSD-style
  info_run ps auxwwww
  ## Dump environment variables after command-line (e)
  info_run ps auxwwwwe
elif [ -x /usr/ucb/ps ]; then
  ## BSD-style
  info_run /usr/ucb/ps auxwwww
  ## Dump environment variables after command-line (e)
  info_run /usr/ucb/ps auxwwwwe
fi

case "$uname" in
Linux)
  info_run ps -eFl --forest
  ;;
SunOS)
  if ps -Z >/dev/null 2>&1; then
    ## -Z for zones
    info_run ps -efjlyZ
  else
    info_run ps -efjly
  fi
  ;;
AIX)
  if ps -@ >/dev/null 2>&1; then
    ## -@ for WPARs
    info_run ps -AflXZ@
  else
    info_run ps -AflXZ
  fi
  info_run vmstat -v
  ;;
esac

## 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
  ## Activity for each block devic
  info_run sar -d
  ;;
SunOS)
  ## Paging statictics
  info_run sar -p
  ;;
AIX)
  ## FIXME
  ;;
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
  ;;
AIX)
  info_run errpt -a
  ;;
*)
  info_cat /var/log/messages
  ;;
esac
info_cat /var/log/syslog

## daemontools multilog
ls /service/*/log/main/current 2>/dev/null |while read log; do
  info_cat "$log"
done |{ type tai64nlocal >/dev/null 2>&1 && tai64nlocal || cat; }

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_cat /proc/sys/fs/file-max /proc/sys/fs/file-nr
  info_run lsblk --all -o NAME,TYPE,FSTYPE,SIZE,SCHED,UUID,RO,MOUNTPOINT
  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
  info_run repquota -auv
  info_run repquota -agv
  ;;
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
  info_run repquota -av
  ## 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
    ## ZFS version 4 (Solaris 10 8/07)
    info_run zfs get -s local all 2>/dev/null
    ## ZFS version 4 (Solaris 10 8/07)
    info_run zfs history 2>/dev/null
  fi
  ## Zone (Container)
  if type zoneadm >/dev/null 2>&1; then
    info_run zoneadm list -vc
    zoneadm list |while read name; do
      info_run zonecfg -z "$name" info
    done
  fi
  ;;
AIX)
  info_run bootlist -o -m service
  info_run bootlist -o -m normal
  info_run df -k
  info_run df -i
  ## LVM
  lspv
  lsvg |while read vg; do
    info_run lsvg "$vg"
    info_run lsvg -p "$vg"
    info_run lsvg -l "$vg"
    info_run lsvg -l |tail +3 |while read lv dummy; do
      info_run lslv "$lv"
    done
  done
  info_run repquota -auvl
  info_run repquota -agvl
  ## WPAR
  info_run lswpar
  ## FIXME
  ;;
*)
  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)
  info_cat cat /root/anaconda-ks.cfg
  if [ -f /etc/debian_version ]; then
    info_cat /etc/network/interfaces
  else
    info_cat /etc/sysconfig/network
    info_cat /etc/sysconfig/network-scripts/ifcfg-*
    info_cat /etc/sysconfig/network-scripts/route-*
  fi
  if firewall-cmd --state >/dev/null 2>&1; then
    info_run firewall-cmd --get-active-zones
    info_run firewall-cmd --get-default-zone
    info_run firewall-cmd --list-all --permanent
    info_run firewall-cmd --list-all-zones --permanent
    info_run firewall-cmd --list-ports --permanent
    info_run firewall-cmd --list-sources --permanent
    info_run firewall-cmd --list-rich-rule --permanent
    info_run firewall-cmd --direct --get-all-rules
  else
    ## FIXME: Dump nftables info
    info_run iptables -nvL -t filter
    info_run iptables -nvL -t nat
    info_run iptables -nvL -t mangle
    info_run iptables -nvL -t raw
    info_run iptables -nvL -t security
    info_run ip6tables -nvL -t filter
    info_run ip6tables -nvL -t nat
    info_run ip6tables -nvL -t mangle
    info_run ip6tables -nvL -t raw
    info_run ip6tables -nvL -t security
  fi
  info_run netstat -anp
  info_run ip -s link
  info_run ip -s neighbor
  info_run ip addr
  info_run ip maddr
  info_run ip route
  info_run ip mroute
  info_run ip rule
  info_run ip tunnel
  info_run ss -aeiomp
  info_run nmcli device show
  info_run nmcli connection show
  ;;
SunOS)
  info_cat /etc/nodename
  info_cat /etc/hostname.*
  info_cat /etc/inet/ipnodes
  info_run ldapclient list
  info_run dladm show-link
  info_run dladm show-dev
  for ifname in `dladm show-dev -p |sed -n 's/ .*//p'`; do
    dladm show-dev -s "$ifname"
  done
  ndd -get /dev/ip '?' |sed 's/(.*//' |while read name; do
    [ X"$name" = X"?" ] && continue
    ndd -get /dev/ip "$name"
  done
  ndd -get /dev/ip6 '?' |sed 's/(.*//' |while read name; do
    [ X"$name" = X"?" ] && continue
    ndd -get /dev/ip6 "$name"
  done
  info_run netstat -an
  info_run routeadm
  info_cat /etc/defaultrouter
  ## NIS/NIS+ domainname
  info_run domainname
  ## FIXME: Correct?
  info_run nisls -l org_dir
  ;;
AIX)
  info_cat /etc/filesystems /etc/netsvc.conf
  info_run netstat -an
  info_run domainname
  ;;
*)
  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
  ;;
AIX)
  info_run nfso -a
  info_run exportfs
  ;;
*)
  info_run exportfs
  ;;
esac

info_run showmount -e
info_run showmount -d
info_run nfsstat

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

## Mailbox
if [ -d /var/mail ]; then
  mbox_dir="/var/mail"
elif [ -d /var/spool/mail ]; then
  mbox_dir="/var/spool/mail"
fi
if [ -n "${mbox_dir:-}" ]; then
  echo_title "Mailbox usage - size (kBytes)"
  du -sk "$mbox_dir"/* |sort -nr
  echo_title "Mailbox usage - number of messages"
  for mbox in "$mbox_dir"/*;do
    printf '%10d %s\n' `grep "^From " "$mbox" |wc -l` "$mbox"
  done \
  |sort -nr \
  ;
fi

echo_title "Maildir usage - size (kBytes)"
getent passwd |cut -d: -f6 \
  |sort -u \
  |while read home; do
    [ -d "$home/Maildir" ] && echo "$home/Maildir"
  done \
  |xargs du -sk \
  |sort -nr \
  ;
echo_title "Maildir usage - number of messages"
getent passwd |cut -d: -f6 \
  |sort -u \
  |while read home; do
    [ -d "$home/Maildir" ] && echo "$home/Maildir"
  done \
  |while read maildir; do
    printf '%10d %s\n' `find "$maildir" -type f |wc -l` "$maildir"
  done \
  |sort -nr \
  ;

## FIXME: Printer
info_run lpstat -t -l

## FIXME: NTP
info_run ntpq -p -n
case "$uname" in
SunOS)
  info_cat /etc/inet/ntp.conf
  ;;
*)
  info_cat /etc/ntp.conf
  info_cat /etc/ntp/ntpservers
  ;;
esac

echo_title Done.

