Here I publish a script that you can use to see if apache is running using Python and SNMP. This can be used with Icinga or Nagios without modification but can easily be changed to check for other processes.

Usage is ./isApacheup.py <host>

#!/usr/bin/python
#Progam usage ./getDisk.py <host>
 
#Copyright and Code from from sshadmincontrol.com
#To use this code you must keep this reference.
 
#!/usr/bin/python
 
import commands
import sys
 
runsh=commands.getstatusoutput
walkcmd="/usr/bin/snmpwalk"
getcmd="/usr/bin/snmpget"
host=sys.argv[1]
version="2c"
community="cacti"
oid="hrSWRunPath"
 
def walk(getvar):
        thecommand = walkcmd + " -v " + version + " -c " + community + " " + host + " " + getvar
        return runsh(thecommand)
 
def get(getvar):
        thecommand = getcmd + " -v " + version + " -c " + community + " " + host + " " + getvar
        return runsh(thecommand)
 
runningpath=walk(oid)
 
if (runningpath[0]==0):
        if "httpd" in runningpath[1]:
                returnstr = "OK:Apache (proxy) is Running"
        else:
                returnstr = "MAJOR:Apache (proxy) is NOT Running "
else:
        returnstr = "UNKNOWN:" +  runningpar[1]
 
 
 
print returnstr
 
if "CRITICAL" in returnstr:
        sys.exit(2)
elif "MAJOR" in returnstr:
        sys.exit(2)
elif "UNKNOWN" in returnstr:
        sys.exit(3)
elif "OK" in returnstr:
        sys.exit(0)
else:
        sys.exit(3)

You can use this freely as long as you give credit to sshadmincontrol.com as shown in this scipt.

Also don’t forget to try out the monkey tool, it’s a great time saver.

Go Back to the tutorial menu page.