diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2013-07-26 15:29:12 +0200 |
---|---|---|
committer | root <root@vm-monitoring.spline.inf.fu-berlin.de> | 2013-07-26 15:29:12 +0200 |
commit | f92ad5d271698c952de11e389836d496bb8dcab5 (patch) | |
tree | 276d232064bc90b83c7304e197d049f2b60010e2 | |
download | format-notify-f92ad5d271698c952de11e389836d496bb8dcab5.tar.gz format-notify-f92ad5d271698c952de11e389836d496bb8dcab5.tar.bz2 format-notify-f92ad5d271698c952de11e389836d496bb8dcab5.zip |
inital commit
-rwxr-xr-x | format-notify | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/format-notify b/format-notify new file mode 100755 index 0000000..d34fe67 --- /dev/null +++ b/format-notify @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import os, sys, re, errno, time + +def add_color(status): + if status == 'WARNING': + return ("\x034%s\x0f" % status) + elif status == 'CRITICAL' or status == 'UNKNOWN' or status == 'DOWN': + return ("\x034\x02%s\x0f" % status) + elif status == 'OK' or status == 'UP': + return ("\x033\x02%s\x0f" % status) + else: + return status + +def clean_output(status, output): + output = output.strip() + if output.startswith(status): + output = output[len(status):] + return re.sub(r'^[- :]*', '', output) + +def main(): + file = sys.argv.pop(1) + service = sys.argv.pop(1) + status = sys.argv.pop(1) + output = sys.argv.pop(1) + + if file == '--test': + print("%s %s: %s" % (service, + add_color(status), + clean_output(status, output))) + sys.exit(0) + + if not os.path.exists(file): + sys.exit(1) + + now = time.time() + while True: + try: + f = os.open(file, os.O_WRONLY | os.O_NONBLOCK) + break + except OSError: + if time.time() - now > 2: + sys.exit(1) + else: + time.sleep(.5) + + os.write(f, "%s %s: %s\n" % (service, + add_color(status), + clean_output(status, output))) + os.close(f) + +if __name__ == '__main__': + main() |