mypy: Annotate *api/integrations/nagios/nagios-notify-zulip*.
This commit is contained in:
parent
cb98dd411e
commit
04f7fe14fa
|
@ -2,22 +2,25 @@
|
||||||
import optparse
|
import optparse
|
||||||
import zulip
|
import zulip
|
||||||
|
|
||||||
|
from typing import List, Text, Dict, Any
|
||||||
|
|
||||||
VERSION = "0.9"
|
VERSION = "0.9"
|
||||||
# Nagios passes the notification details as command line options.
|
# Nagios passes the notification details as command line options.
|
||||||
# In Nagios, "output" means "first line of output", and "long
|
# In Nagios, "output" means "first line of output", and "long
|
||||||
# output" means "other lines of output".
|
# output" means "other lines of output".
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser() # type: optparse.OptionParser
|
||||||
parser.add_option('--output', default='')
|
parser.add_option('--output', default='')
|
||||||
parser.add_option('--long-output', default='')
|
parser.add_option('--long-output', default='')
|
||||||
parser.add_option('--stream', default='nagios')
|
parser.add_option('--stream', default='nagios')
|
||||||
parser.add_option('--config', default='/etc/nagios3/zuliprc')
|
parser.add_option('--config', default='/etc/nagios3/zuliprc')
|
||||||
for opt in ('type', 'host', 'service', 'state'):
|
for opt in ('type', 'host', 'service', 'state'):
|
||||||
parser.add_option('--' + opt)
|
parser.add_option('--' + opt)
|
||||||
(opts, args) = parser.parse_args()
|
(opts, args) = parser.parse_args() # type: Any, List[Text]
|
||||||
|
|
||||||
client = zulip.Client(config_file=opts.config, client="ZulipNagios/" + VERSION)
|
client = zulip.Client(config_file=opts.config,
|
||||||
|
client="ZulipNagios/" + VERSION) # type: zulip.Client
|
||||||
|
|
||||||
msg = dict(type='stream', to=opts.stream)
|
msg = dict(type='stream', to=opts.stream) # type: Dict[str, Any]
|
||||||
|
|
||||||
# Set a subject based on the host or service in question. This enables
|
# Set a subject based on the host or service in question. This enables
|
||||||
# threaded discussion of multiple concurrent issues, and provides useful
|
# threaded discussion of multiple concurrent issues, and provides useful
|
||||||
|
@ -26,7 +29,7 @@ msg = dict(type='stream', to=opts.stream)
|
||||||
# We send PROBLEM and RECOVERY messages to the same subject.
|
# We send PROBLEM and RECOVERY messages to the same subject.
|
||||||
if opts.service is None:
|
if opts.service is None:
|
||||||
# Host notification
|
# Host notification
|
||||||
thing = 'host'
|
thing = 'host' # type: Text
|
||||||
msg['subject'] = 'host %s' % (opts.host,)
|
msg['subject'] = 'host %s' % (opts.host,)
|
||||||
else:
|
else:
|
||||||
# Service notification
|
# Service notification
|
||||||
|
@ -41,7 +44,7 @@ msg['content'] = '**%s**: %s is %s' % (opts.type, thing, opts.state)
|
||||||
# The "long output" can contain newlines represented by "\n" escape sequences.
|
# The "long output" can contain newlines represented by "\n" escape sequences.
|
||||||
# The Nagios mail command uses /usr/bin/printf "%b" to expand these.
|
# The Nagios mail command uses /usr/bin/printf "%b" to expand these.
|
||||||
# We will be more conservative and handle just this one escape sequence.
|
# We will be more conservative and handle just this one escape sequence.
|
||||||
output = (opts.output + '\n' + opts.long_output.replace(r'\n', '\n')).strip()
|
output = (opts.output + '\n' + opts.long_output.replace(r'\n', '\n')).strip() # type: Text
|
||||||
if output:
|
if output:
|
||||||
# Put any command output in a code block.
|
# Put any command output in a code block.
|
||||||
msg['content'] += ('\n\n~~~~\n' + output + "\n~~~~\n")
|
msg['content'] += ('\n\n~~~~\n' + output + "\n~~~~\n")
|
||||||
|
|
Loading…
Reference in a new issue