/
hipcard.py
231 lines (198 loc) · 8.13 KB
/
hipcard.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
"""Zabbix Action Alert Script for HipChat v2 API.
This module fires off messages to HipChat using the cards layout that is
collapse-able. The alerts contain links to the following areas within Zabbix.
Trigger Descriptions
Past Events for the Trigger
Acknowledgement form for the Trigger
URL Link for URL Defined in the Trigger
"""
import re
import sys
import json
import requests
class ZabbixAlert(object):
"""Parses Zabbix message and formats it for consumption by HipChat v2 API.
The _description method is not currently used due to 5
line limit in hipchat card messages.
Attributes:
images(okimg, probimg): Provide the URLs for images to use as icons
for OK and PROBLEM states.
sendmsg(): Sends the formatted JSON to the HipChat API endpoint.
"""
HEADERS = {'Content-Type': 'application/json'}
def __init__(self, roomid, message, hipname, token, zbxurl):
"""
The __init__ method requires 4 arguments.
Args:
roomid (int): The room ID for the HipChat room to be alerted.
This would be the {ALERT.SENDTO} macro from Zabbix.
message (str): The message would be the {ALERT.MESSAGE} macro from
Zabbix.
It should be formatted like the following
name: "{TRIGGER.NAME}"
id: "{TRIGGER.ID}"
status: "{TRIGGER.STATUS}"
hostname: "{HOSTNAME}"
event_id: "{EVENT.ID}"
severity: "{TRIGGER.SEVERITY}"
url: "{TRIGGER.URL}"
description: "{TRIGGER.DESCRIPTION}"
hipname (str): The company name for your hipchat account.
token (str): The HipChat v2 API token.
zbxurl (str): The FQDN of your Zabbix installation.
"""
self.roomid = roomid
self.message = message
self.hipname = hipname
self.token = token
self.zbxurl = zbxurl
self.okimg = "http://"
self.probimg = "http://"
def _parse(self):
pattern = (r'name: *"(.*)"\s*id: *"(.*)"\s*status: *"(.*)"\s*hostname:'
r' *"(.*)"\s*event_id: *"(.*)"\s*severity: *"(.*)"\s*url: *'
r'"(.*)"\s*description: *"([\w\W\s]*)"')
matches = re.match(pattern, self.message)
return matches
def _name(self):
name = self._parse()
return name.group(1)
def _triggerid(self):
triggerid = self._parse()
return triggerid.group(2)
def _status(self):
status = self._parse()
return status.group(3)
def _hostname(self):
hostname = self._parse()
return hostname.group(4)
def _eventid(self):
eventid = self._parse()
return eventid.group(5)
def _severity(self):
sev = self._parse()
return sev.group(6)
def _url(self):
url = self._parse()
if url.group(7) == "":
url = "#"
else:
url = url.group(7)
return url
def _description(self):
"""This is here for future use. It is not currently implemented."""
desc = self._parse()
return desc.group(8)
def images(self, okimg=None, probimg=None):
"""Set the image URLS for the icons used in OK and PROBLEM states."""
self.okimg = okimg
self.probimg = probimg
def _statassets(self):
if self._status() == "PROBLEM":
statlozenge = "lozenge-error"
img = self.probimg
if self._status() == "OK":
statlozenge = "lozenge-success"
img = self.okimg
return (statlozenge, img)
def _sevassets(self):
if self._severity() == "Not classified" or \
self._severity() == "Information":
sevlozenge = "lozenge"
msgcolor = "gray"
elif self._severity() == "Warning" or self._severity() == "Average":
sevlozenge = "lozenge-current"
msgcolor = "yellow"
elif self._severity() == "High" or self._severity() == "Disaster":
sevlozenge = "lozenge-error"
msgcolor = "red"
if self._status() == "OK":
msgcolor = "green"
return (sevlozenge, msgcolor)
def _formatmessage(self):
message = ("{0}:{1} \n <a href='{5}"
"/tr_comments.php?triggerid={2}'>More Information</a> | "
"<a href='{5}/events.php?"
"filter_set=1&triggerid={2}&period=604800'>Events</a> | "
"<a href='{5}/zabbix.php?"
"action=acknowledge.edit&acknowledge_type=1&"
"eventids[]={3}&backurl=tr_status.php'>Acknowledge</a> | "
"<a href='{4}'>Trigger Link</a></b>") \
.format(self._status(), self._name(), self._triggerid(),
self._eventid(), self._url(), self.zbxurl)
return message
def _card(self):
statloz, statimg = self._statassets()
sevloz, _ = self._sevassets()
card = {
"style": "application",
"format": "medium",
"url": ("{1}/events.php?filter_set=1&"
"triggerid={0}&period=604800").format(self._triggerid(),
self.zbxurl),
"id": self._eventid(),
"title": self._name(),
"activity": {
"html": ("<b><span class='aui-lozenge aui-{0}'>{1}</span> {2}"
"</b>").format(statloz, self._status(), self._name())},
"description": {
"format": "html",
"value": ("<b><a href='{3}/"
"tr_comments.php?triggerid={0}'>More Information</a>"
" | <a href='{3}/events.php?"
"filter_set=1&triggerid={0}&period=604800'>Events</a>"
" | <a href='{3}/zabbix.php?"
"action=acknowledge.edit&acknowledge_type=1&"
"eventids[]={1}&backurl=tr_status.php'>Acknowledge"
"</a> | <a href='{2}'>Trigger Link</a></b>").format(
self._triggerid(), self._eventid(), self._url(),
self.zbxurl)},
"icon": {"url": statimg},
"attributes": [{
"value": {
"label": self._status(),
"style": statloz
},
"label": "Status"
},
{"value": {
"label": self._severity(),
"style": sevloz
},
"label": "Severity"
},
{"value": {
"label": self._hostname(),
"style": "lozenge"
},
"label": "Hostname"
},
],
}
return card
def sendmsg(self):
"""Sends the message data to the HipChat API.
Call it after calling the Class with needed parameters
to send the message"""
_, color = self._sevassets()
payload = {'notify': 'true', 'color': color, 'message':
self._formatmessage(), 'card': self._card(), 'message_format':
'html'}
params = {'auth_token': self.token}
pattern = re.compile("^http(s|)://")
if not pattern.match(self.hipname):
self.hipname = "https://{0}.hipchat.com".format(self.hipname)
resp = requests.post('{0}/v2/room/{1}/notification'.
format(self.hipname, self.roomid), data=json.dumps
(payload), headers=self.HEADERS, params=params)
return resp
def main():
ZBXALERT = ZabbixAlert(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],
sys.argv[5])
if len(sys.argv) == 8:
ZBXALERT.images(okimg=sys.argv[6], probimg=sys.argv[7])
resp = ZBXALERT.sendmsg()
return resp
if __name__ == '__main__':
main()