Solution 1
Program Code
# File: /dolomite/home/kscott/class/cs209-sysadmin/ass1/kscott.sh
# Author: K. Scott Rowe
# Time-stamp: <02/26/2000 18:35:21 cs209@rainbow>
#kscott Feb 6 2000:
# finger doesn't return an error code if "connection refused"
# rusers is not setup on linux
# rup only returns #users when linux-to-linux
# one-ping-only syntax is different on SunOS and Linux
# rup on solaris seperates loads with a space, linux uses commas and a space
MAILTO="cs209" ; export MAILTO #who gets the mail
CONF_FILE="kscott.cfg" ; export CONF_FILE # machines to check
OSTYPE="`uname -s`" ; export OSTYPE # use for OS differences
TMP_FILE=/tmp/$0.$$ ; export TMP_FILE # temp file to write things to
PATH=/sbin:/usr/sbin:/usr/ucb:/bin:/usr/bin:/usr/local/bin ; export PATH
MyPing() # ping acts differently on different OS's
{
if [ "$OSTYPE" = "SunOS" ] ; then
ping $1 1 > /dev/null 2>&1
elif [ "$OSTYPE" = "Linux" ] ; then
ping -c 1 $1 > /dev/null 2>&1
else
echo "$0: Unknown OS type ($OSTYPE)"
exi 1
fi
[ "$?" = "0" ] && return 0 || return 1
}
MyFinger() # finger doesn't return error codes
{
# compare stderr to see if finger fails
if [ "`finger @$1 2>&1 1> /dev/null`" != "" ] ; then
return 1 #failure
else # if it didnt fail, run it for real this time
# skip first two lines, and remove leading white space from wc output
users="`finger @$1 | tail +3 | wc -l | awk '{print $1}'`"
return 0 #users is already set
fi
}
#### here is where the main program starts #####
cp /dev/null $TMP_FILE # truncate temp file
printf "%-22.22s\tPing\tUsers\tLoad Average\n\n" "Machine Name" >> $TMP_FILE
for x in `cat $CONF_FILE` ; do #iterate over machines in cfg file
MyPing $x
if [ "$?" = "0" ] ; then #check return code to see if ping worked
ping="up"
MyFinger $x #sets a global variable users (no -1 ret code)
if [ "$?" = "0" ] ; then # if finger worked, try rup too
load="`rup $x 2> /dev/null | sed -ne 's/^.*average: \([^ ,]*\)[ ,].*$/\1/p'`"
else
users="" #finger didn't work
load="" #so rup probably wont either
fi
else #if no ping, nothing else wiil work either
ping=""
users=""
load=""
fi
printf "%-22.22s\t$ping\t$users\t$load\n" "$x" >> $TMP_FILE # print our results
done
mail -s "Up Stats" ${MAILTO} < $TMP_FILE 2> /dev/null 2>&1 #mail out our results
rm $TMP_FILE #clean up after ourselves
Program Config File
rainbow.nmt.edu
yellow.nmt.edu
winlm.nmt.edu
cobra.nmt.edu
Program Output
Machine Name Ping Users Load Average
rainbow.nmt.edu up 16 1.34
yellow.nmt.edu
winlm.nmt.edu up
cobra.nmt.edu up 9 0.00
Today's Date:
Last Modified:
K. Scott Rowe