#!/bin/ksh

# syscheck v1.0.3: http://www.bolthole.com/solaris/
#
#  Free to use and modify without charge; so long as you
#  hold the author not liable for any damage, side effects, etc, etc.
#   "use at your own risk", blah blah.
#  Philip Brown, Sept 2006
#
# general systems check for Solaris.
# - Checks fullness of filesystems, Disk Suite errors, swap space,
#   and number of files in /var/spool/mqueue
#
# Tune the variables below, then run this script from cron or something.

# REPORTTO is an email address
# MAXDISKFULL is max fullness allowed for any filesystem
# MINFREE is smallest amount of swap memory you want available, in 'k'
#
# You probly want more then 10000k for MINFREE!

REPORTTO=phil
MAXDISKFULL=90
MINFREE=10000
MAXMAILFILES=1000

# You shouldnt have to change anything below here
######################################################################
#

PATH=/usr/bin:/usr/sbin:/sbin

TMPFILE=/tmp/output.$$
HOSTNAME=`uname -n`


# be somewhat paranoid about security.
touch $TMPFILE
while [[ -h $TMPFILE ]] ; do rm $TMPFILE; touch $TMPFILE; done

errprint()
{
	print $* >>$TMPFILE
}

if [[ -d /usr/opt/SUNWmd/sbin ]] ; then
	PATH=$PATH:/usr/opt/SUNWmd/sbin
	dometacheck=1
fi
# For newer releases
if [[ -x /usr/sbin/metastat ]] ; then
	dometacheck=1
fi


df -kl | awk '
NR==1	{next}
$1 ~ /vol/	{next}
$1 ~ /lofi/	{next}
	{ split($5,tempval,"%");
	  used=tempval[1];
	  if(int(used)> '$MAXDISKFULL'){
	     print "space used on " $6 " (" $1 ") is " $5;
	  }
	}
' >>$TMPFILE

swapline=`swap -s`
set $swapline
swapfree=${11%k}
if [[ $swapfree -lt $MINFREE ]] ; then
	errprint ERROR: only $swapfree swap
#else
#	errprint its okay we have $swapfree free
fi


if [[ "$dometacheck" != "" ]] ; then
	statecheck=`metastat | grep State: | egrep -v Okay`
	if [[ "$statecheck" != "" ]] ; then
		errprint Metadevices show 'error(s)'
	fi
fi

if [[ "$MAXMAILFILES" != "" ]] ; then
	cd /var/spool/mqueue
	mcount=`print * | wc -w`
	if [[ "$mcount" -gt $MAXMAILFILES ]] ; then
		errprint ERROR: $mcount mail files in /var/spool/mqueue
	fi
fi

##################################################
# And here's the end: Check for error output , and
# email a warning if we find any

if [[ -s $TMPFILE ]] ; then
	print "" >>$TMPFILE
	uname -a >>$TMPFILE
	date >>$TMPFILE
	print "Generated by $0"
	mailx -s "Systems problem on $HOSTNAME" $REPORTTO <$TMPFILE
fi
rm $TMPFILE

