2016-04-07 09:03:22 -04:00
|
|
|
#!/usr/bin/env python
|
2012-11-23 14:03:46 -05:00
|
|
|
import optparse
|
2013-08-07 11:51:03 -04:00
|
|
|
import zulip
|
2012-11-23 14:03:46 -05:00
|
|
|
|
2013-12-05 18:06:13 -05:00
|
|
|
VERSION = "0.9"
|
2012-11-23 14:03:46 -05:00
|
|
|
# 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='')
|
2013-02-13 16:19:08 -05:00
|
|
|
parser.add_option('--stream', default='nagios')
|
2013-08-07 11:36:46 -04:00
|
|
|
parser.add_option('--config', default='/etc/nagios3/zuliprc')
|
2012-11-23 14:03:46 -05:00
|
|
|
for opt in ('type', 'host', 'service', 'state'):
|
|
|
|
parser.add_option('--' + opt)
|
|
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
2013-12-06 17:50:55 -05:00
|
|
|
client = zulip.Client(config_file=opts.config, client="ZulipNagios/" + VERSION)
|
2013-02-13 16:19:08 -05:00
|
|
|
|
|
|
|
msg = dict(type='stream', to=opts.stream)
|
2012-11-23 14:03:46 -05:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2013-05-21 18:13:29 -04:00
|
|
|
if len(msg['subject']) > 60:
|
|
|
|
msg['subject'] = msg['subject'][0:57].rstrip() + "..."
|
2012-11-23 14:03:46 -05:00
|
|
|
# e.g. **PROBLEM**: service is CRITICAL
|
|
|
|
msg['content'] = '**%s**: %s is %s' % (opts.type, thing, opts.state)
|
|
|
|
|
2012-11-26 17:08:13 -05:00
|
|
|
# The "long output" can contain newlines represented by "\n" escape sequences.
|
|
|
|
# The Nagios mail command uses /usr/bin/printf "%b" to expand these.
|
|
|
|
# We will be more conservative and handle just this one escape sequence.
|
|
|
|
output = (opts.output + '\n' + opts.long_output.replace(r'\n', '\n')).strip()
|
2012-11-23 14:03:46 -05:00
|
|
|
if output:
|
2013-01-02 10:22:25 -05:00
|
|
|
# Put any command output in a code block.
|
|
|
|
msg['content'] += ('\n\n~~~~\n' + output + "\n~~~~\n")
|
2012-11-23 14:03:46 -05:00
|
|
|
|
|
|
|
client.send_message(msg)
|