Monday, October 17, 2005

[UNIX] Daily Plot - Solaris SAR data using gnuplot

#! /bin/sh
#
# For Solaris, 
# generate daily sar graph for cpu and freemem (normalised to 100%)
# run this using cron at 23:59

 
 
#
# prepare environment variables
#
PATH=/usr/bin:/usr/sbin:/bin:/apps/usr/local/bin
export PATH
LD_LIBRARY_PATH=/usr/lib:/apps/usr/local/lib
export LD_LIBRARY_PATH
 
 
#
# local variables
#
TEMP_USR=/tmp/sar-usr.$$
TEMP_MEM=/tmp/sar-mem.$$
TEMP_GP=/tmp/sar-gp.$$
#
PNGDIR="/spare/var/adm/sa/`date '+%Y-%m'`"
PNG="$PNGDIR/sar-`date '+%Y-%m-%d'`.png"
if [ ! -d $PNGDIR ]; then
        mkdir -p $PNGDIR
        chown adm:sys $PNGDIR
        chmod g+w $PNGDIR
fi
 
 
#
# save SAR cpu and freemem data
#
sar -u > $TEMP_USR
sar -r > $TEMP_MEM
 
 
#
# convert free memory (pages) to percentage
#
pagesize=`pagesize`
memory=`prtconf | awk '/^Memory/ {print $3}'`
scale=`echo "scale=10; $pagesize*100/(1024*1024*$memory)" | bc -l`
 
 
 
#
# prepare gnuplot file to generate png
# cpu utilisation and freemem are plot as percentage
# fremem is based on 4096MB memory and pagesize of 8192
#
echo "
set terminal png color
set output '$PNG'
set xdata time
set timefmt '%H:%M:%S'
set xrange ['00:00:00':'23:59:59']
set yrange [0:100]
set xlabel 'Hour'
set ylabel 'Percentage'
set xtics '00:00:00', 3600, '23:59:59'
set format x '%H'
set nomxtics
set grid
set size 1,0.5
set key right outside
set timestamp bottom
set title 'CPU/Memory Utilisation'
plot '$TEMP_USR' using 1:(\$2+\$3) title 'CPU (usr+sys)' with linespoints, \     '$TEMP_MEM' using 1:(\$2*$scale) title 'Free memory' with linespoints
" > $TEMP_GP
gnuplot $TEMP_GP
 
 
#
# cleanup
#
rm -f $TEMP_USR $TEMP_MEM $TEMP_GP

4 Comments:

Blogger stanlo said...

Thanks for the script. it has some useful tips

8:22 PM  
Blogger chihungchan said...

I am glad that you find that useful

7:32 AM  
Blogger Ram said...

Thanks Chan! The example script was quite useful.

5:50 PM  
Blogger chihungchan said...

You are welcome!

11:07 PM  

Post a Comment

<< Home