Created
August 17, 2013 20:30
-
-
Save jhalbrecht/6258561 to your computer and use it in GitHub Desktop.
Plugin for Nagios I wrote to monitor temperature data from sensors serving XML in homeAmation format. See: http://homeamation/azurewebsites.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
''' | |
Created on Jan 21, 2012 | |
@author: jeffa aka @jhalbrecht | |
''' | |
# jha 8/17/2013 | |
# Prepare code for publishing. | |
# jha 1/21/2012 | |
# http://www.ibm.com/developerworks/aix/library/au-nagios/#iratings | |
# http://www.gefoo.org/generalfoo/?p=201 | |
import sys | |
import getopt | |
import urllib2 | |
from xml.dom.minidom import parseString | |
nagios_codes = {'OK': 0, 'WARNING': 1, 'CRITICAL': 2, 'UNKNOWN': 3, 'DEPENDENT': 4} | |
def nagios_return(code, response): | |
# Prints the response message and exits the script with | |
# one of the defined exit codes. | |
print code + ': ' + response | |
sys.exit(nagios_codes[code]) | |
def usage(): | |
print """Usage: check_temperature [-h|--help] [-w|--warning level] [-c|--critical level] | |
Warning level defaults to 85.0 | |
Critical level defaults to 95.0""" | |
sys.exit(3) | |
def main(): | |
try: | |
options, args = getopt.getopt(sys.argv[1:], | |
"h:w:c:", | |
"--help --warning= --critical=",) | |
except getopt.GetoptError: | |
usage() | |
sys.exit(3) | |
Warning = 85.0 ; Critical = 95.0 | |
for name, value in options: | |
if name in ("-h", "--help"): | |
usage() | |
if name in ("-w", "--warning"): | |
try: | |
Warning = 0.0 + float(value) | |
except Exception: | |
nagios_return('UNKNOWN','Unable to convert to floating point value') | |
if name in ("-c", "--critical"): | |
try: | |
Critical = 0.0 + float(value) | |
except Exception: | |
nagios_return('UNKNOWN','Unable to convert to floating point value') | |
# download the file | |
try: | |
# jha 8/17/2013 Align with HomeAmation expectations | |
# MODIFY HERE | |
# with url to your temperature XML FQDN or ip address | |
file = urllib2.urlopen("http://x.y.z.h/PutYourUrlHere") | |
except Exception: | |
nagios_return('UNKNOWN','urlopen failed') | |
#convert to string: | |
data = file.read() | |
#close file because we dont need it anymore: | |
file.close() | |
#parse the xml you downloaded | |
dom = parseString(data) | |
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName: | |
xmlTag = dom.getElementsByTagName('CurrentTemperature0')[0].toxml() | |
#strip off the tag (<tag>data</tag> ---> data): | |
xmlData=xmlTag.replace('<CurrentTemperature0>','').replace('</CurrentTemperature0>','') | |
# Checking for excessive heat. Could enhance to check for excessive cold | |
if float(xmlData) >= Critical: | |
nagios_return('CRITICAL','Temperature: %.2f fahrenheit' % float(xmlData)) | |
elif float(xmlData) >= Warning: | |
# nagios_return('WARNING','Temperature: ' + xmlData + ' fahrenheit') | |
# bingled a nice format string | |
nagios_return('WARNING','Temperature: %.2f fahrenheit' % float(xmlData)) | |
else: | |
nagios_return('OK','Temperature: %.2f fahrenheit' % float(xmlData)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment