Nagios Plugin to Count lines in HTTP Logs for the previous minute

Obsoleted: Check Nagios HTTP Metrics

#!/bin/bash
#
# Count Apache access log line count
# Written by Senthil Nathan
# Last Modified: Dec 8th 2015
#
# Usage: ./access_count -f access_log_file -w Count Warn -c Count Critical
#
# Description: Count the number of lines in apache access log for the previous minute
#
# This plugin is to check the lines generated by the log in the previosu minute
# Assumes apache log time stamp of format "%d/%b/%Y:%H:%M"
# E.g. 08/Dec/2015:10:55:15
#
# Output:
#
#  Count is OK/Warning/Critical|'Access Count'=xxxxxx;nnnnnn;mmmmmm;0
#
# Examples:
#
#   Warn if total access count / minute > 10000
#   Critical if total access count > 20000
#   access_count -f /opt/apache2/HTTPServer12/logs/rts1prd-app_access_log -w 10000 -c 20000
#
#
 
PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="1.0"
 
. $PROGPATH/utils.sh
 
check_root()
{
    # make sure script is running as root
    if [ `whoami` != root ]; then
        echo "UNKNOWN: please make sure script is running as root"
        exit $STATE_UNKNOWN
    fi
}
 
print_usage() {
    echo "Usage: $PROGNAME -f <access log file path> -w <warning count> -c <critical count>"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}
 
print_revision() {
    echo "Program: $PROGNAME"
    echo "Version: $REVISION"
}
print_help() {
    print_revision
    echo ""
    print_usage
    echo ""
    echo "Check total access log count for the previous minute for Nagios"
    echo ""
}
 
# Check user is root (not required)
#check_root
 
# Make sure the correct number of command line
# arguments have been supplied
 
if [ $# -lt 1 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi
 
# Grab the command line arguments
 
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision
            exit $STATE_OK
            ;;
        -V)
            print_revision
            exit $STATE_OK
            ;;
        --warning)
            thewarn=$2
            shift
            ;;
        -w)
            thewarn=$2
            shift
            ;;
        --critical)
            thecrit=$2
            shift
            ;;
        -c)
            thecrit=$2
            shift
            ;;
        -f)
            thefile=$2
            shift
            ;;
        --filename)
            thefile=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done
 
# Validate arguments
if [ -z $thecrit ]; then
  print_usage
  exit $STATE_UNKNOWN
fi
if [ -z $thewarn ]; then
  print_usage
  exit $STATE_UNKNOWN
fi
if [ -z $thefile ]; then
  print_usage
  exit $STATE_UNKNOWN
fi
 
# Check begins here
 
#
declare -i accesscount
PMIN=`date --date '-1 min' +"%d/%b/%Y:%H:%M"`
accesscount=`grep -c $PMIN $thefile`
 
#
if [ $accesscount -ge $thecrit ]; then
  echo "Access Count Total is Critical|'Access Count Total'=${accesscount};${thewarn};${thecrit};0"
  exit $STATE_CRITICAL
fi
if [ $accesscount -ge $thewarn ]; then
  echo "Access Count Total is Warning|'Access Count Total'=${accesscount};${thewarn};${thecrit};0"
  exit $STATE_WARNING
fi
if [ $accesscount -lt $thewarn ]; then
  echo "Access Count Total is OK|'Access Count Total'=${accesscount};${thewarn};${thecrit};0"
  exit $STATE_OK
fi
#
echo "Access Count Check Unknown"
exit $STATE_UNKNOWN

QR Code
QR Code tech:nagios:plugin_access_count (generated for current page)