forked from treibholz/smtp-gee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp-gee.py
executable file
·388 lines (299 loc) · 12.3 KB
/
smtp-gee.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
This lives in https://github.com/treibholz/smtp-gee
'''
import smtplib
import configparser
import time
import hashlib
import socket
import imaplib
import argparse
import sys
from email.mime.text import MIMEText
socket.setdefaulttimeout(10)
class Account(object): # {{{
"""docstring for Account"""
def __init__(self, name, login=False, password=False, smtp_server="localhost", imap_server="localhost", imap_folder="INBOX", smtp_over_ssl=False, smtp_port=25): # {{{
super(Account, self).__init__()
self.name = name
self.login = login
self.password = password
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.imap_server = imap_server
self.imap_folder = imap_folder
self.email = login
self.smtp_timeout = 30
self.imap_timeout = 30
self.__debug = False
self.smtp_over_ssl = smtp_over_ssl
self.error_string = ""
self.current_folder = ""
# }}}
def send(self, recipient): # {{{
"""docstring for send"""
timestamp = time.time()
payload = """Hi,
this is a testmail, generated by SMTP-GEE.
sent on: %s
sent at: %s
sent from: %s
sent to: %s
Cheers.
SMTP-GEE
""" % (socket.getfqdn(), timestamp, self.email, recipient.email, )
test_id = hashlib.sha1(payload.encode('utf-8')).hexdigest()
msg = MIMEText(payload)
msg['From'] = self.email
msg['To'] = recipient.email
msg['Subject'] = "[SMTP-GEE] |%s" % (test_id, )
try:
if self.smtp_over_ssl:
if self.__debug: print("SMTP-over-SSL is used")
s = smtplib.SMTP_SSL( self.smtp_server, port = self.smtp_port, timeout = self.smtp_timeout)
else:
if self.__debug: print("SMTP is used")
s = smtplib.SMTP( self.smtp_server, port = self.smtp_port, timeout = self.smtp_timeout)
s.starttls()
#s.set_debuglevel(2)
s.login(self.login, self.password )
s.sendmail( self.email, recipient.email, msg.as_string() )
s.quit()
return test_id
except smtplib.SMTPAuthenticationError as smtp_error:
self.error_string += "SMTPAuthenticationError: {0}".format(smtp_error)
return False
except smtplib.SMTPConnectError as smtp_error:
self.error_string += "SMTPConnectError: {0}".format(smtp_error)
return False
except:
self.error_string += "Unexpected error: {0}".format(sys.exc_info())
return False
# }}}
def check(self, check_id, stopwatch=None): # {{{
"""docstring for check"""
try:
m = imaplib.IMAP4_SSL(self.imap_server)
m.login(self.login, self.password)
data=[b'']
# Wait until the message is there.
while data == [b'']:
if stopwatch != None:
if stopwatch.gettime() > self.imap_timeout:
return False
for current_folder in self.imap_folder.split(','):
typ, n = m.select(current_folder)
if typ != "OK":
self.error_string += "Can't select %s: %s" % (current_folder, typ)
return False
if self.__debug:
print("Search in %s %s (%d)" % (current_folder, typ, int(n[0])))
typ, data = m.search(None, 'SUBJECT', '"%s"' % check_id)
if data != [b'']:
self.current_folder = current_folder
break
time.sleep(1)
for num in data[0].split():
typ, _data = m.fetch(num, '(RFC822)')
msg = _data[0][1]
# deleting should be more sophisticated, for debugging...
m.store(num, '+FLAGS', r'\Deleted')
m.expunge()
m.close()
m.logout()
return True
except imaplib.IMAP4.error as imap_error:
self.error_string += "IMAP error: {0}".format(imap_error)
return False
except:
self.error_string += "Unexpected error: {0}".format(sys.exc_info())
return False
# }}}
def set_debug(self, debug): # {{{
"""docstring for set_debug"""
self.__debug = debug
# }}}
# }}}
class Stopwatch(object): # {{{
"""docstring for Stopwatch"""
def __init__(self, debug=False):
super(Stopwatch, self).__init__()
self.__debug = debug
self.__start = -1
self.counter = 0
def gettime(self):
return time.time() - self.__start
def start(self):
"""docstring for start"""
self.__start = time.time()
def stop(self):
"""docstring for stop"""
self.counter += time.time() - self.__start
self.__start = -1
# }}}
if __name__ == "__main__":
# fallback returncode
returncode = 3
# Parse Options # {{{
parser = argparse.ArgumentParser(
description='Check how long it takes to send a mail (by SMTP) and how long it takes to find it in the IMAP-inbox',
epilog = "Because e-mail is a realtime-medium and you know it!")
main_parser_group = parser.add_argument_group('Main options')
main_parser_group.add_argument('--from', dest='sender', action='store',
required=True,
metavar="<name>",
help='The account to send the message')
main_parser_group.add_argument('--rcpt', dest='rcpt', action='store',
required=True,
metavar="<name>",
help='The account to receive the message')
main_parser_group.add_argument('--nagios', dest='nagios', action='store_true',
required=False,
default=False,
help='output in Nagios mode')
main_parser_group.add_argument('--except-means', dest='except_return', action='store',
metavar="<int>",
required=False,
default=2,
help='Map Exceptions to another returncode. Default: %(default)s')
main_parser_group.add_argument('--debug', dest='debug', action='store_true',
required=False,
default=False,
help='Debug mode')
main_parser_group.add_argument('--config',dest='config_file', action='store',
default='config.ini',
metavar="<file>",
required=False,
help='alternate config-file')
smtp_parser_group = parser.add_argument_group('SMTP options')
smtp_parser_group.add_argument('--smtp_warn', dest='smtp_warn', action='store',
required=False,
default=15,
metavar="<sec>",
type=int,
help='warning threshold to send the mail. Default: %(default)s')
smtp_parser_group.add_argument('--smtp_crit', dest='smtp_crit', action='store',
required=False,
default=30,
metavar="<sec>",
type=int,
help='critical threshold to send the mail. Default: %(default)s')
smtp_parser_group.add_argument('--smtp_timeout', dest='smtp_timeout', action='store',
required=False,
default=30,
metavar="<sec>",
type=int,
help='timeout to stop sending a mail. Default: %(default)s')
imap_parser_group = parser.add_argument_group('IMAP options')
imap_parser_group.add_argument('--imap_warn', dest='imap_warn', action='store',
required=False,
default=20,
metavar="<sec>",
type=int,
help='warning threshold until the mail appears in the INBOX. Default: %(default)s')
imap_parser_group.add_argument('--imap_crit', dest='imap_crit', action='store',
required=False,
default=30,
metavar="<sec>",
type=int,
help='critical threshold until the mail appears in the INBOX. Default: %(default)s')
imap_parser_group.add_argument('--imap_timeout', dest='imap_timeout', action='store',
required=False,
default=30,
metavar="<sec>",
type=int,
help='timeout to stop waiting for a mail to appear in the INBOX (not implemented yet). Default: %(default)s')
args = parser.parse_args()
# }}}
# Read Config {{{
c = configparser.ConfigParser()
c.read(args.config_file)
a={}
for s in c.sections():
a[s] = Account(s)
a[s].set_debug(args.debug)
# This has to be more easy...
a[s].smtp_server = c.get(s, 'smtp_server')
a[s].imap_server = c.get(s, 'imap_server')
a[s].password = c.get(s, 'password')
a[s].login = c.get(s, 'login')
a[s].email = c.get(s, 'email')
a[s].smtp_timeout = args.smtp_timeout
a[s].imap_timeout = args.imap_timeout
# FIXME: Really, really Ugly!
a[s].smtp_port = 25
try:
a[s].smtp_over_ssl = c.get(s, 'smtp_over_ssl')
a[s].smtp_port = 465
except:
pass
try:
a[s].smtp_port = c.get(s, 'smtp_port')
except:
pass
a[s].imap_folder = 'INBOX'
try:
a[s].imap_folder = c.get(s, 'imap_folder')
except:
pass
# }}}
### Here the real work begins ###
# Create the stopwatches.
smtp_time = Stopwatch()
imap_time = Stopwatch()
# send the mail by SMTP
smtp_time.start()
smtp_result = a[args.sender].send(a[args.rcpt])
smtp_time.stop()
if args.debug:
print(smtp_result)
if smtp_result:
# Receive the mail.
imap_time.start()
imap_result = a[args.rcpt].check(smtp_result, stopwatch=imap_time)
imap_time.stop()
### Present the results
if not args.nagios:
# Default output
print("SMTP, (%s) time to send the mail: %.3f sec." % (args.sender, smtp_time.counter, ))
print("IMAP, (%s) time until the mail appeared in the destination mailbox/%s: %.3f sec." % (args.rcpt, a[args.rcpt].current_folder, imap_time.counter, ))
else:
# Nagios output
# this could be beautified...
nagios_code = ('OK', 'WARNING', 'CRITICAL', 'UNKNOWN' )
if ((smtp_time.counter >= args.smtp_crit) or (imap_time.counter >= args.imap_crit)):
returncode = 2
elif ((smtp_time.counter >= args.smtp_warn) or (imap_time.counter >= args.imap_warn) or (a[args.rcpt].current_folder != 'INBOX')):
returncode = 1
else:
returncode = 0
if not smtp_result: # if it failed
returncode = int(args.except_return)
error_string = a[args.sender].error_string
nagios_template="%s: (%s->%s) SMTP failed in %.3f sec, NOT received %s in %.3f sec (%s)|smtp=%.3f;%.3f;%.3f imap=%.3f;%.3f;%.3f"
elif not imap_result: # if it failed
error_string = a[args.rcpt].error_string
returncode = int(args.except_return)
nagios_template="%s: (%s->%s) sent in %.3f sec, IMAP failed, NOT received %s in %.3f sec (%s)|smtp=%.3f;%.3f;%.3f imap=%.3f;%.3f;%.3f"
else:
error_string=""
nagios_template="%s: (%s->%s) sent in %.3f sec, received in %s in %.3f sec%s|smtp=%.3f;%.3f;%.3f imap=%.3f;%.3f;%.3f"
print(nagios_template % (
nagios_code[returncode],
args.sender,
args.rcpt,
smtp_time.counter,
a[args.rcpt].current_folder,
imap_time.counter,
error_string,
smtp_time.counter,
args.smtp_warn,
args.smtp_crit,
imap_time.counter,
args.imap_warn,
args.imap_crit,
))
sys.exit(returncode)
## vim:fdm=marker:ts=4:sw=4:sts=4:ai:sta:et