Thursday, 30 July 2015

Web Server Statistic Calculation

#!/bin/sh

usage="Usage: `basename $0` -f <frequency in seconds, min 1, default 60> -l <log file>"

# Set up options
while getopts ":l:f:" options; do
 case $options in
 l ) logFile=$OPTARG;;
 f ) frequency=$OPTARG;;
 \? ) echo -e $usage
  exit 1;;
 * ) echo -e $usage
  exit 1;;

 esac
done

# Test for logFile
if [  ! -n "$logFile" ]
then
 echo -e $usage
 exit 1
fi

# Test for frequency
if [  ! -n "$frequency" ]
then
 frequency=60
fi

# Test that frequency is an integer
if [  $frequency -eq $frequency 2> /dev/null ]
then
 :
else
 echo -e $usage
 exit 3
fi

# Test that frequency is an integer
if [  $frequency -lt 1 ]
then
 echo -e $usage
 exit 3
fi

if [ ! -e "$logFile" ]
then
 echo "$logFile does not exist."
 echo
 echo -e $usage
 exit 2
fi

NOW=$(tail -n1 $logFile | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}')
REF=$(($NOW - 1*60))
NUM=$REF
LINE=1
COUNT=0
BAD=0
lastCount=`wc -l $logFile | sed 's/\([0-9]*\).*/\1/'`
while true
do
while [ $NUM -ge $REF ]
do
 NUM=$( tail -n$LINE $logFile | head -1 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' )
 tail -n$LINE /var/log/httpd/access_log | head -1 | grep -i "200" >/dev/null && let COUNT=$COUNT+1
 tail -n$LINE /var/log/httpd/access_log | head -1 | grep -v "200" >/dev/null && let BAD=$BAD+1
 let LINE=$LINE+1
done
 let COUNT=$COUNT-1
 let BAD=$BAD-1
 curl -X POST -d '[{"name":"response","columns":["val1","val2"],"points":[['$COUNT','$BAD]]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
 newCount=`wc -l $logFile | sed 's/\([0-9]*\).*/\1/'`
 diff=$(( newCount - lastCount ))
 rate=$(echo "$diff / $frequency" |bc -l)
 #echo $rate
 curl -X POST -d '[{"name":"hps","columns":["val"],"points":[['$rate']]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
 lastCount=$newCount
 sleep $frequency
done

No comments:

Post a Comment