Graphing sar output using sendmail

This procedure is quite useful when you want to get regular sar information.
There are two rather interesting concepts here. Firstly emailing sar output to a system running apache,
and secondly, the method used to create the html graphs. The following is a cook book on how I did this;-

1) On each of your systems, turn on system accounting either by using the "sys" or "root" user account.

I used tghe root account as follows;-


##### SAR 15 second intervals business days else 15 min intervals
0 8-17 * * 1-5 su - sys /usr/lib/sa/sa1 `echo 300 | awk '{print $1," ",3600/$1;}'`
0 8-17 * * 0,6 su - sys /usr/lib/sa/sa1 `echo 300 | awk '{print $1," ",3600/$1;}'`
59 23 * * * su - sys /usr/lib/sa/sa2 -s 0:00 -e 23:58 -i 30
2) Add an entry in root's cron as follows;-
30 0 * * * /usr/local/bin/sarmail
3) Create the sarmail script;- **There are 2 variables to change.**
#!/bin/sh
#
# send sar output as mail
#
# if called before 1am send yesterdays data else todays data
# if an argument is given, override default with the day specified
#
ADDRESSEE="trial"
TARGET=""

if [ ! -z "$1" ]; then
        SAR="/var/adm/sa/sa`echo $1 | tr -dc '0-9' | awk '{printf("%02d",$1)}`"
elif [ `date +%H` -lt 1 ]; then
        SAR=`ls -1rt /var/adm/sa/sa[0-9][0-9] | tail -2 | head -1`
else
        SAR=`ls -1rt /var/adm/sa/sa[0-9][0-9] | tail -1`
fi
[ ! -f $SAR ] && echo "File not found: $SAR" && exit 2

sar -A -f $SAR | /usr/bin/mailx -s "`hostname` `basename ${SAR}`" ${ADDRESSEE}@${TARGET}
4) On the "$TARGET" system create the trial script. **2 variables to change** SAR_DIR is a temp directory for compiling the html and HTM_DIR is the document root (and location) of the web server. Have a look at the "systype" function. Here I am defining cpu and run queue info required for ksarhtml output for HP systems and Solaris There are also a number of other graphs that can be created. Please refer to the command line information page on ksar.jar


#!/bin/sh
#
#
SARDIR=/sar-output
DATE=`date "+%d"`
MONTH=`date "+%b"`
HTM_DIR=/var/apache/htdocs/sar

systype()
{
        case $host in
        hphost1|hphost2|hphost3)
                SYSTYPE="HpuxcpuSar HpuxRqueueSar"
        ;;
        *)
                SYSTYPE="SolariscpuSar SolarisRqueueSar"
        ;;
        esac
}
while read subject host saname ; do
        [ "$subject" = "Subject:" ] && break
done

[ ! -d /${SARDIR}/$host ] && mkdir -p /${SARDIR}/$host
sfdate="`echo $saname|sed s/sa//g`"
if [ $sfdate != $DATE ];then
        DATE=`TZ=$TZ+24 date "+%d"`
        MONTH=`TZ=$TZ+24 date "+%b"`
fi
cat > /${SARDIR}/$host/${host}.$saname

systype
# make sure file exists
if [ -f $SARDIR/$host/${host}.$saname ];then
#do the kSar stuff
        [ ! -d $HTM_DIR/$host ] && mkdir -p $HTM_DIR/$host
        java -jar /usr/local/bin/kSar.jar -input $SARDIR/$host/${host}.${saname} -graph "$SYSTYPE" -outputJPG $HTM_DIR/$host/$MONTH$DATE -addHTML
[ -f $SARDIR/$host/${host}.${saname} ] && rm $SARDIR/$host/${host}.${saname}
fi 
cd $HTM_DIR/$host
#remove entries over 2 weeks old
find . -mtime +14 -exec rm {} \;
> $HTM_DIR/$host/index.html
echo "<html>" >> $HTM_DIR/$host/index.html
echo "<body bgcolor="white">" >> $HTM_DIR/$host/index.html
for month in `ls *html|grep -v '^ind'|cut -c1-3|sort -u`
do
        echo "<h1> <center>$month <h1>" >> $HTM_DIR/$host/index.html
        echo "<table border=4>" >> $HTM_DIR/$host/index.html
        for day in `ls $month*html`
        do
                echo "<tr>" >> $HTM_DIR/$host/index.html
                niceday="`echo $day|sed s/\_index.html//g`"
                echo "<td><a href=$day>$niceday</a></td>" >> $HTM_DIR/$host/index.html
                echo "</tr>" >> $HTM_DIR/$host/index.html
        done
        echo "</table>" >> $HTM_DIR/$host/index.html
done
echo "</body>" >> $HTM_DIR/$host/index.html
echo "</html>" >> $HTM_DIR/$host/index.html
5) Download and the ksar.jar and install in /usr/local/bin

6) Edit the /etc/mail/aliases and add the following line to the end. Then run /usr/sbin/newaliases.
trial:          "|/usr/local/bin/trial"

© R. Davies. 2007, Wolf Consulting Ltd.