#!/bin/ksh 
###########################################################################
# $Header: /cvsstore/sunpsc/SCIC/scic,v 5.37 2010/02/10 23:04:02 nl128925 Exp $
###########################################################################
#
# (Script Specific Information)
#       Script Name..........SCIC (Server Critical Information Compiler)
#       Author...............Spencer Chappell   - spencer.chappell@uk
#                            Fergus Maynes      - fergus.maynes@uk
#       Updated by...........David Dixon        - david.dixon@central
#                            Robert Sutcliffe   - robert.sutcliffe@central
#                            Rob Kolb           - rob.kolb@holland
#                            Mike Beddows       - michael.beddows@sun.com
#                            Norman Lyon        - norman.lyon@sun.com
#       Purpose..............To accumulate the information required to restore
#                            a server to it's exact state following a major
#                            crash, fire etc....
#       OS Requirements......Solaris 2.n, SunOS
#       Date Written.........1995
#       Date Revised.........see ident line
# (Updates)
#       20 May 1997                                             Rob Kolb
#               Source one shared parameter file
#               Re-arrange mailheader
#               Added DiskSuite and VxVm info
#               Added Storage Array info
#               Changed disk-properties to use 'inquiry'
#               Added some prtdiag-stuff to determine system-type
#               Re-organized output
#       2 March 1999                                            Robert Sutcliffe
#               Removed shared parameter file (script is no longer part of package)
#               Removed mail capability, as output will be placed on Web (secure)
#               "inquiry" program doesn't work with RSM, and Photons (ser no. includes control characters)
#               Added "adf" program output as a replacement, along with a change to the "format" section
#               to list the disks
#               Also, formatted the output of "showrev -p" (patch numbers and revisions)
#       28 June 2001 - Mike Beddows
#               Added ability to grab serialport and footprint info from serialports NIS map (only for
#               SWAN servers).
#       15 August 2001 - Mike Beddows
#               Added savecore information (only for swan servers for now).
#               Fixed metastat reporting errors for SCIC.
#       07 October 2002 - Mike Beddows
#               Modified the way the script looks for SDS binaries
#               Added #sof and #eof for troubleshooting incompleted scics
#               Setup versioning
#               Using mirror account to update off SWAN servers
#               Added ssh configuration files - sunclient.cfg and sshd_confg
#               Added SunMC and CST configuration/preference files
#               Added eeprom info
#               Removed "-l" flag for pkginfo
#               Made output more readable
#       15 January 2003 - Mike Beddows
#               Updated for VXVM 3.5
#               Added /etc/sudoers
#       04 February 2003 - Mike Beddows 3.1.2
#               readded "pkginfo -l" per team meeting (vote 6:3) 02/04/2003
#       13 February 2003 - Mike Beddows 3.1.3
#               Added output of /etc/inet/ntpd.conf - RFE submitted by Geoff Lassner
#       25 February 2003 - Mike Beddows 3.1.4
#               Added commands to section headers - RFE submitted by Zulfikhar Mohammed
#               Added `ls -l /usr/safword/sid/bin - RFE submitted by Geoff Lassner
#               Added lustatus command output
#               Added `cat /etc/release` - RFE submitted by Norman Lyon
#               Added SunCluster info
#               Disabled adf command
#               Reorganized SCIC output
#       02 April 2003 - Mike Beddows 3.1.5
#               Added "uptime" and process table
#       16 April 2003 - Mike Beddows 3.1.6
#               Modified how SCIC pulls initial IP - per request by Geoff L
#       22 July 2003 - Mike Beddows 3.1.7
#               Updated for Solaris 9 SVM
#               Modified domain pull information
#               Readded adf per SDT 2690510
#               Cleaned up Veritas version output
#               Moved and renamed "current domain" to "Domain Name" after "Machine Name"
#       25 Sep 2003 - Mike Beddows 3.1.8
#               -added "-v" to netstat command
#       11 Oct 2003 - MIke Beddows 3.1.9
#               Added uname -a, isainfo -kv
#       16 Sep 2004 - Mike Beddows 3.1.10
#               Added zone config info - per RFE submitted by Norman Lyon
#	17 Sep 2004 - Mike Beddows 4.0
#		Rewrote in korn shell
# 		made everything functions for ease of use,update,etc
#		imbedded ability to update crontab
#		added command line arguments
#		added ability to overwrite defaults for those stubborn, old off-swan servers
#		basically changed a lot of code to make it cleaner and more efficient
#		incorporated into N1 SPS, but can still be run on command line
#       11 Oct 2004 - Mike Beddows 4.0.1
#		made some minor corrections to the code (closed security hole)
#       21 Jul 2005 - Norman Lyon
#               updated SVM functions to not grep out of
#               /var/sadm/install/contents since Sun is calling this file
#               unstable, and may remove it - Sun wants us to use pkgchk.
#               Updated SVM functions to query disk sets
#               Added /etc/mail/aliases to the list of files to cat
#               Added SMF info
#       28 Jul 2005 - Norman Lyon
#               added RBAC files (/etc/user_attr /etc/exec_attr /etc/prof_attr)
#       28 Oct 2005 - Norman Lyon
#               added in standard 'pkginfo' prior to 'pkginfo -l'.
#               mailed SCIC reports will be sent out using gpg with key for
#               scic-report@brmpsca.
#       31 Oct 2005 - Norman Lyon
#               updated getDomainName so it will pick out domain names based on
#               first 3 octets of IP, if the other methods fail.  Same code
#               also sets where SCIC should be sent.  This required function
#               to be called earlier in script.
#        2 Jan 2006 - Norman Lyon
#               add execution of raidctl, if it exists, and RAID volumes are
#               found
#       30 Jan 2006 - Norman Lyon
#               add time_out function to luxadm calls
#               change calls to format (to grab partition tables) to call
#               format $DISK, not format and then select disk.  Should speed
#               up matters when a dead disk is seen since it won't be scanned
#               when we want the partition tables for each disk.
#        1 Feb 2006 - Norman Lyon
#               add time_out function call to the raidctl call....  it looks
#               like some systems hung from raidctl.....
#               require that prtconf has at least 1 instance of regexp
#               "LSI Logic 10[23]0" before executing raidctl command
#        7 Feb 2006 - Norman Lyon
#               add '-V' option to echo out the version of SCIC
#        9 Mar 2006 - Norman Lyon
#               Dan Romike wants the df calls to not send stderr to /dev/null
#       11 Apr 2006 - Norman Lyon
#               code to identify domain was grepping on hostname binary in
#               /etc/hosts, not the output from the hostname command...
#               add get_cluster_setup.sh to scic for clustered systems
#       24 May 2006 - Norman Lyon
#               capture ndd settings for all plumbed interfaces.
#               try to save cluster config info only if cluster config file
#               was created.
#	 7 Dec 2006 - Norman Lyon
#		add gmp03-infra-01.uk for hosts in .uk
#       12 Dec 2006 - Norman Lyon
#		update code for ndd to grab from physical interface, not VLAN
#		interface
#       23 Mar 2007 - Norman Lyon
#               update to grab ZFS information
#               add 'netstat -in'
#               add /usr/openv/netbackup/bin/version /etc/default/kbd
#               grab ALOM version info
#       26 Mar 2007 - Norman Lyon
#               don't use gpg if if bombs out - this will mean un-encrypted
#               mail, but it's better than no mail at all
#       30 Mar 2007 - Norman Lyon
#               update zoneadm list to include '-i' option
#               add 'prsinfo -p' for Dan's billing reports
#               updated code to execute dumpadm on all Solaris versions other
#               than 4*|5.5*|5.6 if dumpadm is executable
#               add /etc/cluster/release to files catted
#               'sendmail -bp' & 'sendmail -bp -Ac' (S9+)
#               Some MStor stuff from /home/support/san
#       23 Apr 2007 - Norman Lyon
#               chmod output file to 0400
#               change umask to 0377
#               add fcinfo hba_map into runGetSanConfig
#        8 May 2007 - Norman Lyon
#               add luxadm probe -> luxadm display $DEV
#        7 Jun 2007 - Norman Lyon
#               add 192.18.[67] as subnets in UK in eu.sun.com domain
#       19 Jun 2007 - Norman Lyon
#               add /kernel/drv/md.conf
#        5 Jul 2007 - Norman Lyon
#               add dladm show-(aggr|dev|link)
#       18 Jul 2007 - Norman Lyon
#               add a few more UK subnets
#       24 Jul 2007 - Norman Lyon
#               grab some info for SAMFS/QFS
#       16 Oct 2007 - Norman Lyon
#               move th-elwn01-01.holland to gmp03-infra-01.uk
#        3 Jan 2008 - Norman Lyon
#               change mail addresses for SCIC reports to go to scic-$GEO
#        6 Mar 2008 - Norman Lyon
#               execute raidctl all the time, regardless of type of controller
#       10 Apr 2008 - Norman Lyon
#               add 129.147.39 as BRM subnet
#       15 Apr 2008 - Norman Lyon
#               add 129.147.13[89] as BRM subnets
#       22 Apr 2008 - Norman Lyon
#               run sccheck with option '-v2'
#       29 Jul 2008 - Norman Lyon
#               add in subnets for 192.18.128 (CLT), 129.156.1[6-9] (GMP),
#               129.147.20[2-9] (BRM)
#	11 Nov 2008 - Rex Featherston
#		add in subnets for 129.147.11[468] (BRM) and 129.147.120 (BRM)
#       27 Mar 2009 - Norman Lyon
#               add in 'zpool get all $pool' for all online pools
#       30 Mar 2009 - Norman Lyon
#               Randall wants 'zpool history $pool' added in.....
#       30 Nov 2009 - Norman Lyon
#               Chris noticed that psrinfo -(p|v) was always doing psrinfo -p,
#               and OS less than S10 don't support the '-p'....
#       10 Feb 2010 - Norman Lyon
#               Change 'fcinfo hba_map' to 'fcinfo hba-port'
#
##############################################################################################

#===================#
# VARIABLES         #
#===================#

#### provide means of overriding variables w/out affecting main script
###if [ -s $overridefile ] ; then . $overridefile; fi

# command variables
awk=/usr/bin/nawk
basename=/usr/bin/basename
cat=/usr/bin/cat
cfgadm=/usr/sbin/cfgadm
chmod=/usr/bin/chmod
chown=/usr/bin/chown
crontab=/usr/bin/crontab
cut=/usr/bin/cut
date=/usr/bin/date
dc=/usr/bin/dc
df=/usr/bin/df
dfmounts=/usr/sbin/dfmounts
dirname=/usr/bin/dirname
dladm=/usr/sbin/dladm
dumpadm=/usr/sbin/dumpadm
echo=/usr/bin/echo
eeprom=/usr/sbin/eeprom
egrep=/usr/bin/egrep
fcinfo=/usr/sbin/fcinfo
find=/usr/bin/find
format=/usr/sbin/format
grep=/usr/bin/grep
hastat=/opt/SUNWcluster/bin/hastat
head=/usr/bin/head
hostname=/usr/bin/hostname
hostid=/usr/bin/hostid
ifconfig=/usr/sbin/ifconfig
iostat=/usr/bin/iostat
isainfo=/usr/bin/isainfo
kill=/usr/bin/kill
ls=/usr/bin/ls
lustatus=/usr/sbin/lustatus
luxadm=/usr/sbin/luxadm
mailx=/usr/bin/mailx
metadb=/usr/sbin/metadb
metastat=/usr/sbin/metastat
metaset=/usr/sbin/metaset
mkdir=/usr/bin/mkdir
ndd=/usr/sbin/ndd
netstat=/usr/bin/netstat
perl=/usr/bin/perl
perldist=/usr/dist/share/perl,v5.003/5bin.sun4/perl
pkgchk=/usr/sbin/pkgchk
pkginfo=/usr/bin/pkginfo
pnmstat=/usr/cluster/bin/pnmstat
prstat=/usr/bin/prstat
ps=/usr/bin/ps
psrinfo=/usr/sbin/psrinfo
pwd=/usr/bin/pwd
raidctl=/usr/sbin/raidctl
rm=/usr/bin/rm
samsharefs=/opt/SUNWsamfs/sbin/samsharefs
san_chkpathk=/home/support/san/chkpathk.ksh
san_portmap=/home/support/san/portmap
scstat=/usr/cluster/bin/scstat
sccheck=/usr/cluster/bin/sccheck
sed=/usr/bin/sed
sendmail=/usr/lib/sendmail
share=/usr/sbin/share
showrev=/usr/bin/showrev
sleep=/usr/bin/sleep
sort=/usr/bin/sort
ssaadm=/usr/sbin/ssaadm
ssacli=/opt/SUNWssa/bin/ssacli
svcs=/usr/bin/svcs
swap=/usr/sbin/swap
sysdef=/usr/sbin/sysdef
tail=/usr/bin/tail
tee=/usr/bin/tee
test=/usr/bin/test
tr=/usr/bin/tr
ypmatch=/usr/bin/ypmatch
uname=/usr/bin/uname
uptime=/usr/bin/uptime
vxdg=/usr/sbin/vxdg
vxdisk=/usr/sbin/vxdisk
vxlicense=/usr/sbin/vxlicense
vxlicrep=/sbin/vxlicrep
vxprint=/usr/sbin/vxprint
vxserial=/usr/sbin/vxserial
wc=/usr/bin/wc
which=/usr/bin/which
who=/usr/bin/who
zoneadm=/usr/sbin/zoneadm
zonecfg=/usr/sbin/zonecfg
zpool=/usr/sbin/zpool
zfs=/usr/sbin/zfs

scadm=/usr/platform/`$uname -i`/sbin/scadm

# standard variables
filedate=`$date '+%Y%m%d%H%M'`
date=`$date`
home=`$dirname $0`; if [[ $home = "." ]]; then home=`$pwd`; fi
host=`$hostname`
hostid=`$hostid`
myname=`$basename $0`
tmpdir="$home/tmp"; test -d "$home/tmp" || $mkdir $home/tmp
# version of this script - CVS updates this automatically
version=`echo '$Revision: 5.37 $'|cut -f2 -d' '`

# default variables
keepscic=7
defcron="15 18 * * 4"
supportaddress=scic-sa@scic-central.central.sun.com
overridefile=$home/.scicrc
scicoutputfile=$home/$myname.$hostid.$host.$filedate
timeout=300
searchon="#####"

# initialise temporary files used to hold information during the script
nddlog=$tmpdir/nddlog
disklog=$tmpdir/disklog
partlog=$tmpdir/partlog
partlog2=$tmpdir/partlog2
critical_errors=$tmpdir/critical_errors
sysinfo=$tmpdir/sysinfo
get_cluster_setup=$home/scripts/get_cluster_setup.sh

#===================#
# FUNCTIONS         #
#===================#

# determine host's domain
runGetDomain() {
   # Try /etc/resolv.conf
   if [[ "$domain" = "" && -s "/etc/resolv.conf" ]]; then 
      domain=`$cat /etc/resolv.conf | $egrep -v '#|;' | $egrep '^(domain|search)' |$awk '{print $2}' | $awk -F. '{i=NF} {OFS="."} {if (i>2) {x=i-2 ; y=i-1 ; z=i; print $x,$y,$z} else {x=i-1;y=i;print $x,$y}}' | $tr "[A-Z]" "[a-z]" | head -1`
   fi
   # Try /etc/defaultdomain
   if [[ "$domain" = "" && -s "/etc/defaultdomain" ]]; then
      domain=`$cat /etc/defaultdomain | $grep -v \# | $awk -F. '{i=NF} {OFS="."} {if (i>2) {x=i-2 ; y=i-1 ; z=i; print $x,$y,$z} else {x=i-1;y=i;print $x,$y}}' | $tr "[A-Z]" "[a-z]"`
   fi

   case `$egrep -v '(^#|127.0.0.1)' /etc/hosts | $grep "^[0-9].*${host}" | $head -1 | $awk -F. '{print $1"."$2"."$3}'` in
     10.2.*|10.1.*|10.12.*|172.19.*|10.8.*|129.147.13[0-9]|172.20.*|129.147.53|207.127.*|129.147.3[0-9]*|129.147.[67]|192.18.13[24]|192.18.9[79]|192.29.115|129.147.5[89]|129.147.60|192.18.10[28]|129.147.15[6-8]|192.29.15|129.147.253|129.147.20[2-9]|129.147.11[468]|129.147.120)
       [[ -z $domain ]] && domain=central.sun.com
       reportaddress=scic-report@scic-central.central.sun.com ;;
     129.152.*|192.18.12[89]|192.18.13[24]|192.29.16|192.29.118)
       [[ -z $domain ]] && domain=east.sun.com
       reportaddress=scic-report@scic-east.east.sun.com ;;
     129.158.3[17]|192.29.20)
       [[ -z $domain ]] && domain=japan.sun.com
       reportaddress=scic-report@scic-japan.japan.sun.com ;;
     10.12.2|129.158.12[2345]|129.158.7[145-6]|192.29.19|192.168.125|192.18.1[89]|192.18.200)
       [[ -z $domain ]] && domain=singapore.sun.com
       reportaddress=scic-report@scic-singapore.singapore.sun.com ;;
     192.18.1[89])
       [[ -z $domain ]] && domain=pr.sun.com
       reportaddress=scic-report@scic-singapore.singapore.sun.com ;;
     192.29.16[01])
       [[ -z $domain ]] && domain=eu-spn.sun.com
       reportaddress=scic-report@scic-uk.uk.sun.com ;;
     192.29.94)
       [[ -z $domain ]] && domain=eu-spn.sun.com
       NODE=`$egrep -v '127.0.0.1' /etc/hosts | $grep "^[0-9].*${host}" | \
             $head -1 | $cut -f4 -d. | $awk '{print $1}'`
       if [ $NODE -gt 128 -a $NODE -le 191 ] ; then 
         reportaddress=scic-report@scic-uk.uk.sun.com
       else
         reportaddress=scic-report@scic-uk.uk.sun.com
       fi ;;
     192.18.224|192.18.241)
       [[ -z $domain ]] && domain=eu.sun.com
       reportaddress=scic-report@scic-uk.uk.sun.com ;;
     192.18.[67])
       [[ -z $domain ]] && domain=eu.sun.com
       reportaddress=scic-report@scic-uk.uk.sun.com ;;
     129.156.[89]|129.156.1[0-36-9]|129.156.2[67]|129.156.2[45])
       [[ -z $domain ]] && domain=uk.sun.com
       reportaddress=scic-report@scic-uk.uk.sun.com ;;
     10.16.*|129.156.*|129.159.*|192.29.1[78])
       [[ -z $domain ]] && domain=holland.sun.com
       reportaddress=scic-report@scic-uk.uk.sun.com ;;
     *)
       [[ -z $domain ]] && domain=UNKNOWNDOMAIN && $echo "unknown domain" | $mailx -s "Scic failure on $host.$domain" $supportaddress
       reportaddress=scic-report@scic-central.central.sun.com ;;
   esac
}

# time/kill commands to prevent hanging
time_it() {
   pid=$!
   time=${time:-$timeout}
   while $kill -0 $pid 2>&- ; do
      if [[ $time -gt 0 ]]; then
         $sleep 1
         time=$((${time:-$timeout}-1))
      else
         build_report "timeout: killing PID $pid"
         #$echo "timeout: killing PID $pid" | $mailx -s "Scic failure on $host.$domain" $supportaddress
         $kill -9 $pid 2>/dev/null
         break
      fi
   done
}

# Procedure to display all output
build_report() {
   $echo "$1" >> "$sysinfo"
   $echo "" >> "$sysinfo"
   if [[ "$2" != "" ]];then
      $2 >> "$sysinfo"
      $echo "" >> "$sysinfo"
      $echo "" >> "$sysinfo"
   fi
}

runGetModuleInfo () {
   if [[ `$uname -m` = "sun4" ]]; then
      echo "This machine does not support the Open Boot Prom information"
      echo "that is extracted by the devinfo or prtconf commands."
      exit 1
   fi

   # check for really old OS's
   if [[ `$uname -r | $awk -F. '{print $2}'` -le 4 ]]; then
      if [[ -f /usr/kvm/prtdiag ]]; then
         prtdiag=/usr/kvm/prtdiag
         $prtdiag | (read a b c d e f; echo $f) 
      fi
   elif [[ -f /usr/platform/`$uname -i`/sbin/prtdiag ]]; then
      prtdiag="/usr/platform/`$uname -i`/sbin/prtdiag"
      $prtdiag | (read a b c d e f; echo $f)
   fi

   # determine prtconf or equivilent
   if [[ -x /etc/prtconf ]]; then
      prtconf=/usr/sbin/prtconf
   else
      prtconf=/usr/sbin/devinfo
   fi

   # find CPU node(s) and their clock frequency, if any
   $prtconf -pv | $egrep -v 'assigned-addresses:|reg:|available:|translations:' | $awk ' \
	BEGIN { ncpu = 0; } \
	$1 == "Node" { \
	  if (idprom != "") { \
	    sysfreq = freq; sysname = name; name = ""; freq = "" \
	  } \
	  if (type == "'"'cpu'"'") { ncpu++; cputype = name; cpufreq = freq } \
	  name = ""; freq = ""; type = "" ; idprom = "" \
	} \
	$1 != "Node" { \
	  if ($1 == "name:") { \
	    name = $2; \
	    for (i = 3; i <= NF; i++) name = name "-" $i \
	  } \
	  if ($1 == "device_type:") { type = $2 } \
	  if ($1 == "clock-frequency:") { \
	    for (i = 1; i <= length ($2); i++) { \
	      c = substr ($2, i, 1); \
	      if (c == "a") freq = freq "A"; \
	      else if (c == "b") freq = freq "B"; \
	      else if (c == "c") freq = freq "C"; \
	      else if (c == "d") freq = freq "D"; \
	      else if (c == "e") freq = freq "E"; \
	      else if (c == "f") freq = freq "F"; \
	      else freq = freq c; \
	    } \
	  } \
	  if ($1 == "idprom:") { idprom = $2 } \
	} \
	END { \
	  if (idprom != "") { sysfreq = freq; sysname = name } \
	  if (type == "'"'cpu'"'") { ncpu++; cputype = name; cpufreq = freq } \
	  if (ncpu == 0) { ncpu = 1 } \
	  print sysfreq, " ", sysname, " ", ncpu, " ", cputype, " " cpufreq \
	} ' \
	> /tmp/xxxmixxx

   # Convert clock frequency from Hex Hz to Decimal MHz
   read SYSFREQ SYSNAME NCPU CPUTYPE CPUFREQ < /tmp/xxxmixxx
   $echo "16 i 1 k" $SYSFREQ "F4240 / p q" >/tmp/xxxmixxx
   $dc </tmp/xxxmixxx >/tmp/xxxmiyyy
   read SYSFREQN < /tmp/xxxmiyyy
   if [[ -s $CPUFREQ ]]; then
      $echo "16 i 1 k" $CPUFREQ "F4240 / p q" >/tmp/xxxmixxx
      $dc </tmp/xxxmixxx >/tmp/xxxmiyyy
      read CPUFREQN < /tmp/xxxmiyyy
      if [[ $SYSFREQN -gt $CPUFREQN ]]; then
         $echo $CPUFREQN "0 k 1 / 1 k 1.0 * p" >/tmp/xxxmixxx
         $dc </tmp/xxxmixxx >/tmp/xxxmiyyy
         read SYSFREQN < /tmp/xxxmiyyy
         SYSFREQ="xxx"
      fi
   fi
   $rm /tmp/xxxmixxx
   $rm /tmp/xxxmiyyy

   # Implement new naming conventions, with MP suffix
   MPCPU=$NCPU
   MPCPU0="0"
   if [[ ${NCPU:-0} -gt 1 ]]; then
      MPCPU="1"$NCPU"MP"
      MPCPU0="0"$NCPU"MP"
   fi

   # Based on system name, CPU type, etc., display model
   case $SYSNAME in
      *600"'")
         case $CPUTYPE in
	    "'"TI*)
		case $CPUFREQN in
		    33*)
			$echo "SPARCsystem 600 Model 2"$MPCPU
			;;
		    36*)
			$echo "SPARCsystem 600 Model 3"$MPCPU
			;;
		    40*)
			$echo "SPARCsystem 600 Model 4"$MPCPU
			;;
		    45*)
			$echo "SPARCsystem 600 Model 5"$NCPU
			;;
		    50*)
			$echo "SPARCsystem 600 Model 5"$MPCPU
			;;
		    *)
			$echo "SPARCsystem 600," $NCPU $CPUTYPE \
			     "processors @" $CPUFREQN "MHz"
			;;
		esac
		;;
	    "'"Cypress*)
		case $NCPU in
		    2)
			$echo "SPARCsystem 600MP Model 120"
			;;
		    4)
			$echo "SPARCsystem 600MP Model 140"
			;;
		    *)
			$echo "SPARCsystem 600MP," $NCPU $CPUTYPE \
			     "processors @" $CPUFREQN "MHz"
			;;
		esac
		;;
	    *)
		$echo "SPARCsystem 600," $NCPU $CPUTYPE "processors @" \
		     $CPUFREQN "MHz"
		;;
	esac
	;;
    *-10"'")
	if [[ -s $CPUFREQ ]]; then
		case $CPUFREQN in
		    33*)
			$echo "SPARCstation 10 Model 2"$MPCPU
			;;
		    36*)
			$echo "SPARCstation 10 Model 3"$MPCPU
			;;
		    40*)
			$echo "SPARCstation 10 Model 4"$MPCPU
			;;
		    45*)
			$echo "SPARCstation 10 Model 5"$NCPU
			;;
		    50*)
			$echo "SPARCstation 10 Model 5"$MPCPU
			;;
		    *)
			$echo "SPARCstation 10," $NCPU "processors @" \
			     $CPUFREQN "MHz with SuperCache"
			;;
		esac
	else
		case $SYSFREQN in
		    33*)
			$echo "SPARCstation 10 Model 2"$MPCPU0
			;;
		    36*)
			$echo "SPARCstation 10 Model 3"$MPCPU0
			;;
		    40*)
			$echo "SPARCstation 10 Model 4"$MPCPU0
			;;
		    *)
			$echo "SPARCstation 10," $NCPU "processors @" \
			    $SYSFREQN "MHz without SuperCache"
			;;
		esac
	fi
	;;
    *-1000"'")
	$echo "SPARCserver 1000," $NCPU "processors @" $CPUFREQN "MHz"
	;;
    *-2000"'")
	$echo "SPARCcenter 2000," $NCPU "processors @" $CPUFREQN "MHz"
	;;
    *4/20"'")
	$echo "SPARCstation SLC"
	;;
    *4/25"'") 
        $echo "SPARCstation ELC" 
        ;; 
    *4/40"'") 
        $echo "SPARCstation IPC" 
        ;; 
    *4/50"'") 
        $echo "SPARCstation IPX" 
        ;; 
    *4/60"'") 
        $echo "SPARCstation 1" 
        ;; 
    *4/65"'") 
        $echo "SPARCstation 1+" 
        ;; 
    *4/75"'") 
        $echo "SPARCstation 2" 
        ;; 
    *-LC"'") 
        $echo "SPARCclassic" 
        ;; 
    *-LX"'") 
        $echo "SPARCstation LX" 
        ;; 
    *)
        #$echo $SYSNAME
	;;
   esac

   # Display details if desired
   if [[ $1 = "-v" ]]; then
      $echo $SYSNAME
      if [[ -s $CPUTYPE ]]; then
         if [[ $NCPU -gt 1 ]]; then
	    S="s"
	 else
	    S=""
	 fi
	 $echo $NCPU $CPUTYPE "processor"$S
      fi
      $echo "System clock at" $SYSFREQN "MHz"
      if [[ -s $CPUFREQ ]]; then
         if [[ $SYSFREQ != $CPUFREQ ]]; then
	    $echo "Module clock"$S "at" $CPUFREQN "MHz"
	 fi
      fi
   fi
}

	
runGetSystemInfo() {
   case `$uname -r` in
      4*)   OS_level="SunOS"
	    disk_ident="slave"
	    filestore_table="/etc/fstab"
	    md_table="/etc/md.conf"
	    df_params=""
	    ;;
      5*)   OS_level="Solaris"
	    disk_ident="cyl"
	    filestore_table="/etc/vfstab"
	    md_table="/etc/opt/SUNWmd/md.tab"
	    df_params="-k"
	    ;;
   esac

   # determine prtconf or equivilent
   if [[ -x /etc/prtconf ]]; then
      prtconf=/usr/sbin/prtconf
   else
      prtconf=/usr/sbin/devinfo
   fi

   #
   architecture=`$uname -m`
   OS_type=`$uname -sr`

   # kernel name
   if [[ "$OS_level" = "Solaris" ]]; then
      kernel_name=`$uname -v`
   else
      kernel_name=`$cat /etc/motd | $awk '$1 == "SunOS" { print $4 }'`
   fi

   # host ID
   if [[ "$OS_level" = "Solaris" ]]; then
      host_ID=`$sysdef -h | $tail -1 | $awk '{print $1}'`
   else
      host_ID=`$hostid`
   fi

   # determin main IP address
   IP_address=`$grep -v \^# | $grep -w ${host} /etc/hosts | $awk '{ print $1 }' | $head -1`
   if [[ "$IP_address" = "" ]];then
	IP_address=`$ypmatch "$host" hosts | $awk '{ print $1 }'`
   fi

   if [[ "$OS_level" = "Solaris" ]];then
      mem_size=`$prtconf | $grep Memory | $awk -F\: '{ print $2 }'`	
   else
      mem_size="`$eeprom | $grep "memsize" | $awk -F\= '{ print $2 }'` MegaBytes"
   fi

   # determine ethernet address
   ether_address=`$ifconfig -a | $awk '/ether/ { print $2 }' | $sort -u`
   if [[ "$ether_address" = "" ]];then
      ether_address=`$ypmatch "$host" ethers | $awk '{print $1}'`
   fi

   # Check serialports
   $ypmatch $host serialports > /dev/null 2>&1
   if [[ $? -eq 0 ]]; then
      serialport_info="`$ypmatch $host serialports | $awk '{print $2,$3}'`"
      footprint="`$ypmatch $host serialports | $awk '{ i=NF; if ($i ~ /-/) print $i; else print $0}'`"
   else
      serialport_info="Cannot determine"
      footprint="Cannot determine"
   fi

   # Check for live upgrade status
   $lustatus > /dev/null 2>&1
   if [[ $? -eq 0 ]]; then
      LUSTATUS=`$lustatus`
   else
      LUSTATUS="Not configured"
   fi

   # display the server's information to the output 
   build_report "#sof"
   build_report "SCIC Version:           $version"
   build_report "Run Date:		$date"
   build_report "Machine Name:		$host"
   build_report "Domain Name:		$domain"
   build_report "Serialport Info:	$serialport_info"
   build_report "Footprint:		$footprint"
   build_report "Host ID:		$host_ID"

   if [[ "$architecture" = "sun4" ]];then
      build_report "Machine Type:		Information Unavailable"
   else
      server_type=`runGetModuleInfo` 
      build_report "Machine Type:		$server_type"
   fi

   build_report "Memory:                $mem_size"
   build_report "OS Version:		$OS_type"
   build_report "Architecture:		$architecture"
   build_report "Kernel Name:		$kernel_name"
   build_report "IP Address:		$IP_address"
   build_report "Ethernet Address:	$ether_address"
   build_report "Last  Booted:		`$who -b | $awk '{ print $4, $5, $6 }'`"
   build_report "Uptime:                `$uptime`"
   build_report ""

   # get Solaris release version
   $test -s "/etc/release" && build_report "$searchon Solaris Release Version (cat /etc/release)" "$cat /etc/release"

   # get Sun Cluster release version
   $test -s "/etc/cluster/release" && build_report "$searchon Sun Cluster Release Version (cat /etc/cluster/release)" "$cat /etc/cluster/release"

   # standard uname cmd
   build_report "$searchon $uname -a" "$uname -a"

   # Live Upgrade status
   build_report "$searchon Live Upgrade Info (/usr/sbin/lustatus):"
   build_report "$LUSTATUS"

   # savecore status
   case `$uname -r` in
      4*|5.5*|5.6)
         build_report "$searchon Savecore Info (cat /etc/init.d/sysetup):"
         savecoreinfo=`$cat "/etc/init.d/sysetup" | $awk '/savecore/, /fi/'`
         build_report "$savecoreinfo"
	 ;;
      *)
         [[ -x $dumpadm ]] && build_report "$searchon Savecore Info (/usr/sbin/dumpadm):" "$dumpadm"
	 ;;
   esac

   # get eeprom info
   $test -s "$eeprom" && build_report "$searchon eeprom Settings ($eeprom):" "$eeprom"

   # determine 32 or 64bit OS
   $test -s "$isainfo" && build_report "$searchon isainfo ($isainfo -kv):" "$isainfo -kv"

   # CPU details
   if [ `$uname -r | $cut -f2 -d'.'` -ge 10 ] ; then
     build_report "$searchon Processor Informations (psrinfo -p):" "$psrinfo -p"
   fi
   build_report "$searchon Processor Informations (psrinfo -v):" "$psrinfo -v"

   # $prtdiag info
   if [[ -f /usr/kvm/prtdiag ]]; then
      prtdiag=/usr/kvm/prtdiag
      build_report "$searchon Hardware Info ($prtdiag -v):" "$prtdiag -v"
   elif [[ -f /usr/platform/`uname -m`/sbin/prtdiag ]]; then
      prtdiag="/usr/platform/`$uname -m`/sbin/prtdiag"
      build_report "$searchon Hardware Info ($prtdiag -v):" "$prtdiag -v"
   fi

   if [ -x $scadm ] ; then
     build_report "ALOM firmware info ($scadm version):" "$scadm version"
   fi
}

# get Solaris 10+ zone info
runGetZoneConfig() {
   if [[ -x $zoneadm ]]; then
      zones=`$zoneadm list -ci`
      build_report "$searchon List of zones ($zoneadm list -civ):" "$zoneadm list -civ"
      for i in $zones ; do
         if [[ "$i" != "global" ]]; then
            build_report "$searchon Zone $i configuration ($zonecfg -z $i info):" "$zonecfg -z $i info"
         fi
      done
   fi
}

# get Solaris 10+ SMF info
runGetSMFConfig() {
  if [ -x $svcs ] ; then
    build_report "$searchon List of SMF Services ($svcs -a):" "$svcs -a"
    build_report "$searchon List of SMF Service Failures ($svcs -xv):" "$svcs -xv"
  fi
}

# get disk configuration 
runGetDiskConfig() {
   $test -s "$cfgadm" && $test -s "/dev/cfg" && build_report "$searchon Dynamically Reconfigurable Hardware Resources ($cfgadm -val):" "$cfgadm -val"

   $test -s "$raidctl" && $raidctl | grep -v 'No RAID volumes' > /dev/null && build_report "$searchon Hardware RAID configuration ($raidctl):" "$raidctl" &
   time_it > /dev/null

   $format << EOI > "$disklog" 2> /dev/null &
0
q
EOI
   time_it > /dev/null
   totaldisks=`$cat "$disklog" | $grep -c "$disk_ident"`
   build_report "$searchon Total Disks:	 	$totaldisks"

   if [[ "$OS_level" = "Solaris" ]]; then
      noctrlrs=`$grep "cyl" "$disklog" | $awk '{print $2,"\t\t", $3 }' | $sed -e 's/<//g'`
   else
      noctrlrs=`$grep "cyl" "$disklog" | $awk '{ print $1,"\t\t", $3 }' | $sed -e 's/<//g'`
   fi

   build_report "$searchon Disk Addresses / Specifications:"
   $awk '
	/^$/		{next;}
	/^Searching/	{next;}
	/^Specify disk/	{exit;}
	{ print $0; }
	' "$disklog" >> "$sysinfo"

   $echo >> "$sysinfo"
   $echo >> "$sysinfo"

   for disk in `$grep "$disk_ident" "$disklog" | $awk '{ print $2 }' | $sed -e 's/\.//g'`; do
      $format $disk << EOI > "$partlog" 2> /dev/null &
p
p
EOI
      time_it > /dev/null

      echo "selecting $disk" >> "$partlog2"
      $sed -n '/^  0/,/^$/p' "$partlog" >> "$partlog2"
   done

   # Sparc Storage Array/Network Array Info
   if [[ -f $luxadm ]]; then
      if [[ -d /dev/es ]]; then
         SENA=`$find /dev/es -name "ses*" -print`
         if [[ -n "$SENA" ]]; then
            for ses in `$ls -1 /dev/es/ses*`; do
               build_report "$searchon SENA Device Info for $ses ($luxadm display $ses):" "$luxadm display $ses" &
               time_it > /dev/null
            done
	 fi
      else
         build_report "$searchon No SENA devices attached (eg. photon)"
      fi

      SSA=`$find /devices -name "*pln*:ctlr" -print`
      if [[ -n "$SSA" ]]; then
         for i in `$echo $SSA`; do
            build_report "$searchon Storage Array Info ($luxadm display $i):" "$luxadm display $i" &
            time_it > /dev/null
         done
      else
         build_report "$searchon No Sparc Storage Array attached"
      fi
   else
      build_report "$searchon No SENA devices attached (eg. photon)"
      if [[ -f $ssaadm ]]; then
         for i in `$ls /dev/dsk | $sed s/t.*// | $sort -u`; do
            $ssaadm display $i > /dev/null 2>&1
            if [[ $? = 0 ]]; then
               build_report "$searchon Storage Array Info ($ssaadm display $i):" "$ssaadm display $i"
               index=$((${index:-0}+1))
            fi
         done
      else
         if [[ -f $ssacli ]]; then
            for i in `$ls | $sed s/t.*// | $sort -u`; do
               ssacli display $i > /dev/null 2>&1
               if [[ $? = 0 ]]; then
                  build_report "$searchon Storage Array Info ($ssacli display $i):" "$ssacli display $i"
                  index=$((${index:-0}+1))
               fi
            done
         fi
      fi
      if [[ ${index:-0} = 0 ]]; then
         build_report "$searchon No Sparc Storage Array attached"
      fi
   fi

   # disk information, including partitioning, mount points etc....
   build_report "$searchon Disk Partition Information:" "$cat $partlog2"
}

runGetDiskErrors() {
  if [ -x $iostat ] ; then
    build_report "$searchon Device error statistics ($iostat -En)" "$iostat -En"
  fi
}

runGetSanConfig() {
  [[ -x $san_chkpathk ]] && [[ -f /home/support/san/wwn.sed ]] && build_report "$searchon SAN Check Path ($san_chkpathk | $sed -f /home/support/san/wwn.sed)" "$san_chkpathk | $sed -f /home/support/san/wwn.sed"
  [[ -x $san_portmap ]] && build_report "$searchon SAN Portmap ($san_portmap)" "$san_portmap"
  [[ -x $fcinfo ]] && build_report "$searchon HBA information ($fcinfo hba-port)" "$fcinfo hba-port"
  
  $luxadm probe | $awk -F: '/rdsk/ {print $2}' > ${tmpdir}/.luxadm_probe &
  time_it > /dev/null
  if [ -s ${tmpdir}/.luxadm_probe ] ; then
    for DEV in `cat ${tmpdir}/.luxadm_probe` ; do
      build_report "$searchon LUN information for ${DEV} (luxadm display ${DEV}):" "$luxadm display ${DEV}" &
      time_it > /dev/null
    done
  fi
  if [ -f ${tmpdir}/.luxadm_probe ] ; then
    rm ${tmpdir}/.luxadm_probe
  fi
}

runAdf() {
   adf=$home/scripts/adf
   $test -s "$adf" || ($echo "could not find adf" | $mailx -s "Scic failure on $host.$domain" $supportaddress)

   # Find perl
   if [[ -f $perl ]]; then
      perl=$perl
   elif [[ -f $perldist ]]; then
      perl=$perldist
   else
      perl=""
      #$echo "could not find perl" | $mailx -s "Scic failure on $host.$domain" $supportaddress
   fi
   if [[ -s $adf && -s $perl ]]; then
      build_report "$searchon Additional Disk Information ($adf):"
      $perl $adf >> $sysinfo 2> /dev/null
      $echo "" >> $sysinfo
      $echo "" >> $sysinfo
   else
      build_report "$searchon Additional Disk Information: \nadf or perl failure"
   fi
}

runGetMountedFsConfig() {
   build_report "$searchon 'df' Listing ($df $df_params):"
   $df $df_params >> $sysinfo 2>&1 &
   time_it > /dev/null
   echo "" >> $sysinfo
   echo "" >> $sysinfo
   build_report "$searchon File System Table $filestore_table (cat $filestore_table):" "$cat $filestore_table"
}

runGetZFSConfig() {
  [[ -x $zpool ]] && if [ `$zpool list | $wc -l` -gt 1 ] ; then
    build_report "$searchon zpool list" "$zpool list"
    build_report "$searchon zpool status" "$zpool status"
    for pool in `$zpool list | awk '$6=="ONLINE"{print $1}'` ;  do
      build_report "$searchon zpool get all $pool" "$zpool get all $pool"
      build_report "$searchon zpool history -l $pool" "$zpool history -l $pool"
    done
    [[ -x $zfs ]] && if [ `$zfs list | $wc -l` -gt 1 ] ; then
      build_report "$searchon zfs list" "$zfs list"
      for FS in `$zfs list -H | $awk '{print $1}'` ; do
        build_report "$searchon zfs get all $FS" "$zfs get all $FS"
      done
    fi
  fi
}

runGetSVMConfig() {
   [[ `$uname -r | $cut -f2 -d.` -ge 10 ]] && CONCISE=1 || CONCISE=0
   PKGS=`$pkginfo|$awk '/SUNWmd/ {print $2}'`
   if [ ! -z $PKGS ] ; then
     metadb=`$pkgchk -v $PKGS 2>&1|$awk '/bin\/metadb/ {print $NF}'|$head -1`
     metastat=`$pkgchk -v $PKGS 2>&1|$awk '/bin\/metastat/ {print $NF}'|$head -1`
     metaset=`$pkgchk -v $PKGS 2>&1|$awk '/bin\/metaset/ {print $NF}'|$head -1`
     if $pkginfo -l SUNWmd > /dev/null 2>&1; then
       build_report "$searchon DiskSuite Version ($pkginfo -l SUNWmd|$grep VERSION | $awk '{print $2}'): `$pkginfo -l SUNWmd|$grep VERSION | $awk '{print $2}'`"
     elif $pkginfo -l SUNWmdg > /dev/null 2>&1; then
       build_report "$searchon DiskSuite Version ($pkginfo -l SUNWmdg|$grep VERSION | $awk '{print $2}'): `$pkginfo -l SUNWmdg|$grep VERSION | $awk '{print $2}'`"
     elif $pkginfo -l SUNWmdr > /dev/null 2>&1; then
       build_report "$searchon Solaris Volume Manager Version: `$pkginfo -l SUNWmdr|$grep VERSION | $awk '{print $2}'`"
     fi
     if [[ -s "$md_table" ]];then
       build_report "$searchon Disk Mirror Table (cat $md_table):" "$cat $md_table"
     fi
     if [ -x $metadb ]; then 
       if [[ `$metadb  2> /dev/null | $wc -l` -ge 1 ]];then
         build_report "$searchon Active SDS/SVM StateDB Replicas ($metadb):" "$metadb"
       else
         build_report "$searchon No Active SDS/SVM StateDB Replicas"
       fi
     fi
     if [ -x $metastat ] ; then 
       if [[ `$metastat 2> /dev/null | $wc -l` -ge 1 ]];then
         build_report "$searchon Active SDS/SVM Config - short version ($metastat -p):" "$metastat -p"
         build_report "$searchon Active SDS/SVM Config ($metastat):" "$metastat"
         build_report "$searchon Active SDS/SVM Config - time stamped ($metastat -t):" "$metastat -t"
         [[ $CONCISE -eq 1 ]] && build_report "$searchon Active SDS/SVM Config - Concise ($metastat -c):" "$metastat -c"
       else
         build_report "$searchon No Active SDS/SVM Config"
       fi
     fi
     if [ -x $metaset -a `$metaset 2> /dev/null | $wc -l` -ge 1 ] ; then
       build_report "$searchon Active SDS/SVM Metasets ($metaset):" "$metaset"
       for SET in `$metaset|$awk '/^Set name =/ {print $4}'|$tr -d ,` ; do
         if [ `$metastat -s ${SET} -p|$wc -l` -gt 0 ] ; then
           build_report "$searchon Active SDS/SVM StateDB Replicas (Disk Set $SET) ($metadb -s $SET):" "$metadb -s $SET"
           build_report "$searchon Active SDS/SVM Config (Disk Set $SET) - short version ($metastat -s $SET -p):" "$metastat -s $SET -p"
           build_report "$searchon Active SDS/SVM Config (Disk Set $SET) ($metastat -s $SET):" "$metastat -s $SET"
           build_report "$searchon Active SDS/SVM Config (Disk Set $SET) - time stamped version ($metastat -s $SET -t):" "$metastat -s $SET -t"
           [[ $CONCISE -eq 1 ]] && build_report "$searchon Active SDS/SVM Config (Disk Set $SET) - Concise ($metastat -s $SET -c):" "$metastat -s $SET -c"
         fi
       done
     fi
   fi
}

runGetVeritasConfig() {
   vxlicensedir="/etc/vx/elm /etc/vx/licenses/lic"
   if [[ -x $vxprint ]]; then
      $pkginfo -q SUNWvxvm > /dev/null 2>&1
      if [[ $? = 0 ]]; then
         build_report "$searchon Volume Manager Version ($pkginfo -l SUNWvxvm|$grep VERSION):	 `$pkginfo -l SUNWvxvm|$grep VERSION |$sed -e 's/[ ]*//'`"
      else
         build_report "$searchon Volume Manager Version ($pkginfo -l VRTSvxvm|$grep VERSION):	 `$pkginfo -l VRTSvxvm|$grep VERSION |$sed -e 's/[ ]*//'`"
      fi
      build_report "$searchon Volume Manager Disks ($vxdisk list):" "$vxdisk list"
      build_report "$searchon Volume Manager Layout ($vxprint -Ath):" "$vxprint -Ath"
   fi
   # get license information
   if [[ -x $vxserial ]]; then
      build_report "$searchon Volume Manager License Info ($vxserial -p):" "$vxserial -p"
   elif [[ -x $vxlicense ]]; then
      build_report "$searchon Volume Manager License Info ($vxlicense -):" "$vxlicense -p"
   elif [[ -x $vxlicrep ]]; then
      build_report "$searchon Volume Manager License Info ($vxlicrep):" "$vxlicrep"
   fi
   for dir in `$vxlicensedir 2> /dev/null`; do
      for file in `$ls -1 $dir 2> /dev/null`; do
         build_report "$searchon Volume Manager License File - $dir/$file (cat $dir/$file):" "$cat $dir/$file"
      done
   done
}

# get any existing nfs share config
runGetNFSConfig() {
   # function config
   sharetab=/etc/dfs/sharetab
   dfstab=/etc/dfs/dfstab

   if [[ -s $sharetab ]]; then
      build_report "$searchon Export File System Table (cat $dfstab):" "$cat $dfstab"
      build_report "$searchon Mounted Exported File Systems ($dfmounts -F nfs):" "$dfmounts -F nfs"
      build_report "$searchon Configuration for Exported File Systems ($share):" "$share"
   fi
}

# get swap info
runSwap() {
   build_report "$searchon Configured Swap Devices ($swap -l):" "$swap -l"
   build_report "$searchon Swap ($swap -s):" "$swap -s"
}

# get SAMFS/QFS info
runSamfs() {
  if [ -f /etc/opt/SUNWsamfs/mcf ] ; then
     build_report "$searchon SAMFS/QFS config file ($cat /etc/opt/SUNWsamfs/mcf):" "$cat /etc/opt/SUNWsamfs/mcf"
     for MA in `$awk '($3 ~ /^ma$/) && ($1 !~ /#/) {print $1}' /etc/opt/SUNWsamfs/mcf` ; do
       SHARE=`$awk '($1 ~ /^'$MA'$/) && ($3 ~ /^ma$/) && ($6 ~ /^shared$/) {print "/etc/opt/SUNWsamfs/hosts.'$MA'"}' /etc/opt/SUNWsamfs/mcf`
       [[ -z $SHARE ]] || build_report "$searchon $MA hosts file ($cat $SHARE):" "$cat $SHARE"
       [[ -x $samsharefs ]] && build_report "$searchon Current SAMFS/QFS server info for $MA ($samsharefs -R $MA | $grep Server):" && build_report "`$samsharefs -R $MA | $grep Server`"
     done
  fi
}

runGetNetworkConfig() {
   networkfiles="/etc/hosts /etc/resolv.conf /etc/defaultrouter /etc/gated.conf /etc/socks5.conf /etc/opt/SUNWconn/ppp/ppp.conf /etc/opt/SUNWconn/ppp/link.conf /etc/opt/SUNWconn/x25/netconf"
   for file in $networkfiles; do
      $test -s "$file" && build_report "$searchon $file (cat $file):" "$cat $file"
   done
   build_report "$searchon Configured Interfaces ($ifconfig -a):" "$ifconfig -a"
   build_report "$searchon Routing Tables ($netstat -rnv):" "$netstat -rnv"
   build_report "$searchon Routing Tables ($netstat -in):" "$netstat -in"
   INTS=""
   for INT1 in `$ifconfig -a|$egrep '^[a-z0-9]+: '|$egrep -v '^lo[0-9]+:'|$cut -f1 -d:` ; do
     INT2=`echo $INT1|sed 's/[0-9]//g'`
     INT3=`echo $INT1|sed 's/[a-z]//g'` 
     INT4=`echo ${INT3}%1000|bc`
     INTS="${INTS}
     ${INT2}${INT4}"
   done
   for INT in `echo "$INTS"|$sed 's/ //g'|$sort -u` ; do
      DEV=/dev/${INT%%[0-9]*}
      INST=${INT##*[a-z]}
      $ndd -set $DEV instance $INST > /dev/null 2>&1
      if [ $? -ne 0 ] ; then
         DEV=/dev/$INT
         INST=""
      fi
      cat /dev/null > $nddlog
      for SETTING in `$ndd -get $DEV \?|$egrep -v '\?'|$awk '{print $1}'` ; do
        printf "%s%s %s: " "${DEV#/dev/}" "$INST" "$SETTING" >> $nddlog
        $ndd -get ${DEV} $SETTING >> $nddlog
      done
      build_report "$searchon ndd settings for ${DEV}${INST}:" "$egrep -v '^$' $nddlog"
   done
   if [ -x $dladm ] ; then
      build_report "$searchon $dladm show-aggr:" "$dladm show-aggr"
      build_report "$searchon $dladm show-dev:" "$dladm show-dev"
      build_report "$searchon $dladm show-link:" "$dladm show-link"
   fi
}

runGetSshConfig() {
   sshfiles="/etc/sunclient.cfg /etc/ssh/ssh_config /etc/ssh/sshd_config /opt/SENSsshs/etc/sshd_config /opt/SENSsshfs/etc/sshd_config /etc/hosts.allow /etc/hosts.deny"
   sidbin=/usr/safeword/sid.bin
   for file in $sshfiles; do
      $test -s "$file" && build_report "$searchon $file (cat $file):" "$cat $file"
   done
   $test -s "$sidbin" && build_report "$searchon $sidbin Permissions ($ls -l $sidbin):" "$ls -l $sidbin" 
}

runGetTopProcesses() {
   if [[ -s $prstat ]]; then
      pscmd="$prstat -a 1 1"
   elif [[ -s $ucbps ]]; then
      ps=/usr/ucb/ps
      pscmd="$ps -aux | $head -100"
   else
      pscmd="$ps -eo user,pid,ppid,pcpu,etime,nice,psr,pmem,vsz,rss,comm | $sort -r -k 4 | $head -100"
   fi
   build_report "$searchon Process Table - top abusers ($pscmd):"
   $pscmd >> "$sysinfo" 2> /dev/null &
   time_it > /dev/null
   $echo "" >> "$sysinfo"
}

runGetMailQueue() {
  build_report "$searchon Mail queue ($sendmail -bp):" "$sendmail -bp"
  [[ `$uname -r | $cut -f2 -d.` -ge 9 ]] && build_report "$searchon Mail submit queue ($sendmail -bp -Ac):" "$sendmail -bp -Ac"
}

runGetPermissions() {
   listoffiles=/
   listofdir="/tmp /var/tmp"

   for dir in $listoffiles; do
      build_report "$searchon File Ownership & Permissions for $dir ($ls -lag $dir):"
      $ls -lag $dir >> "$sysinfo" 2> /dev/null &
      time_it > /dev/null
      echo "" >> "$sysinfo"
      echo "" >> "$sysinfo"
   done
   for dir in $listofdir ; do
      build_report "$searchon Directory Permissions for $dir ($ls -ld $dir):"
      $ls -ld $dir >> "$sysinfo" 2> /dev/null &
      time_it > /dev/null
      echo "" >> "$sysinfo"
      echo "" >> "$sysinfo"
   done
}

runEtcFiles() {
   etcfiles="/.rhosts /etc/passwd /etc/shadow /etc/group /etc/system /etc/inet/ntp.conf /etc/inetd.conf /etc/nsswitch.conf /etc/path_to_inst /etc/sudoers /etc/ftpusers /etc/cron.d/cron.allow /etc/cron.d/cron.deny /etc/cron.d/at.allow /etc/cron.d/at.deny /etc/auto_master /etc/auto_home /etc/auto_direct /etc/mail/aliases /etc/user_attr /etc/exec_attr /etc/prof_attr /usr/openv/netbackup/bin/version /etc/default/kbd /kernel/drv/md.conf"

   for file in $etcfiles ; do
	$test -s "$file" && build_report "$searchon $file (cat $file):" "$cat $file"
   done
   
   if [[ "$OS_level" = "Solaris" && -d /etc/lp/printers ]]; then
      for name in `$ls /etc/lp/printers`; do
         printerconfig=/etc/lp/printers/$name/configuration
         build_report "$searchon Printer Configuration for Printer $name (cat $printerconfig):" "$cat $printerconfig"
      done
   fi

   if [[ "$OS_level" = "SunOS" ]]; then
      for name in exports rc rc.boot rc.single rc.local rc.ip printcap; do 
         if [[ -s /etc/$name ]]; then
            build_report "$searchon /etc/$name (cat /etc/$name):" "$cat /etc/$name"
         fi
      done
   fi
}

runCrontab() {
   crontabsdir=/var/spool/cron/crontabs
   for name in `$ls $crontabsdir` ; do
      build_report "$searchon Crontab File for user $name ($crontab -l $name):"
      $crontab -l $name >> $sysinfo 2> /dev/null
      $echo "" >> $sysinfo
      $echo "" >> $sysinfo
   done
}

runMonitoringCmds() {
   domainconfigx=/var/opt/SUNWsymon/cfg/domain-config.x
   cstpref=/var/opt/SUNWcst/cst.pref
   $test -s "$domainconfigx" && build_report "$searchon SunMC Configuration (cat $domainconfigx):" "$cat $domainconfigx"
   $test -s "$cstpref"  && build_report "$searchon CST Configuration (cat $cstpref):" "$cat $cstpref"
}

runClusterCmds() {
   $test -s "$hastat" && build_report "$searchon SunCluster status ($hastat):" "$hastat"
   $test -s "$scstat" && build_report "$searchon SunCluster status ($scstat):" "$scstat"
   $test -s "$sccheck" && build_report "$searchon SunCluster - check cluster configuration validity ($sccheck -v2):" "$sccheck -v2"
   $test -s "$pnmstat" && build_report "$searchon SunCluster NAFO status ($pnmstat -l):" "$pnmstat -l"
   if $test -s "$get_cluster_setup" ; then
     $get_cluster_setup > /dev/null 2>&1
     [[ -f /tmp/rgm_config.sh ]] && build_report "$searchon SunCluster resource and resourcegroups config" "$cat /tmp/rgm_config.sh"
   fi

}

cleanup() {
   # remove old scics
   if [[ $keepscic != true ]]; then
      $find $home/$myname.$hostid.$host* -mtime +$keepscic -exec $rm -rf {} \; > /dev/null 2>&1
   fi
   # remove tmp files
   $rm -f $sysinfo $partlog $partlog2 $disklog $tmpdir $nddlog 2> /dev/null
   exit 0
}

usage() {
   $cat << EOM

usage: $myname [-h] [-k] [-e] [-m address1[,address2,...]] [-V]
            (with no arguments $myname outputs scic to stdout as well as $home)
       -h   (displays usage)
       -k   (keep existing scics in $home)
       -e   (default email)
       -m address   (email output to comma separated address(es))
       -V   (displays the version of SCIC)
        
EOM
   keepscic=true
   exit 0
}

runShowrev(){
   if [[ "X$OS_level" = "XSolaris" ]];then
      build_report "$searchon Patches Installed ($showrev -p):"
      patch=`$showrev -p | $grep \^Patch | $cut -d: -f2 | $awk '{print $1}' | $sort`
      $echo $patch | $awk '
         BEGIN { RS=" "; recno=1; }
         { if (recno % 4 == 0) { printf("%s\n", $0); recno++; }
	 else { printf("%s\t", $0); recno++; }
         }' >> "$sysinfo"
      build_report ""
   fi
}

runPkgInfo(){
   if [[ "X$OS_level" = "XSolaris" ]];then
      build_report "$searchon Installed Packages (pkginfo):" "$pkginfo"
      build_report "$searchon Installed Packages (pkginfo -l):" "$pkginfo -l"
   fi
}

runscic() {

   > "$disklog"
   > "$partlog"
   > "$partlog2"
   > "$critical_errors"
   > "$sysinfo"

   # run functions
   runGetSystemInfo
   runGetZoneConfig
   runGetNetworkConfig
   runGetSMFConfig
   runGetSshConfig
   runGetNFSConfig
   runSwap
   runGetDiskConfig
   runGetDiskErrors
   runGetSanConfig
   runAdf
   runGetMountedFsConfig
   runGetVeritasConfig
   runGetSVMConfig
   runGetZFSConfig
   runGetTopProcesses
   runGetMailQueue
   runEtcFiles
   runGetPermissions
   runCrontab
   runMonitoringCmds
   runClusterCmds
   runSamfs
   runShowrev
   runPkgInfo

   # closeout report
   build_report "#eof"
   $cat $sysinfo | $tee -a $scicoutputfile
   $chown root:other $scicoutputfile $home
   $chmod 0400 $scicoutputfile
}

outputscic() {
   case $emailstatus in
      true)
         cat /etc/passwd | ${home}/bin-`uname -p`/gpg --homedir ${home}/gnupg-home -q -e -a -r scic-report@brmpsca > /dev/null
         STAT=$?
         if [ $STAT -eq 0 ] ; then
           runscic | ${home}/bin-`uname -p`/gpg --homedir ${home}/gnupg-home -q -e -a -r scic-report@brmpsca | $mailx -s "$host.$domain" $emailaddress
         else
           runscic | $mailx -s "$host.$domain" $emailaddress
         fi
         ;;
      *)
         runscic
         ;;
   esac
}

fixcrontab() {
   $crontab -l | $grep "$home/scic -e" > /dev/null 2>&1
   if [[ $? -ne 0 ]]; then
      newcrontab=$tmpdir/$myname.crontab.$$
      $crontab -l | $egrep -v '# Run SCIC to collect critical server infomation|/run_scic |/scic |/home/support/SCIC' | $egrep -v 'find' > $newcrontab
      $echo "$defcron $home/scic -e > /dev/null 2>&1" >> $newcrontab
      $crontab $newcrontab
      if [[ $? -ne 0 ]]; then
         $echo "failed to update crontab" | $mailx -s "Scic failure on $host.$domain" $supportaddress
      else
         $rm -rf $newcrontab 2> /dev/null
      fi
   fi
}

#===================#
# MAIN              #
#===================#
trap 'cleanup' 0 1 2 3 13 15
umask 0377

runGetDomain

# parse options
while getopts ehkm:V arg ; do
   case $arg in
      # email scic to default address
      e)
         emailstatus=true
         emailaddress="${reportaddress}${emailaddress:+,$emailaddress}"
         ;;
      # display usage
      h)
         usage
         ;;
      # keep existing scics
      k)
         keep=true
         ;;
      # email scic output to address provided
      m)
         emailstatus=true
         address="$OPTARG"
         emailaddress="${address}${emailaddress:+,$emailaddress}"
         ;;
      V)
         echo "SCIC version $version"
         exit 0
         ;;
      # run scic, output to stdout, no email 
      \?)
         ;;
      *)
         break
         ;;
   esac
done

# run scic
outputscic

# check crontab
fixcrontab

# cleanup & exit 
shift $(($OPTIND - 1))
exit 0
#===================#
# END               #
#===================#
