Home
Interests
Photos
Favorites

Shell Corner: Generating Server Statistics over the Web Using ksh and html October 2004
Hosted by Ed Schaefer

This month, Jorge Torralba submits a ksh script that calculates system health information and creates an html-formatted file for display via the Web.

Generating Server Statistics over the Web Using ksh and html ó A Poor Man's Version

With Linux being the next-generation OS available to the masses, it is increasingly difficult to find shareware for alternative Unix platforms. Yes, you may say itís out there and Iím just not looking in the right places. This may be true, but in most cases, you also must have libraries and compile the code. This can be a nuisance, especially if youíre a Solaris user.

I was looking for a script that would provide system health information via the Web to run on my Solaris 8 Intel Web server remotely. I didn't want anything too fancy, but I wanted enough for a quick glance at the state of my system. During my searches, I found a plethora of packages that would run under Linux, but nothing that was ready to go on a Solaris system. Thus, I decided to write a simple script that would generate the information I was looking for, format it, and serve it up on my Web site.

The code is straightforward. I use a basic ksh script that calls system commands to create an html-formatted file for display via the Web. I use uname to determine basic system information, df for disk utilization, the sar command for system activity, iostat for drive information, and netstat provides interface information. The code, with comments, is listed at the end of this article.

Figure 1: sample output

Figure 1 is sample output generated by calling the script. After reviewing the above code and the new messages listed in Figure 1, itís obvious I will need to add a section to give me information on swap space.

To get real time information from my server, type this URL into your browser: http://www.HomeTheaterDiscussion.com/sysinfo.php

If youíre wondering about that php in the URL, it's simply a call to a execute the script from within the Web site. This can be done in other ways, but my Web site has a PHP front-end.

So, here's the code:

#!/bin/ksh
# sysinfo.cgi
# author: Jorge Torralba

#--- The first lines which are echoed to the html file are critical for
#--- any text which will be formatted as HTML from a shell script.
#--- "Content-type: text/html" and a blank line
#--- afterwards must be the first thing sent to the browser by any CGI
#--- script executed from the cgi-bin directory.

echo "content-type: text/html"
echo ""

#--- Defining the html file to create

outhtml="/myvar/mydirectory/cgi/sysinfo.html"
workdir="/myvar/mydirectory/cgi"

#--- Clearing out the previously generated html file whenever the script is
#--- called.

cat /dev/null > $outhtml

#--- We begin to gather basic system identification information
#--- and start sending the results to the html file

uname -X | grep -v unknown > $workdir/sysinfo.txt

echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>System Identification</B></font>" >> $outhtml
echo "<BR>" >> $outhtml
echo "<HR>" >> $outhtml

while read sysinfo
do
echo "<font face='Arial'>" >> $outhtml
echo $sysinfo >> $outhtml
echo "</font>" >> $outhtml
echo "<BR>" >> $outhtml
done < $workdir/sysinfo.txt

#--- Here we get and format the uptime info

echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>Uptime information</B></font>" >> $outhtml
echo "<font face='Arial'>" >> $outhtml
echo "<BR>" >> $outhtml
echo "<HR>" >> $outhtml
echo `uptime` >> $outhtml
echo "</font>" >> $outhtml
echo "<BR>" >> $outhtml
echo "<BR>" >> $outhtml

#--- Now we are getting the disk utilization statistics from the
#--- Solaris environment using the df command.

echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>Disk Utilization</B></font>" >> $outhtml
echo "<HR>" >> $outhtml
echo "<BR>" >> $outhtml

df -k > $workdir/sysinfo.txt
echo "<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: \
collapse' bordercolor='#111111' width='100%'>" >> $outhtml

#--- We use a little on off test to create an alternate background color
#--- line so that reading the results is easier to do

bold=1
b=0

while read df
do

if [ $b -eq 1 ]; then
bgc="bgcolor='#C0C0C0'"
b=0
else
bgc=""
b=1
fi

echo "<tr>" >> $outhtml

FS=`echo $df | cut -f1 -d " "`
KB=`echo $df | cut -f2 -d " "`
US=`echo $df | cut -f3 -d " "`
AV=`echo $df | cut -f4 -d " "`
CA=`echo $df | cut -f5 -d " "`
MN=`echo $df | cut -f6 -d " "`

hotline=0

if [ $hotline -eq 1 ]; then
fnc="color='#FF0000'"
bold=1
else
fnc="color='#000000'"
bold=0
fi

if [ $bold -eq 1 ]; then
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$FS</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$KB</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$US</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$AV</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$CA</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$MN</b></font></td>" >> $outhtml
bold=0
else
echo "<td "$bgc" ><font "$fnc " face='Arial'>$FS</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$KB</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$US</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$AV</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$CA</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$MN</font></td>" >> $outhtml
fi


echo "</tr>" >> $outhtml

done < $workdir/sysinfo.txt

echo "</table>" >> $outhtml

echo "<BR>" >> $outhtml
echo "<BR>" >> $outhtml

#--- Now we are getting the sar statistics.


echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>SAR information</B></font>" >> $outhtml
echo "<HR>" >> $outhtml
echo "<BR>" >> $outhtml


sar -u 1 5 | grep -v -i sunos | sed '/^$/d' > $workdir/sysinfo.txt
echo "<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: \
collapse' bordercolor='#111111' width='100%'>" >> $outhtml

bold=1
b=0

while read df
do

if [ $b -eq 1 ]; then
bgc="bgcolor='#C0C0C0'"
b=0
else
bgc=""
b=1
fi
echo "<tr>" >> $outhtml

TM=`echo $df | cut -f1 -d " "`
US=`echo $df | cut -f2 -d " "`
SY=`echo $df | cut -f3 -d " "`
WI=`echo $df | cut -f4 -d " "`
ID=`echo $df | cut -f5 -d " "`

#--- As in the disk utilization section, we alternate the line color
#--- for ease of reading. We also highlight the last line of the sar
#--- command in bold and red. To do this, we use the hotline variable
#--- and look for average in our output.


hotline=`echo $TM | grep -i average | wc -l`

if [ $hotline -eq 1 ]; then
fnc="color='#FF0000'"
bold=1
else
fnc="color='#000000'"
bold=0
fi

if [ $bold -eq 1 ]; then
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$TM</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$US</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$SY</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$WI</b></font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'><b>$ID</b></font></td>" >> $outhtml
bold=0
else
echo "<td "$bgc" ><font "$fnc " face='Arial'>$TM</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$US</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$SY</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$WI</font></td>" >> $outhtml
echo "<td "$bgc" ><font "$fnc " face='Arial'>$ID</font></td>" >> $outhtml
fi

echo "</tr>" >> $outhtml

done < $workdir/sysinfo.txt
echo "</table>" >> $outhtml

echo "<BR>" >> $outhtml
echo "<BR>" >> $outhtml

#--- We now send the last messages of the system messages
#--- by adding a Ėx to the tail command we could show the last number
#--- of x lines in the messages file. I am content with the default.

tail /var/adm/messages > $workdir/sysinfo.txt

echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>Tail of /var/adm/messages</B></font>" >> $outhtml
echo "<BR>" >> $outhtml
echo "<HR>" >> $outhtml

while read sysinfo
do
echo "<font face='Arial'>" >> $outhtml
echo $sysinfo >> $outhtml
echo "</font>" >> $outhtml
echo "<BR>" >> $outhtml
done < $workdir/sysinfo.txt

#--- The iostat command gives me detailed information regarding my
#--- system drives including any errors.

iostat -En > $workdir/sysinfo.txt

echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>iostat -E</B></font>" >> $outhtml
echo "<BR>" >> $outhtml
echo "<HR>" >> $outhtml

while read sysinfo
do

#--- As before, I highlight any errors with red and bold.

hotline=`echo $sysinfo | grep -i errors | wc -l`

if [ $hotline -eq 1 ]; then
fnc="color='#FF0000'"
bold=1
else
fnc="color='#000000'"
bold=0
fi
echo "<font " $fnc " face='Arial'>" >> $outhtml
if [ $bold -eq 1 ]; then
echo "<B>" >> $outhtml
fi
echo $sysinfo >> $outhtml
if [ $bold -eq 1 ]; then
echo "</B>" >> $outhtml
fi
echo "</font>" >> $outhtml
echo "<BR>" >> $outhtml
bold=0
done < $workdir/sysinfo.txt

#--- Finally, a netstat command gives me some basic information on the
#--- interface condition.

netstat -i > $workdir/sysinfo.txt

echo "<HR>" >> $outhtml
echo "<font color='#0000FF' face='Arial'><B>netstat -i</B></font>" >> $outhtml
echo "<BR>" >> $outhtml
echo "<HR>" >> $outhtml

while read sysinfo
do
echo "<font face='Arial'>" >> $outhtml
echo $sysinfo >> $outhtml
echo "</font>" >> $outhtml
echo "<BR>" >> $outhtml
done < $workdir/sysinfo.txt
# end script

Jorge Torralba is a software engineer for Intel Corporation in Oregon, mainly focusing now on Oracle, SQL Server, and Unix in support of 300mm chip manufacturing. He is an avid hiker who loves photography, home theaters, and playing the piano. His love for these hobbies has led to the development of www.hometheaterdiscussion.com, a site dedicated to home theaters, and www.rangefinderforum.com, a site dedicated to photography using range finder cameras.

Copyright © 2004 CMP Media LLC

Questions or problems regarding this web site should be directed to abeckman@outdoorssite.com.

Copyright © 2008 Art Beckman. All rights reserved.

Last Modified: March 9, 2008