forked from NDrive/nagios-mattermost
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mattermost.py
executable file
·113 lines (97 loc) · 4.24 KB
/
mattermost.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# Copyright (c) 2015 NDrive SA
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import argparse
import json
try:
import urllib.request as urllib_request
except ImportError:
import urllib2 as urllib_request
try:
import urllib.parse as urllib_parse
except ImportError:
import urllib as urllib_parse
VERSION = "1.2.0"
TEMPLATE_HOST = "__{notificationtype}__ {hostalias} is {hoststate} - {hostoutput}" # noqa
TEMPLATE_SERVICE = "__{notificationtype}__ {hostalias}/{servicedesc} is {servicestate} - {serviceoutput}" # noqa
def parse():
parser = argparse.ArgumentParser(description='Sends alerts to Mattermost')
parser.add_argument('--url', help='Incoming Webhook URL', required=True)
parser.add_argument('--channel', help='Channel to notify')
parser.add_argument('--username', help='Username to notify as',
default='Icinga')
parser.add_argument('--iconurl', help='URL of icon to use for username',
default='https://s3.amazonaws.com/cloud.ohloh.net/attachments/50631/icinga_logo_med.png') # noqa
parser.add_argument('--notificationtype', help='Notification Type',
required=True)
parser.add_argument('--hostalias', help='Host Alias', required=True)
parser.add_argument('--hoststate', help='Host State')
parser.add_argument('--hostoutput', help='Host Output')
parser.add_argument('--servicedesc', help='Service Description')
parser.add_argument('--servicestate', help='Service State')
parser.add_argument('--serviceoutput', help='Service Output')
parser.add_argument('--author', help='Author')
parser.add_argument('--comment', help='Comment')
parser.add_argument('--oneline', action='store_true', help='Print only one line')
parser.add_argument('--version', action='version',
version='%(prog)s {version}'.format(version=VERSION))
args = parser.parse_args()
return args
def emoji(notificationtype):
return {
"RECOVERY": ":white_check_mark:",
"PROBLEM": ":fire:",
"DOWNTIMESTART": ":clock10:",
"DOWNTIMEEND": ":sunny:",
"DOWNTIMEREMOVED": "",
"CUSTOM": ":sound:",
"FLAPPINGSTART": ":cloud:",
"FLAPPINGEND": ":sunny:",
"ACKNOWLEDGEMENT": ":exclamation:",
}.get(notificationtype, "")
def make_data(args):
template = TEMPLATE_SERVICE if args.servicestate else TEMPLATE_HOST
# Emojis
text = emoji(args.notificationtype) + " " + template.format(**vars(args))
if args.oneline:
text = text.splitlines()[0]
if args.author:
text += " authored by " + args.author
if args.comment:
text += " commented with " + args.comment
payload = {
"username": args.username,
"icon_url": args.iconurl,
"text": text
}
if args.channel:
payload["channel"] = args.channel
data = {'payload' : json.dumps(payload)}
return data
def request(url, data):
rawdata = urllib_parse.urlencode(data).encode("utf-8")
req = urllib_request.Request(url, rawdata)
response = urllib_request.urlopen(req)
return response.read()
if __name__ == "__main__":
args = parse()
data = make_data(args)
response = request(args.url, data)
print(response.decode('utf-8'))