Nagios plugin for Apache
Posted by Sam
I've been using Nagios for years and for monitoring it's been great. The biggest problem is that there has never been a decent graphing solution that wasn't a giant pain in the butt. Thankfully that's no longer the case. I recently stumbled across pnp and it's been perfect. Good enough in fact that I've stopped looking for a replacement.
Since Phusion will now compile on Solaris I've switched from LiteSpeed back to Apache. Since going back to Apache I decided to dust off an old Nagios script I had for monitoring Apache's processes. It's pretty basic but it works with pnp to graph how busy your Apache servers are. Here's the script.
#!/opt/csw/bin/python
import httplib,re,sys,getopt
#Return codes
OK = 0
Warning = 1
Critical = 2
Unknown = 3
#Variables
hostname = ''
warningThreshold = -1
criticalThreshold = -1
def getUsers(hostname):
con = httplib.HTTPConnection(hostname)
con.request("GET", "/server-status")
res = con.getresponse()
results = res.read()
con.close()
match = re.search("\d+ requests currently", results)
if match:
newmatch = re.search("\d+", match.group())
if newmatch:
return int(newmatch.group())
else:
return -1
else:
return -1
def showUsage():
print sys.argv[0]+' -h -H hostname -w warning -c critical'
sys.exit()
#Get command line options
try:
optlist, args = getopt.getopt(sys.argv[1:], '-h-H:-w:-c:')
except:
showUsage()
for o, a in optlist:
if o == '-h':
showUsage()
sys.exit()
if o == '-H':
hostname = a
if o == '-c':
criticalThreshold = int(a)
if o == '-w':
warningThreshold = int(a)
# Validate command line options
if criticalThreshold == -1:
print 'critical threshold was not set'
showUsage()
if warningThreshold == -1:
print warningThreshold
print 'warning threshold was not set'
showUsage()
if hostname == '':
print 'hostname was not set'
showUsage()
try:
currentUsers = getUsers(hostname)
except:
print 'Unknown'
sys.exit(Unknown)
if currentUsers >= criticalThreshold:
print 'Critical: '+str(currentUsers)+' users|current_users='+str(currentUsers)
sys.exit(Critical)
elif currentUsers >= warningThreshold:
print 'Warning: '+str(currentUsers)+' users|current_users='+str(currentUsers)
sys.exit(Warning)
elif currentUsers >= 0:
print 'OK: '+str(currentUsers)+' users|current_users='+str(currentUsers)
sys.exit(OK)
else:
print 'Unknown'
sys.exit(Unknown)