Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A boilerplate nagios check script.
#!/usr/bin/env python
"""
Boilerplate Nagios check script.
"""
import os, os.path, sys, getopt, datetime
import yaml
nagios_codes = {'OK': 0,
'WARNING': 1,
'CRITICAL': 2,
'UNKNOWN': 3,
'DEPENDENT': 4}
def main():
""" example options processing
here we're expecting 1 option "-h"
with a parameter
"""
try:
opts, args = getopt.getopt(sys.argv[1:], "h:")
except getopt.GetoptError, err:
usage()
host = None
for o, value in opts:
if o == "-h":
host = value
else:
usage()
result = check_condition(host)
nagios_return(result['code'], result['message'])
def usage():
""" returns nagios status UNKNOWN with
a one line usage description
usage() calls nagios_return()
"""
nagios_return('UNKNOWN',
"usage: {0} -h host".format(sys.argv[0]))
def nagios_return(code, response):
""" prints the response message
and exits the script with one
of the defined exit codes
DOES NOT RETURN
"""
print code + ": " + response
sys.exit(nagios_codes[code])
def check_condition(host=None):
return dict(
code = "OK" # or "CRITICAL" or "WARNING"
message = "This host or service is okay."
)
if(__name__ == '__main__'):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment