#!/bin/ksh 
#
#ident	"@(#)load	1.4	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
AVGFILE=/tmp/file.avg
TMPFILE=/tmp/ld.op

setup(){
rm -f $AVGFILE
cat << _EOF > $AVGFILE
AVG1=0
AVG5=0
AVG15=0
_EOF
}

write_avg(){

rm -f $AVGFILE
cat << _EOF > $AVGFILE
AVG1=$AVG1
AVG5=$AVG5
AVG15=$AVG15
_EOF

}

get_avg(){
CAVG1=`grep -w AVG1 $AVGFILE | awk -F'=' '{print $2}'`
CAVG5=`grep -w AVG5 $AVGFILE | awk -F'=' '{print $2}'`
CAVG15=`grep -w AVG15 $AVGFILE | awk -F'=' '{print $2}'`

min1=`head -1 $TMPFILE | awk '{print $3}'`
SUM=`echo "$CAVG1 + $min1" | bc -l`
AVG1=`echo "$SUM / 2" | bc -l`
min5=`head -1 $TMPFILE | awk '{print $5}'`
SUM=`echo "$CAVG5 + $min5" | bc -l`
AVG5=`echo "$SUM / 2" | bc -l`
min15=`head -1 $TMPFILE | awk '{print $7}'`
SUM=`echo "$CAVG15 + $min15" | bc -l`
AVG15=`echo "$SUM / 2" | bc -l`
rm -f $TMPFILE

}


#####
#MAIN
#####
 setup
while [[ "1" == "1" ]]
do
 /opt/SUNWstaf/prgs/loadavg > $TMPFILE
 get_avg
 write_avg
 sleep 1000
done
