Unverified Commit f7b9fff1 authored by Finn Christiansen's avatar Finn Christiansen Committed by GitHub
Browse files

Merge pull request #2 from ChristianTacke/pr/improve_message

Improve messages
parents ba126ceb c7e90313
#!/usr/bin/env python3
from pprint import pprint
from xmppnotify import build_argparser, build_message
commonargs = \
["--hostname", "HOSTNAME",
"--hostdisplayname", "HOSTDISPLAYNAME",
"-o", "outputline1\noutputline2",
"--hostaddress", "127.0.0.1",
"--hostaddress6", "::1",
"--icingaweb2url", "ICINGAWEB2URL",
"--state", "STATE", "--longdatetime", "2019-12-31 23:59:59"]
if __name__ == '__main__':
parser = build_argparser()
args = parser.parse_args(commonargs +
["--notificationtype", "PROBLEM",
"-e", "servicename",
"--servicedisplayname", "servicedisplayname"])
pprint(build_message(args))
args = parser.parse_args(commonargs +
["--notificationtype", "RECOVERY",
"--notificationauthorname",
"NOTIFICATIONAUTHORNAME",
"--notificationcomment",
"NOTIFICATIONCOMMENT\nMORE COMMENTS"])
pprint(build_message(args))
......@@ -27,58 +27,61 @@ class XMPPNotify(ClientXMPP):
def build_message(args):
# Prefix all output lines by "> "
if args.output:
output = "> " + args.output.replace("\n", "\n> ")
else:
output = ""
if args.servicename:
# service
message = """***** Service Monitoring on {monitoringhostname} *****
{servicedisplayname} on {hostdisplayname} is {servicestate}!
Info: {serviceoutput}
When: {longdatetime}
Service: {servicename}
Host: {hostname}
message = """[{notificationtype}] {servicedisplayname} on {hostdisplayname} is {servicestate}!
{output}
When: {longdatetime}
Ref: {hostname}!{servicename}
Monitoring host: {monitoringhostname}\
""".format(
notificationtype=args.notificationtype,
monitoringhostname=gethostname(),
servicedisplayname=args.servicedisplayname,
hostdisplayname=args.hostdisplayname,
servicestate=args.state,
serviceoutput=args.output,
output=output,
longdatetime=args.longdatetime,
servicename=args.servicename,
hostname=args.hostname
)
else:
message = """***** Host Monitoring on {monitoringhostname} *****
{hostdisplayname} is {hoststate}!
Info: {hostoutput}
When: {longdatetime}
Host: {hostname}
message = """[{notificationtype}] {hostdisplayname} is {hoststate}!
{output}
When: {longdatetime}
Ref: {hostname}
Monitoring host: {monitoringhostname}\
""".format(
notificationtype=args.notificationtype,
monitoringhostname=gethostname(),
hostdisplayname=args.hostdisplayname,
hoststate=args.state,
hostoutput=args.output,
output=output,
longdatetime=args.longdatetime,
hostname=args.hostname
)
if args.hostaddress:
message += "\nIPv4: {}".format(args.hostaddress)
message += "\nIPv4: {}".format(args.hostaddress)
if args.hostaddress6:
message += "\nIPv6: {}".format(args.hostaddress6)
message += "\nIPv6: {}".format(args.hostaddress6)
if args.notificationcomment:
message += """
\nComment by {notificationauthorname}
{notificationcomment}
# Prefix comment lines by "> "
comment = "> " + args.notificationcomment.replace("\n", "\n> ")
message += """\n
Comment by {notificationauthorname}
{comment}
""".format(
notificationauthorname=args.notificationauthorname,
notificationcomment=args.notificationcomment
comment=comment
)
if args.icingaweb2url and args.servicename:
......@@ -98,13 +101,9 @@ Host: {hostname}
return message
if __name__ == '__main__':
config = configparser.RawConfigParser()
config.read('/etc/xmppnotify.cfg')
jid = config.get('Account', 'jid')
password = config.get('Account', 'password')
def build_argparser():
parser = argparse.ArgumentParser(description='XMPP Notifications')
# required
parser.add_argument('-d', '--longdatetime')
parser.add_argument('-e', '--servicename') # service only
......@@ -123,6 +122,16 @@ if __name__ == '__main__':
parser.add_argument('-c', '--notificationcomment')
parser.add_argument('-i', '--icingaweb2url')
return parser
if __name__ == '__main__':
config = configparser.RawConfigParser()
config.read('/etc/xmppnotify.cfg')
jid = config.get('Account', 'jid')
password = config.get('Account', 'password')
parser = build_argparser()
args = parser.parse_args()
message = build_message(args)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment