summaryrefslogtreecommitdiffstats
path: root/format-notify
diff options
context:
space:
mode:
Diffstat (limited to 'format-notify')
-rwxr-xr-xformat-notify53
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()