Script: Counting Apache Internal Server Errors (Code 500)

If you have information, script, utility, or idea that can be useful for HostMonitor community, you welcome to share information in this forum.
Post Reply
timn
Posts: 184
Joined: Thu Nov 20, 2003 9:57 am
Location: United States

Script: Counting Apache Internal Server Errors (Code 500)

Post by timn »

This is pretty simple, but I thought I'd share.

My web dev group asked if there was a way to monitor the number of code 500 errors ("Internal Server Errors") on our Apache-based sites and alert them if we saw an unusually high number of server errors.

This was my first attempt at scripting this kind of stuff so if you see problems -- or a better, more efficient way to do this, I'm open to learning. The method I have works, but I make no claims beyond that.

The script takes 2 parameters:

LogPath - full path to Apache web access_log
MaxErrors - set result to 'Bad' when this number is exceeded

Script is:

Code: Select all

#!/bin/sh
if [ $# -ge 2 ]
then
  cat $1 | grep '" 500 ' | awk 'END {load='NR'; if (load>'$2') {printf("ScriptRes:Bad:%d\n",load)} else {printf("ScriptRes:Ok:%d\n",load)}; exit}'
else
  echo 'ScriptRes:Unknown:not enough parameters specified'
fi
I have tested this only on Red Hat:
# uname -a
Linux 215717-web2.www.company.com 2.6.18-128.1.1.el5 #1 SMP Mon Jan 26 13:58:24 EST 2009 x86_64 x86_64 x86_64 GNU/Linux
Post Reply