From 84d73db4f3b7a400903e8d6f3489f74601c2f49e Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Fri, 23 Nov 2012 14:03:46 -0500 Subject: [PATCH] Send Nagios alerts to Humbug Fixes #385. (imported from commit 7dac013debd6ccff031fc4da0dd7185e198b4498) --- bots/send-nagios-notification | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 bots/send-nagios-notification diff --git a/bots/send-nagios-notification b/bots/send-nagios-notification new file mode 100755 index 0000000..c28abc6 --- /dev/null +++ b/bots/send-nagios-notification @@ -0,0 +1,47 @@ +#!/usr/bin/env python +import sys +import optparse +from os import path + +sys.path.append(path.join(path.dirname(__file__), '../..')) +import api.common + +# Nagios passes the notification details as command line options. +# In Nagios, "output" means "first line of output", and "long +# output" means "other lines of output". +parser = optparse.OptionParser() +parser.add_option('--output', default='') +parser.add_option('--long-output', default='') +for opt in ('type', 'host', 'service', 'state'): + parser.add_option('--' + opt) +(opts, args) = parser.parse_args() + +msg = dict(type='stream', to='nagios') + +# Set a subject based on the host or service in question. This enables +# threaded discussion of multiple concurrent issues, and provides useful +# context when narrowed. +# +# We send PROBLEM and RECOVERY messages to the same subject. +if opts.service is None: + # Host notification + thing = 'host' + msg['subject'] = 'host %s' % (opts.host,) +else: + # Service notification + thing = 'service' + msg['subject'] = 'service %s on %s' % (opts.service, opts.host) + +# e.g. **PROBLEM**: service is CRITICAL +msg['content'] = '**%s**: %s is %s' % (opts.type, thing, opts.state) + +output = (opts.output + '\n' + opts.long_output).strip() +if output: + # Block-quote any command output. + msg['content'] += ('\n\n' + '\n'.join('> ' + ln for ln in output.splitlines())) + +client = api.common.HumbugAPI( + email = 'humbug+nagios@humbughq.com', + api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', + site = 'https://staging.humbughq.com') +client.send_message(msg)