#!/bin/bash # This Script counts the number of times a certain error code appears and report the respective error level . Note: It is dependent on logtail # Author Alex Nyika Omuyonga 22/10/2014 #function to display correct usage help function usage(){ cat < -c -w /valid/path/to/apache/access/log/file Options: -e: The error code to look for ; e.g. -e 500 -c: The number of times the error occurs to deem the situation critical ; e.g -c 200 -w: The warning threshold ; e.g. -w 20 -l: A valid path to the apache access log file ; e.g. /var/log/httpd/access_log EOF } #The variables that we shall work with #The thresholds #Warning W= #Critical C= #The error code E= #The path/to/apache/access/log/file path= #The count of number of codes counted count= function run(){ if [ $C -lt $count ] ; then echo 'Critical: Error Code:'$E' Errors: '$count exit 2 elif [ $W -lt $count ] then echo 'Warning, Error Code:'$E' Errors: '$count exit 1 else echo "Ok: Error Code:"$E "Errors:"$count exit 0 fi } #pick sent variables while getopts "e:w:c:l:" OPTION do case $OPTION in e) E=$OPTARG ;; w) W=$OPTARG ;; c) C=$OPTARG ;; l) path=$OPTARG ;; ?) usage exit ;; esac done # Test to see if we have all the required parameters #check if the user entered all variables if [[ -z $E ]] || [[ -z $W ]] || [[ -z $C ]] || [[ -z $path ]] then usage exit 1 fi # Check and see that the log file provided exits. If yes then go on to check if [ -f $path ] ; then count=$(logtail $path | grep -c 'HTTP/1.1" '$E) run else echo "Log file not found in "$path exit 1 fi