Last active
December 15, 2015 15:59
-
-
Save philchristensen/5285662 to your computer and use it in GitHub Desktop.
A boilerplate nagios check script.
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
#!/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