Created
October 3, 2013 23:18
-
-
Save phobologic/6818584 to your computer and use it in GitHub Desktop.
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
import collections | |
import sys | |
import traceback | |
S_OK = 0 | |
S_UP = 0 | |
S_WARNING = S_WARN = 1 | |
S_CRITICAL = S_CRIT = 2 | |
S_DOWN = 2 | |
S_UNKNOWN = 3 | |
ExitCode = collections.namedtuple('ExitCode', ['code_name', 'code_value']) | |
OK = ExitCode('OK', S_OK) | |
WARNING = ExitCode('WARNING', S_WARNING) | |
CRITICAL = ExitCode('CRITICAL', S_CRITICAL) | |
UNKNOWN = ExitCode('UNKNOWN', S_UNKNOWN) | |
def print_and_exit(exit_code, message, exc_info=False): | |
print '%s - %s' % (exit_code.code_name, message) | |
if exc_info: | |
traceback.print_exc() | |
sys.exit(exit_code.code_value) | |
def safe_nagios_wrapper(func): | |
""" | |
A decorator useful for wrapping the 'main' part of a python based nagios | |
monitor. This allows it to catch unhandled exceptions and print them in | |
a nagios friendly format. | |
""" | |
def new_function(*args, **kwargs): | |
try: | |
func(*args, **kwargs) | |
except SystemExit: | |
raise | |
except Exception, e: | |
print_and_exit( | |
UNKNOWN, 'Unknown exception: %s' % e.message, exc_info=True) | |
return new_function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment