Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markup and formatting to teams message #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Create additional contacts with their own `_WEBHOOKURL` custom variable macro for each Teams channel needing notifications.
54 changes: 45 additions & 9 deletions notify-teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,48 @@
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'
if notification_type != 'ACKNOWLEDGEMENT':
message['potentialAction'] = action

return message

Expand All @@ -49,11 +76,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)
Expand All @@ -62,18 +92,24 @@ 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

if not sys.__stdin__.isatty():
args['long_message'] = sys.__stdin__.read()
pass

main(args)
main(args)