2020-04-02 09:59:28 -04:00
|
|
|
#!/usr/bin/env python3
|
2017-08-01 23:28:55 -04:00
|
|
|
import argparse
|
2020-04-18 19:00:35 -04:00
|
|
|
from typing import Any, Dict, Text
|
2016-12-28 14:07:58 -05:00
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
import zulip
|
|
|
|
|
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".
|
2017-08-01 23:28:55 -04:00
|
|
|
parser = zulip.add_default_arguments(argparse.ArgumentParser()) # type: argparse.ArgumentParser
|
2021-05-28 05:05:11 -04:00
|
|
|
parser.add_argument("--output", default="")
|
|
|
|
parser.add_argument("--long-output", default="")
|
|
|
|
parser.add_argument("--stream", default="nagios")
|
|
|
|
parser.add_argument("--config", default="/etc/nagios3/zuliprc")
|
|
|
|
for opt in ("type", "host", "service", "state"):
|
|
|
|
parser.add_argument("--" + opt)
|
2017-08-01 23:28:55 -04:00
|
|
|
opts = parser.parse_args()
|
2012-11-23 14:03:46 -05:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
client = zulip.Client(
|
|
|
|
config_file=opts.config, client="ZulipNagios/" + VERSION
|
|
|
|
) # type: zulip.Client
|
2013-02-13 16:19:08 -05:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
msg = dict(type="stream", to=opts.stream) # type: Dict[str, Any]
|
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
|
2021-05-28 05:05:11 -04:00
|
|
|
thing = "host" # type: Text
|
2021-05-28 07:19:40 -04:00
|
|
|
msg["subject"] = f"host {opts.host}"
|
2012-11-23 14:03:46 -05:00
|
|
|
else:
|
|
|
|
# Service notification
|
2021-05-28 05:05:11 -04:00
|
|
|
thing = "service"
|
2021-05-28 07:19:40 -04:00
|
|
|
msg["subject"] = f"service {opts.service} on {opts.host}"
|
2012-11-23 14:03:46 -05:00
|
|
|
|
2021-05-28 05:05:11 -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
|
2021-05-28 07:19:40 -04:00
|
|
|
msg["content"] = f"**{opts.type}**: {thing} is {opts.state}"
|
2012-11-23 14:03:46 -05:00
|
|
|
|
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.
|
2021-05-28 05:05:11 -04:00
|
|
|
output = (opts.output + "\n" + opts.long_output.replace(r"\n", "\n")).strip() # type: Text
|
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.
|
2021-05-28 05:05:11 -04:00
|
|
|
msg["content"] += "\n\n~~~~\n" + output + "\n~~~~\n"
|
2012-11-23 14:03:46 -05:00
|
|
|
|
|
|
|
client.send_message(msg)
|