The personal website of Philip Mather

Rate of Change

Script to monitor the rate of change of something...

#!/bin/bash 
 
LastOutput=0
 
# Used to keep averages
NumberOfDeltas=0
RunningAverage=0
 
dT=60
 
echo "------------------------------------------------------------------------------"
echo "MM/DD/YY HH:MM:SS AM: VALUE DELTA/${dT}s AVE. DELTA/${dT}s"
echo "------------------------------------------------------------------------------"
 
while true
do
   Output=`netstat -st | grep -E "(TCP timeouts)" | tr -d [:alpha:] | tr -d : | tr -d " "`
 
   if [ "$LastOutput" != "0" ]
   then
      Delta=`expr $Output - $LastOutput`
 
      # Notice that we don't count first entry. This is the number of deltas
      NumberOfDeltas=`expr $NumberOfDeltas + 1`
 
      # Calculate the average change. Unfortunately, significant
      # round-off errors due to no decimal point support in expr
      EntriesMinusThisCount=`expr $NumberOfDeltas - 1`
      Product=`expr $EntriesMinusThisCount \* $RunningAverage + $Delta`
      RunningAverage=`expr $Product / $NumberOfDeltas`
 
      echo "$(date +"%D %r"): $Output $Delta $RunningAverage"
   else
      echo "$(date +"%D %r"): $Output - -"
   fi
 
   # Set the last output to current for next iteration
   LastOutput=$Output
 
   # Sleep for minute
   sleep $dT 
done