forked from isaac-galvan/nagios-teams-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify-teams.py
executable file
·79 lines (61 loc) · 1.94 KB
/
notify-teams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
#
#
# Author: Isaac J. Galvan
# Date: 2018-12-04
#
# https://github.com/isaac-galvan
#
import argparse
import json
import requests
import sys
def create_message(url, subject, output, long_message=None):
''' creates a dict with for the MessageCard '''
message = {}
message['summary'] = subject
message['title'] = subject
message['text'] = output
# if not long_message is None:
if long_message:
message['text'] += '\n\n' + long_message
message['@type'] = 'MessageCard'
message['@context'] = 'https://schema.org/extensions'
return message
def send_to_teams(url, message_json):
""" posts the message to the ms teams webhook url """
headers = {'Content-Type': 'application/json'}
r = requests.post(url, data=message_json, headers=headers)
if r.status_code == requests.codes.ok:
print('success')
return True
else:
print('failure')
return False
def main(args):
# verify url
url = args.get('url')
if url is None:
# error no url
print('error no url')
exit(2)
subject = args.get('subject')
output = args.get('output')
long_message = args.get('long_message')
message_dict = create_message(url, subject, output, long_message)
message_json = json.dumps(message_dict)
send_to_teams(url, message_json)
if __name__=='__main__':
args = {}
parser = argparse.ArgumentParser()
parser.add_argument('subject', action='store', help='message subject')
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['url'] = parsedArgs.url
args['output'] = parsedArgs.output
if not sys.__stdin__.isatty():
args['long_message'] = sys.__stdin__.read()
pass
main(args)