From 523506efdb6ad610ef6868d3e6c7a8f465e22100 Mon Sep 17 00:00:00 2001 From: Dave Seff Date: Wed, 12 Jun 2019 12:32:03 +1000 Subject: [PATCH 1/2] Addeed formatting and links to acknowledge --- README.md | 4 ++-- notify-teams.py | 53 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fd90923..82c615d 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Create a command object in the Nagios configuration. ``` define command { command_name notify_teams - command_line /usr/bin/printf "$LONGSERVICEOUTPUT$" | /path/to/script/notify-teams.py "$NOTIFICATIONTYPE$: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" "$SERVICEOUTPUT$" $_CONTACTWEBHOOKURL$ + command_line /usr/bin/printf "$LONGSERVICEOUTPUT$" | /path/to/script/notify-teams.py "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$SERVICEDESC$" "$SERVICESTATE$" "$SERVICEOUTPUT$" $_CONTACTWEBHOOKURL$ } ``` Create a contact object with the custom variable macro _WEBHOOK set to the URL from the Teams channel connector. This variable is used when running the command above. @@ -53,4 +53,4 @@ define contact { Then add the contact to an existing object or contact group and reload your configuration. -Create additional contacts with their own `_WEBHOOKURL` custom variable macro for each Teams channel needing notifications. \ No newline at end of file +Create additional contacts with their own `_WEBHOOKURL` custom variable macro for each Teams channel needing notifications. diff --git a/notify-teams.py b/notify-teams.py index ce5a3ae..289ace3 100755 --- a/notify-teams.py +++ b/notify-teams.py @@ -11,21 +11,47 @@ import json import requests import sys +import socket -def create_message(url, subject, output, long_message=None): +def format_links(host, service = ''): + #replace spaces with '+' + service_link = str.replace(service, ' ', '+') + return 'http://%s/nagios/cgi-bin/cmd.cgi?cmd_typ=34&host=%s&service=%s' % (socket.gethostname(), host, service_link) + +def create_message(url, notification_type, host, service, alert, output, long_message=None): ''' creates a dict with for the MessageCard ''' message = {} - message['summary'] = subject - message['title'] = subject + message['summary'] = '%s: %s/%s is %s' % (notification_type, host, service, alert) + message['title'] = '%s: %s/%s is %s' % (notification_type, host, service, alert) message['text'] = output + if alert == 'WARNING': + color = 'FFFF00' + elif alert == 'CRITICAL': + color = 'FF0000' + elif alert == 'UNKNOWN': + color = 'FF7F00' + else: + color = '00FF00' + + message['themeColor'] = color + # if not long_message is None: if long_message: - message['text'] += '\n\n' + long_message + message['text'] += '\n\n%s' % (long_message) + service_link = format_links(host, service) + action = [{ + '@context': 'http://schema.org', + '@type': 'ViewAction', + "name": "Acknowledge this alert", + "target": [service_link] + }] message['@type'] = 'MessageCard' + #message['@type'] = 'ActionCard' message['@context'] = 'https://schema.org/extensions' + message['potentialAction'] = action return message @@ -49,11 +75,14 @@ def main(args): print('error no url') exit(2) - subject = args.get('subject') + host = args.get('host') + notification_type = args.get('type') + service = args.get('service') + alert = args.get('alert') output = args.get('output') long_message = args.get('long_message') - message_dict = create_message(url, subject, output, long_message) + message_dict = create_message(url, notification_type, host, service, alert, output, long_message) message_json = json.dumps(message_dict) send_to_teams(url, message_json) @@ -62,13 +91,19 @@ def main(args): args = {} parser = argparse.ArgumentParser() - parser.add_argument('subject', action='store', help='message subject') + parser.add_argument('type', action='store', help='notification type') + parser.add_argument('host', action='store', help='hostname') + parser.add_argument('service', action='store', help='service description') + parser.add_argument('alert', action='store', help='warning, crit, or ok') parser.add_argument('output', action='store', help='output of the check') parser.add_argument('url', action='store', help='teams connector webhook url') parsedArgs = parser.parse_args() - args['subject'] = parsedArgs.subject + args['type'] = parsedArgs.type + args['host'] = parsedArgs.host + args['service'] = parsedArgs.service + args['alert'] = parsedArgs.alert args['url'] = parsedArgs.url args['output'] = parsedArgs.output @@ -76,4 +111,4 @@ def main(args): args['long_message'] = sys.__stdin__.read() pass - main(args) \ No newline at end of file + main(args) From f4aff8a462a9462e837903a846a24fcd56844d3e Mon Sep 17 00:00:00 2001 From: Dave Seff Date: Wed, 12 Jun 2019 12:46:08 +1000 Subject: [PATCH 2/2] Dont show link for Acknowledge message --- notify-teams.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/notify-teams.py b/notify-teams.py index 289ace3..d357e7f 100755 --- a/notify-teams.py +++ b/notify-teams.py @@ -51,7 +51,8 @@ def create_message(url, notification_type, host, service, alert, output, long_me message['@type'] = 'MessageCard' #message['@type'] = 'ActionCard' message['@context'] = 'https://schema.org/extensions' - message['potentialAction'] = action + if notification_type != 'ACKNOWLEDGEMENT': + message['potentialAction'] = action return message