#!/usr/bin/ksh
#
# CG
# This script collects lots of information about an AIX system.
#

PATH=/usr/bin:/usr/sbin:/etc
export ubin=/usr/bin
export usbin=/usr/sbin
export PATH=/usr/lpp/ssp/bin:/usr/lpp/ssp/kerberos/bin:$PATH    # PSSP
export PATH=/usr/lpp/ssp/rcmd/bin:$PATH                         # Special for PSSP rsh (Kerberised)
export PATH=$PATH:$HOME/bin                                     # Generic
export PATH=$PATH:/usr/local/bin:/usr/local/bin/X11             # Local code
export WCOLL=/sp/etc/wcoll.nodes				# SP working collective

scriptname=$0
chksyslog=/tmp/cg/AIXinfo
hostname=$(hostname -s)
date=$(date +%d%h%Y)
reposhost=hxnim1_priv
reposdir=/usr/local/repos
reposid=ops

mkdir -p /tmp/cg/AIXinfo/pre
mkdir -p /tmp/cg/AIXinfo/post
mkdir -p /tmp/cg/AIXinfo/archive/pre
mkdir -p /tmp/cg/AIXinfo/archive/post

#chgrp repos /tmp/cg/AIXinfo
#chgrp repos /tmp/cg/AIXinfo/pre
#chgrp repos /tmp/cg/AIXinfo/post
#chgrp repos /tmp/cg/AIXinfo/archive
#chgrp repos /tmp/cg/AIXinfo/archive/pre
#chgrp repos /tmp/cg/AIXinfo/archive/post

chmod 750 /tmp/cg/AIXinfo
chmod 750 /tmp/cg/AIXinfo/pre
chmod 750 /tmp/cg/AIXinfo/post
chmod 750 /tmp/cg/AIXinfo/archive
chmod 750 /tmp/cg/AIXinfo/archive/pre
chmod 750 /tmp/cg/AIXinfo/archive/post

chmod g+s /tmp/cg/AIXinfo
chmod g+s /tmp/cg/AIXinfo/pre
chmod g+s /tmp/cg/AIXinfo/post
chmod g+s /tmp/cg/AIXinfo/archive
chmod g+s /tmp/cg/AIXinfo/archive/pre
chmod g+s /tmp/cg/AIXinfo/archive/post

case $1 in
	 -pre) check=pre
         ;;
        -post) check=post
         ;;
      -verify) check=verify 
         ;;
      -archive) check=archive
         ;;
      -repos) check=repos
         ;;
           *) echo "Invalid option. Please user either -pre, -post, -verify, -archive or -repos."
         ;;
esac
#STARTING to collect information.
chksys()
{
    echo "$hostname SYSTEM CONFIGURATION INFORMATION. $date \n"

    echo "UPTIME.\n"
    uptime
    echo

    echo "OSLEVEL INFO.\n"
    oslevel 
    oslevel -r
    oslevel -s
    instfix -i | grep AIX
    instfix -i | grep SP
    echo
    echo Installed IFIXes.
    emgr -l
    echo
    echo Time Zone Info.
    echo $TZ
    grep TZ /etc/environment
    echo "\n"

    echo "LICENSE INFO.\n"
    if [[ -x /usr/bin/lslicense ]] ; then
      /usr/bin/lslicense 
    fi
    echo "\n"

    echo "LSCONF OUTPUT.\n"
    if [[ -x /usr/sbin/lsconf ]] ; then
       /usr/sbin/lsconf
    fi
    echo "\n"

    echo "SMT OUTPUT.\n"
    if [[ -x /usr/sbin/smtctl ]] ; then
       /usr/sbin/smtctl
    fi
    echo "\n"

    echo
    echo "HMC INFO."   
    lsrsrc IBM.MCP
    echo

    echo "LPARSTAT OUTPUT.\n"
	     if [[ -x /usr/bin/lparstat ]] ; then
		         /usr/bin/lparstat -i
					    fi
						     echo "\n"

    echo "LSSRAD INFO.\n"
    /usr/sbin/lssrad -av
    echo "\n"

    echo "IPL BOOTABLE DISK INFO.\n"
    /usr/sbin/ipl_varyon -i
    echo "\n"

    echo "BOOTINFO -R (MEMORY).\n"
    bootinfo -r
    echo "\n"

    echo "BOOTLIST.\n"
    bootlist -m normal -o
    echo "\n"

    echo "ERRPT OUTPUT.\n"
    errpt
    echo "\n"

    echo "FILESYSTEM INFORMATION.\n"
    df -k
    echo "\n/etc/filesystems\n"
    cat /etc/filesystems
    echo "\n"

    echo "MOUNTED FILESYSTEMS.\n"
    mount
    echo "\n"

    echo "FILESYSTEM DETAILS.\n"
    lsfs -q
    echo "\n"

    echo "RPM OUTPUT.\n"
    rpm -qa
    echo "\n"

    echo "LSLPP OUTPUT.\n"
    lslpp -l
    echo
    lslpp -h
    echo 
    lppchk -m3 -v
    echo 
    lppchk -m3 -c
    echo "\n"

    echo "PHYSICAL VOLUMES.\n"
    lspv
    echo "\n"

    echo "USER INFO.\n"
    cat /etc/passwd
    cat /etc/security/passwd
    lsuser -f ALL
    echo "\n"

    echo "GROUP INFO.\n"
    cat /etc/group
    cat /etc/security/group
    lsgroup -f ALL
    echo "\n"

    echo "VOLUME GROUPS.\n"
    lsvg
    echo "\n"

    echo "VOLUME GROUPS FS INFO.\n"
    for i in $(lsvg)
    do
      echo $i:
      lsvgfs $i
      echo
    done
    echo "\n"


    echo "VOLUME GROUPS PV INFO.\n"
    for i in $(lsvg)
    do
      lsvg -p $i
    done
    echo "\n"

    echo "VOLUME GROUP DETAILS.\n"
    lsvg | lsvg -i
    echo "\n"


    echo "VOLUME GROUP LOGICAL VOLUME INFORMATION.\n"
    lsvg | lsvg -il
    echo "\n"

    echo "LOGICAL VOLUME INFORMATION.\n"
    for i in $(lsvg | lsvg -il | egrep -v "LV|:" | awk '{print $1}')
    do
      echo "\n----->$i\n" 
      lslv $i
      echo "\n"
      lslv -l $i
    echo "\n"
    done
 
    echo "VOLUME GROUP BACKUP EXCLUDE LIST.\n"
    for i in $(lsvg)
    do
      echo $i
      cat /etc/exclude.$i
      echo "\n"
    done
    echo "\n"

    echo "PAGING SPACE CONFIGURATION.\n"
    lsps -a
    echo "\n"

    echo "SYSDUMP CONFIGURATION.\n"
    sysdumpdev -l
    echo "\n"


    echo "LSDEV OUTPUT.\n"
    lsdev -CH
    echo "\n"

    echo "LSPATH OUTPUT.\n"
    lspath
    echo "\n"

    echo "KDB VIOS OUTPUT.\n"
    echo cvai | kdb -script 
    echo
    echo vfcs | kdb -script 
    echo "\n"

    echo "LSMPIO OUTPUT.\n"
    lsmpio -q
    echo
    lsmpio -a
    echo 
    lsmpio -are
    echo
    lsmpio
    echo "\n"

    echo "LSPATH VSCSI PATH PRIORITY OUTPUT.\n"
    if lsdev -Cc disk | grep "Virtual SCSI" > /dev/null
    then
    getvscsipaths() {
    vscsidisks=`lsdev -Cc disk | grep "Virtual SCSI" | awk '{print $1}'`
    for iv in `echo $vscsidisks`
    do
    lspath -l $iv -H -F "name:parent:connection:status"
    echo
    done
    }

    getvscsipaths | grep -v name > /tmp/getvscsipaths.out

    for x in `cat /tmp/getvscsipaths.out`
    do
    diskpath=`echo $x | awk -F: '{print $2}'`
    diskvscsi=`echo $x | awk -F: '{print $1}'`
    echo $diskvscsi $diskpath
    echo lspath -AEl $diskvscsi -p $diskpath
    lspath -AEl $diskvscsi -p $diskpath
    echo
    done
    fi 
    echo "\n"

    echo "PCMPATH INFO.\n"
    if [[ -x /usr/bin/pcmpath ]] ; then
      /usr/bin/pcmpath query adapter
     echo
     /usr/bin/pcmpath query device
     echo
    fi
    echo "\n"

    echo "LSVPCFG OUTPUT.\n"
    if [[ -x /usr/sbin/lsvpcfg ]] ; then
      /usr/sbin/lsvpcfg
    fi
    echo "\n"

    echo "DATAPATH INFO.\n"
    if [[ -x /usr/bin/datapath ]] ; then
      /usr/bin/datapath query adapter
     echo
     /usr/bin/datapath query device
     echo
    fi
    echo "\n"

    echo "LS2105 OUTPUT.\n"
    if [[ -x /usr/bin/ls2105 ]] ; then
       /usr/bin/ls2105
    fi
    echo "\n"

    echo "LSESS OUTPUT.\n"
    if [[ -x /usr/bin/lsess ]] ; then 
       /usr/bin/lsess
    fi
    echo "\n"
 
    echo "LSSDD OUTPUT.\n"
    if [[ -x /usr/bin/lssdd ]] ; then
      /usr/bin/lssdd
    fi
    echo "\n"

    echo "LSCFG OUTPUT.\n"
    lscfg -vp
    echo "\n"
  
    echo "LSSRC OUTPUT.\n"
    lssrc -a | grep active | awk '{print $1,$2,$4}'
    lssrc -a | grep inoperative | awk '{print $1,$2,$3}'
    echo "\n"

    echo "LSATTR OUTPUT.\n"
    for i in $(lsdev -C | awk '{print $1}')
    do
      echo "\n----->$i\n"
      lsattr -El $i
    done
    echo "\n"

    echo "NO OUTPUT.\n"
    no -a
    echo "\n"
    
    echo "NETWORK INFO.\n"
    netstat -ni
    echo "\n"
    lsof -i
    echo "\n"
    netstat -nr 
    echo "\n"
    netstat -m
    echo "\n"
    ifconfig -a
    echo "\n"
    showmount -e
    echo "\n"
    echo "/etc/hosts:\n"
    cat /etc/hosts
    echo "\n"
    echo "/etc/resolv.conf:\n"
    cat /etc/resolv.conf
    echo "\n"
    echo "/etc/netsvc.conf:\n"
    cat /etc/netsvc.conf
    echo "\n"
    echo "/etc/services:\n"
    cat /etc/services
    echo "\n"
    echo "/etc/inetd.conf:\n"
    cat /etc/inetd.conf
    echo "\n"

    echo "INITTAB.\n"
    echo "/etc/inittab:\n"
    lsitab -a 
    echo "\n"
   
    echo "PRINTER INFO.\n"
    
    cat /etc/qconfig
    echo
    lpstat
    echo
    lsallq
    echo 
    for i in `lsallq`
    do
      lsque -cq $i
      echo  
      lsque -q $i
      echo
      lsallqdev -q$i
      echo 
      lsallqdev -cq $i
      echo
    done 

    
    echo "NIM.\n"
    lsnim -l
    echo "\n"

    echo "WLM STATUS INFO.\n"
    if [[ -x /usr/sbin/wlmcntrl ]] ; then
       /usr/sbin/wlmcntrl -q
       /usr/sbin/wlmstat -a
    fi

    echo "ETC TUNABLES INFO.\n"
    cat /etc/tunables/nextboot
    echo "\n"

    echo "VMTUNE INFO.\n"
    if [[ -x /usr/samples/kernel/vmtune ]] ; then
       /usr/samples/kernel/vmtune
    fi

    echo "SCHEDTUNE INFO.\n"

    if [[ -x /usr/samples/kernel/schedtune ]] ; then
       /usr/samples/kernel/schedtune
    fi
    echo "\n"

    echo "VMO INFO.\n"
    if [[ -x /usr/sbin/vmo ]] ; then
       /usr/sbin/vmo -L
    fi
    echo "\n"

    echo "IOO INFO.\n"
    if [[ -x /usr/sbin/ioo ]] ; then
       /usr/sbin/ioo -L
    fi
    echo "\n"

    echo "SCHEDO INFO.\n"
    if [[ -x /usr/sbin/vmo ]] ; then
       /usr/sbin/schedo -L
    fi
    echo "\n"

    echo "ROOT CRONTAB ENTIRES.\n"

    if [[ -x /usr/bin/crontab ]] ; then
       /usr/bin/crontab -l
    fi
    echo "\n"

   echo "INSTFIX INFO.\n"
   if [[ -x /usr/sbin/instfix ]] ; then
     /usr/sbin/instfix -i
    echo
    /usr/sbin/instfix -iv
   fi
   echo "\n"

    echo "SP SPECIFIC INFO."
    if [[ -x /usr/local/bin/what_nodenum ]] ; then
      echo "Node number = $(/usr/local/bin/what_nodenum)" 
     if [[ -x /usr/lpp/ssp/bin/SYSMAN_test ]] ; then
       /usr/lpp/ssp/bin/SYSMAN_test
       echo "\n"
     fi 

     if [[ -x /usr/lpp/ssp/bin/CSS_test ]] ; then
       /usr/lpp/ssp/bin/CSS_test
       echo "\n"
     fi 
    
     if [[ -x /usr/lpp/ssp/bin/dsh ]] ; then
       /usr/lpp/ssp/bin/dsh date
       echo "\n"
     fi 

    else
     echo "Not an SP node."
     echo "\n"
    fi
    
if [[ -f /usr/sbin/nim ]] ; then
  echo "NIM master infor."
  lsnim 
  lsnim -l
fi 

echo "VIOS SPECIFIC INFO."
echo
echo "Checking if this is a VIOS partition or not. Checking for the existence of a padmin user...."
echo
if lsuser padmin ; then
echo
echo "This appears to be a VIOS partition."
echo
echo "VIOS LEVEL."
su - padmin -c ioscli ioslevel
echo
echo "LSMAP ALL."
su - padmin -c ioscli lsmap -all
echo
echo "LSMAP ALL NET."
su - padmin -c ioscli lsmap -all -net
echo
echo "LSDEV TYPE ADAPTER."
su - padmin -c ioscli lsdev -type adapter
echo
echo "LSVOPT."
su - padmin -c ioscli lsvopt
echo
echo "LSREP."
su - padmin -c ioscli lsrep
echo
echo "LSMAP ALL NPIV."
su - padmin -c ioscli lsmap -all -npiv
echo
echo "LSPV."
su - padmin -c ioscli lspv
echo
echo
echo "VIOS RULES."
su - padmin -c rules -o diff -d
echo
su - padmin -c rules -o diff -d -s
echo
su - padmin -c rules -o diff -s
echo
su - padmin -c rules -o list -d
echo
su - padmin -c rules -o list
echo
echo
echo "VIOS FIREWALL"
su - padmin -c ioscli viosecure -firewall view
echo
echo "SSP CLUSTER LIST."
su - padmin -c ioscli cluster -list
echo
echo "SSP CLUSTER STATUS."
su - padmin -c ioscli cluster -status
echo
echo "SSP PV LIST."
su - padmin -c ioscli pv -list
echo
echo "SSP LU LIST."
su - padmin -c ioscli lu -list
echo
fi

} 
# FINISHED collecting info.

#Pre Check
prelog=$chksyslog/pre/$hostname.chksys-pre.$date
if [[ "$check" = "pre" ]] ; then
  if [[ -f $prelog ]] ; then
    echo "$prelog for $date already exists!"
    exit 1
  fi

 >$prelog 
 echo "Writing to $prelog"
 chksys >> $prelog 2>&1
 chmod 640 $prelog
 #chgrp repos $prelog
 gzip $prelog
 

 echo "Pre log file created: $prelog.gz"

fi


#Post Check
postlog=$chksyslog/post/$hostname.chksys-post.$date
if [[ "$check" = "post" ]] ; then
  if [[ -f $postlog ]] ; then
    echo "$postlog for $date already exists!"
    exit 1
  fi

  >$postlog
  echo "Writing to $postlog"
  chksys >> $postlog 2>&1
  chmod 640 $postlog
  #chgrp repos $postlog
  gzip $postlog
  echo "Post log created: $postlog."
  echo "Run AIXinfo -verify to check system configuration."
fi

#Verify
if [[ "$check" = "verify" ]] ; then
    gzip -d $prelog.gz 
    gzip -d $postlog.gz
 if [[ -f $prelog || -f $postlog ]] ; then
  echo "Checking $hostname system configuration."
  echo "Comparing PRE and POST system configuration log files:\n"
  echo "PRELOG = $prelog"
  echo "POSTLOG = $postlog"
  echo "\nThe following differences were found:\n"

  if [[ -f $prelog || -f $postlog ]] ; then
    diff $prelog $postlog
    chmod 640 $prelog
    chmod 640 $postlog
    #chgrp repos $postlog
    #chgrp repos $prelog
    gzip $prelog
    gzip $postlog
  else
    echo "No PRE or POST log files for comparison."
  fi
 fi
fi

#Archive
if [[ "$check" = "archive" ]] ; then
  echo "Archiving all pre and post chk_sys log files to $chksyslog/archive."
  for i in pre post
  do
     find $chksyslog/$i -type f -exec mv {} $chksyslog/archive/$i \;
  done
fi

#Repos
if [[ "$check" = "repos" ]] ; then
prelog=$chksyslog/pre/$hostname.chksys-pre.$date
  if [[ -f $prelog ]] ; then
    echo "$prelog for $date already exists!"
    exit 1
  fi

 >$prelog
 echo "Writing to $prelog"
 chksys >> $prelog 2>&1
 chmod 640 $prelog
 #chgrp repos $prelog
 gzip $prelog


  set -x
  echo "Pre log file created: $prelog"
  echo "Copying $prelog to $reposhost:$reposdir."
  su $reposid -c "scp $prelog.gz $reposid@$reposhost:$reposdir"
fi
