-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail-notification.py
executable file
·300 lines (276 loc) · 8.99 KB
/
mail-notification.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
An email notification script for Icinga 2
'''
import argparse
from datetime import datetime, timedelta
from os import environ, path
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import re
import smtplib
__author__ = 'Tobias von der Krone'
__copyright__ = ''
__license__ = 'GPL 3'
__version__ = '0.1.0'
__maintainer__ = 'Tobias von der Krone'
__email__ = 'tobias@vonderkrone.info'
__status__ = 'Development'
SMTP_HOST = 'localhost'
ICINGAWEB2_URL = 'https://icinga2/icingaweb2'
def perfdata_table(perfdata):
'''
generate html table for performance data
'''
html = '<table><thead><tr>'
html += '<td>Metric</td><td>Value</td><td>Warning</td><td>Critical</td><td>Minimum</td><td>Maximum</td>'
html += '</tr></thead><tbody>'
if perfdata:
result = re.findall(r'\s?(.+?=\S+)', perfdata)
for data in result:
metric, perf = data.split('=')
values = perf.split(';')
uom_match = re.search(r'[^0-9]*$', values[0])
if uom_match:
uom = uom_match.group(0)
else:
uom = None
# the following values are optional
try:
warn = values[1]
except IndexError:
warn = ''
try:
crit = values[2]
except IndexError:
crit = ''
try:
minimum = values[3]
except IndexError:
minimum = ''
try:
maximum = values[4]
except IndexError:
maximum = ''
# create html row
html += '<tr><td>{metric} {uom}</td><td>{val}</td><td>{warn}</td><td>{crit}</td><td>{minimum}</td><td>{maximum}</td></tr>'.format(
metric=metric.replace('"', '').replace("'", ''),
uom='({})'.format(uom) if uom else '',
val=values[0],
warn=warn,
crit=crit,
minimum=minimum,
maximum=maximum,
)
html += '</tbody></table>'
return html
def main():
'''
main function
'''
parser = argparse.ArgumentParser()
parser.add_argument(
'-t', '--object-type',
help='object type, either "Service" or "Host"',
dest='object_type'
)
parser.add_argument(
'-s', '--sender',
help='email of the sender',
dest='sender'
)
parser.add_argument(
'-r', '--recipient',
help='email of the recipient',
dest='recipient'
)
args = parser.parse_args()
notification_type = environ.get('NOTIFICATIONTYPE')
host_name = environ.get('HOSTNAME')
host_address = environ.get('HOSTADDRESS')
last_check_datetime = datetime.utcfromtimestamp(float(environ.get('LASTCHECK')))
last_check = last_check_datetime.strftime('%F %H:%M:%S %Z')
last_state = environ.get('LASTSTATE')
last_state_type = environ.get('LASTSTATETYPE')
subject = '{} - {}'.format(notification_type, host_name)
if notification_type == 'ACKNOWLEDGEMENT':
comment = environ.get('NOTIFICATIONCOMMENT')
author = environ.get('NOTIFICATIONAUTHORNAME')
if 'SERVICENAME' in environ:
service_name = environ.get('SERVICENAME')
service_displayname = environ.get('SERVICEDISPLAYNAME')
state = environ.get('SERVICESTATE')
state_type = environ.get('SERVICESTATETYPE')
output = environ.get('SERVICEOUTPUT')
duration = environ.get('SERVICEDURATION')
perfdata = environ.get('SERVICEPERFDATA')
details_url = '{icingaweb2_url}/monitoring/service/show?host={hostname}&service={servicename}'.format(
icingaweb2_url=ICINGAWEB2_URL,
hostname=host_name,
servicename=service_name
)
ack_url = '{icingaweb2_url}/monitoring/service/acknowledge-problem?host={hostname}&service={servicename}'.format(
icingaweb2_url=ICINGAWEB2_URL,
hostname=host_name,
servicename=service_name
)
subject += '/{}'.format(service_displayname)
else:
state = environ.get('HOSTSTATE')
state_type = environ.get('HOSTSTATETYPE')
output = environ.get('HOSTOUTPUT')
duration = environ.get('HOSTDURATION')
perfdata = environ.get('HOSTPERFDATA')
details_url = '{icingaweb2_url}/monitoring/host/show?host={hostname}'.format(
icingaweb2_url=ICINGAWEB2_URL,
hostname=host_name
)
ack_url = '{icingaweb2_url}/monitoring/host/acknowledge-problem?host={hostname}'.format(
icingaweb2_url=ICINGAWEB2_URL,
hostname=host_name
)
problem_time_datetime = datetime.utcnow() - timedelta(seconds=int(duration))
problem_time = problem_time_datetime.strftime('%F %H:%M:%S %Z')
duration = timedelta(seconds=int(duration))
subject += ' is {}'.format(state)
# start
text = '''
***** Icinga 2 {notification_type} Notification *****
Host: {hostname} ({hostaddress})
'''.format(
notification_type=notification_type.capitalize(),
hostname=host_name,
hostaddress=host_address)
html = '''
<html>
<head>
<title>Icinga 2 Notification</title>
<style>
table table {{
border-collapse: collapse;
}}
thead td {{
font-weight: bold;
}}
table table tr {{
border-bottom: 1px solid black;
border-top: 1px solid black;
}}
</style>
</head>
<body>
<h1><img src='cid:icinga2_logo' alt='Icinga 2'> {notification_type} Notification</h1>
<table>
<tr><td>Host:</td><td>{hostname} ({hostaddress})</td></tr>
'''.format(
notification_type=notification_type.capitalize(),
hostname=host_name,
hostaddress=host_address
)
if args.object_type == 'Service':
text += '''
Service: {}
'''.format(service_displayname)
html += '''
<tr><td>Service:</td><td>{}</td></tr>
'''.format(service_displayname)
text += '''
State: {state} ({state_type})
Last State: {last_state} ({last_state_type})
Check Output: {output}
Performance Data: {perfdata}
'''.format(
state=state,
state_type=state_type,
last_state=last_state,
last_state_type=last_state_type,
output=output,
perfdata=perfdata if perfdata else '-'
)
html += '''
<tr><td>State:</td><td>{state} ({state_type})</td></tr>
<tr><td>Last State:</td><td>{last_state} ({last_state_type})</td></tr>
<tr><td valign="top">Check Output:</td><td>{output}</td></tr>
<tr><td valign="top">Performance Data:</td><td>{perfdata}</td></tr>
<tr><td> </td><td></td></tr>
'''.format(
state=state,
state_type=state_type,
last_state=last_state,
last_state_type=last_state_type,
output=output,
perfdata=perfdata_table(perfdata) if perfdata else '-'
)
text += '''
Last Check: {last_check}
Duration: {duration} hours (since {problem_time})
'''.format(
last_check=last_check,
duration=str(duration),
problem_time=problem_time
)
html += '''
<tr><td>Last Check:</td><td>{last_check}</td></tr>
<tr><td>Duration:</td><td>{duration} hours (since {problem_time})</td></tr>
'''.format(
last_check=last_check,
duration=str(duration),
problem_time=problem_time
)
if notification_type == 'ACKNOWLEDGEMENT':
text += '''
Comment: {comment}
Author: {author}
'''.format(
comment=comment,
author=author
)
html += '''
<tr><td>Comment:</td><td>{comment}</td></tr>
<tr><td>Author:</td><td>{author}</td></tr>
'''.format(
comment=comment,
author=author
)
text += '''
Show Details: {details_url}
'''.format(details_url=details_url)
html += '''
<tr><td> </td><td></td></tr>
<tr><td>Actions:</td><td><a href="{details_url}">Show Details</a>
'''.format(
details_url=details_url
)
if notification_type not in ['RECOVERY', 'DOWNTIME', 'FLAPPINGSTART', 'FLAPPINGEND']:
text += '''
Acknowledge: {ack_url}
'''.format(ack_url=ack_url)
html += ', <a href="{ack_url}">Acknowledge</a>'.format(
ack_url=ack_url
)
html += '''</td></tr>
</table>
</body>
</html>
'''
# read log data
img_filename = '/usr/share/icingaweb2/public/img/logo_icinga.png'
img_data = open(img_filename, 'rb').read()
# create email
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = args.sender
msg['To'] = args.recipient
msg.attach(MIMEText(text, 'plain'))
msg.attach(MIMEText(html, 'html'))
mime_img = MIMEImage(img_data, name=path.basename(img_filename))
mime_img.add_header('Content-ID', '<icinga2_logo>')
msg.attach(mime_img)
# send email for testing
smpt_conn = smtplib.SMTP(SMTP_HOST)
smpt_conn.sendmail(args.sender, args.recipient, msg.as_string())
smpt_conn.quit()
if __name__ == '__main__':
main()