Nagios plugin - average response time for previous minute

Obsoleted: Check Nagios HTTP Metrics

#!/bin/bash
#
# Average Apache response time
# Written by Senthil Nathan
# Last Modified: Dec 9th 2015
#
# Usage: ./http_response_time -f access_log_file -w Count Warn -c Count Critical
#
# Description: Average response time in apache access log for the previous minute
#
# This plugin is to sum and computer the average response time for previous minute
# Assumes apache log time stamp of format "%d/%b/%Y:%H:%M"
# E.g. 08/Dec/2015:10:55:15
# Assumes the time in microseconds is the 11th field delimited by space
#
# Output:
#
#  Response time is OK/Warning/Critical|'Response Time'=xxxxxxus;nnnnnn;mmmmmm;0
#
# Examples:
#
#   Warn if response time is > 100000 micro seconds
#   Critical if response time is > 150000 micro seconds
#   http_response_time -f /opt/apache2/HTTPServer12/logs/rts1prd-app_access_log -w 100000 -c 150000
#
#
 
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 response time> -c <critical response time>"
    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 response time 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
 
        --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 responsetime
PMIN=`date --date '-1 min' +"%d/%b/%Y:%H:%M"`
responsetime=`grep $PMIN $thefile|awk '{ sum += $11; n++ } END { if (n > 0) printf("%d", sum / n); }'`
 
#if [ $? -eq 1 ]; then
#  echo "Response Time Check Error"
#  exit $STATE_UNKNOWN
#fi
#
if [ $responsetime -ge $thecrit ]; then
  echo "Response Time is Critical|'Response Time'=${responsetime}us;${thewarn};${thecrit};0"
  exit $STATE_CRITICAL
fi
if [ $responsetime -ge $thewarn ]; then
  echo "Response Time is Warning|'Response Time'=${responsetime}us;${thewarn};${thecrit};0"
  exit $STATE_WARNING
fi
if [ $responsetime -lt $thewarn ]; then
  echo "Response Time is OK|'Response Time'=${responsetime}us;${thewarn};${thecrit};0"
  exit $STATE_OK
fi
#
echo "Response time Check Unknown"
exit $STATE_UNKNOWN

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