-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_recent_maildir
395 lines (371 loc) · 12.5 KB
/
check_recent_maildir
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
389
390
391
392
393
394
395
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import sys
import os
import argparse
import subprocess
import mailbox
import email.errors
import datetime
import dateutil.parser
#-------------------------------------------------------------------------------
_candebug = False
def candebug():
global _candebug
return _candebug
def setcandebug(value):
global _candebug
_candebug = value
def infomsg(msg):
if candebug() == True:
print(msg, flush=True)
def exitnagios(status,message):
if status=="OK":
exitcode = 0
elif status=="WARNING":
exitcode = 1
elif status=="CRITICAL":
exitcode = 2
elif status=="UNKNOWN":
exitcode = 3
else:
exitcode = 4
print(status+": "+message, flush=True)
sys.exit(exitcode)
#-------------------------------------------------------------------------------
extra_tz = None
def get_extra_tz():
global extra_tz
if extra_tz == None:
extra_tz = {
"A": 1 * 3600,
"ACDT": 10.5 * 3600,
"ACST": 9.5 * 3600,
"ACT": -5 * 3600,
"ACWST": 8.75 * 3600,
"ADT": 4 * 3600,
"AEDT": 11 * 3600,
"AEST": 10 * 3600,
"AET": 10 * 3600,
"AFT": 4.5 * 3600,
"AKDT": -8 * 3600,
"AKST": -9 * 3600,
"ALMT": 6 * 3600,
"AMST": -3 * 3600,
"AMT": -4 * 3600,
"ANAST": 12 * 3600,
"ANAT": 12 * 3600,
"AQTT": 5 * 3600,
"ART": -3 * 3600,
"AST": 3 * 3600,
"AT": -4 * 3600,
"AWDT": 9 * 3600,
"AWST": 8 * 3600,
"AZOST": 0 * 3600,
"AZOT": -1 * 3600,
"AZST": 5 * 3600,
"AZT": 4 * 3600,
"AoE": -12 * 3600,
"B": 2 * 3600,
"BNT": 8 * 3600,
"BOT": -4 * 3600,
"BRST": -2 * 3600,
"BRT": -3 * 3600,
"BST": 6 * 3600,
"BTT": 6 * 3600,
"C": 3 * 3600,
"CAST": 8 * 3600,
"CAT": 2 * 3600,
"CCT": 6.5 * 3600,
"CDT": -5 * 3600,
"CEST": 2 * 3600,
"CET": 1 * 3600,
"CHADT": 13.75 * 3600,
"CHAST": 12.75 * 3600,
"CHOST": 9 * 3600,
"CHOT": 8 * 3600,
"CHUT": 10 * 3600,
"CIDST": -4 * 3600,
"CIST": -5 * 3600,
"CKT": -10 * 3600,
"CLST": -3 * 3600,
"CLT": -4 * 3600,
"COT": -5 * 3600,
"CST": -6 * 3600,
"CT": -6 * 3600,
"CVT": -1 * 3600,
"CXT": 7 * 3600,
"ChST": 10 * 3600,
"D": 4 * 3600,
"DAVT": 7 * 3600,
"DDUT": 10 * 3600,
"E": 5 * 3600,
"EASST": -5 * 3600,
"EAST": -6 * 3600,
"EAT": 3 * 3600,
"ECT": -5 * 3600,
"EDT": -4 * 3600,
"EEST": 3 * 3600,
"EET": 2 * 3600,
"EGST": 0 * 3600,
"EGT": -1 * 3600,
"EST": -5 * 3600,
"ET": -5 * 3600,
"F": 6 * 3600,
"FET": 3 * 3600,
"FJST": 13 * 3600,
"FJT": 12 * 3600,
"FKST": -3 * 3600,
"FKT": -4 * 3600,
"FNT": -2 * 3600,
"G": 7 * 3600,
"GALT": -6 * 3600,
"GAMT": -9 * 3600,
"GET": 4 * 3600,
"GFT": -3 * 3600,
"GILT": 12 * 3600,
"GMT": 0 * 3600,
"GST": 4 * 3600,
"GYT": -4 * 3600,
"H": 8 * 3600,
"HDT": -9 * 3600,
"HKT": 8 * 3600,
"HOVST": 8 * 3600,
"HOVT": 7 * 3600,
"HST": -10 * 3600,
"I": 9 * 3600,
"ICT": 7 * 3600,
"IDT": 3 * 3600,
"IOT": 6 * 3600,
"IRDT": 4.5 * 3600,
"IRKST": 9 * 3600,
"IRKT": 8 * 3600,
"IRST": 3.5 * 3600,
"IST": 5.5 * 3600,
"JST": 9 * 3600,
"K": 10 * 3600,
"KGT": 6 * 3600,
"KOST": 11 * 3600,
"KRAST": 8 * 3600,
"KRAT": 7 * 3600,
"KST": 9 * 3600,
"KUYT": 4 * 3600,
"L": 11 * 3600,
"LHDT": 11 * 3600,
"LHST": 10.5 * 3600,
"LINT": 14 * 3600,
"M": 12 * 3600,
"MAGST": 12 * 3600,
"MAGT": 11 * 3600,
"MART": 9.5 * 3600,
"MAWT": 5 * 3600,
"MDT": -6 * 3600,
"MHT": 12 * 3600,
"MMT": 6.5 * 3600,
"MSD": 4 * 3600,
"MSK": 3 * 3600,
"MST": -7 * 3600,
"MT": -7 * 3600,
"MUT": 4 * 3600,
"MVT": 5 * 3600,
"MYT": 8 * 3600,
"N": -1 * 3600,
"NCT": 11 * 3600,
"NDT": 2.5 * 3600,
"NFT": 11 * 3600,
"NOVST": 7 * 3600,
"NOVT": 7 * 3600,
"NPT": 5.5 * 3600,
"NRT": 12 * 3600,
"NST": 3.5 * 3600,
"NUT": -11 * 3600,
"NZDT": 13 * 3600,
"NZST": 12 * 3600,
"O": -2 * 3600,
"OMSST": 7 * 3600,
"OMST": 6 * 3600,
"ORAT": 5 * 3600,
"P": -3 * 3600,
"PDT": -7 * 3600,
"PET": -5 * 3600,
"PETST": 12 * 3600,
"PETT": 12 * 3600,
"PGT": 10 * 3600,
"PHOT": 13 * 3600,
"PHT": 8 * 3600,
"PKT": 5 * 3600,
"PMDT": -2 * 3600,
"PMST": -3 * 3600,
"PONT": 11 * 3600,
"PST": -8 * 3600,
"PT": -8 * 3600,
"PWT": 9 * 3600,
"PYST": -3 * 3600,
"PYT": -4 * 3600,
"Q": -4 * 3600,
"QYZT": 6 * 3600,
"R": -5 * 3600,
"RET": 4 * 3600,
"ROTT": -3 * 3600,
"S": -6 * 3600,
"SAKT": 11 * 3600,
"SAMT": 4 * 3600,
"SAST": 2 * 3600,
"SBT": 11 * 3600,
"SCT": 4 * 3600,
"SGT": 8 * 3600,
"SRET": 11 * 3600,
"SRT": -3 * 3600,
"SST": -11 * 3600,
"SYOT": 3 * 3600,
"T": -7 * 3600,
"TAHT": -10 * 3600,
"TFT": 5 * 3600,
"TJT": 5 * 3600,
"TKT": 13 * 3600,
"TLT": 9 * 3600,
"TMT": 5 * 3600,
"TOST": 14 * 3600,
"TOT": 13 * 3600,
"TRT": 3 * 3600,
"TVT": 12 * 3600,
"U": -8 * 3600,
"ULAST": 9 * 3600,
"ULAT": 8 * 3600,
"UTC": 0 * 3600,
"UYST": -2 * 3600,
"UYT": -3 * 3600,
"UZT": 5 * 3600,
"V": -9 * 3600,
"VET": -4 * 3600,
"VLAST": 11 * 3600,
"VLAT": 10 * 3600,
"VOST": 6 * 3600,
"VUT": 11 * 3600,
"W": -10 * 3600,
"WAKT": 12 * 3600,
"WARST": -3 * 3600,
"WAST": 2 * 3600,
"WAT": 1 * 3600,
"WEST": 1 * 3600,
"WET": 0 * 3600,
"WFT": 12 * 3600,
"WGST": -2 * 3600,
"WGT": -3 * 3600,
"WIB": 7 * 3600,
"WIT": 9 * 3600,
"WITA": 8 * 3600,
"WST": 14 * 3600,
"WT": 0 * 3600,
"X": -11 * 3600,
"Y": -12 * 3600,
"YAKST": 10 * 3600,
"YAKT": 9 * 3600,
"YAPT": 10 * 3600,
"YEKST": 6 * 3600,
"YEKT": 5 * 3600,
"Z": 0 * 3600,
}
return extra_tz
def most_recent_date_simple(inbox):
referencedate = None
infomsg(inbox)
try:
inbox.clean()
for key in inbox.iterkeys():
infomsg(key)
try:
message = inbox[key]
except email.errors.MessageParseError:
# The message is malformed. Just leave it.
continue
date = message["Date"]
try:
candidatedate = dateutil.parser.parse(date, tzinfos=get_extra_tz())
except:
candidatedate = None
infomsg(candidatedate)
if (candidatedate!=None) and ((referencedate==None) or (candidatedate>referencedate)):
referencedate = candidatedate
except Exception as e:
exitnagios("CRITICAL","there was an exception - "+str(e))
return referencedate
def most_recent_date_recursive(inbox):
referencedate = most_recent_date_simple(inbox)
allfolders = inbox.list_folders()
for folder in allfolders:
nestedinbox = inbox.get_folder(folder)
candidatedate = most_recent_date_recursive(nestedinbox)
if (candidatedate!=None) and ((referencedate==None) or (candidatedate>referencedate)):
referencedate = candidatedate
infomsg(referencedate)
return referencedate
def get_home_path(username):
cmdline = ["/usr/bin/getent","passwd",username]
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8").strip()
errors = completedproc.stderr.decode("utf-8").strip()
exitcode = completedproc.returncode
if exitcode == 0:
try:
parts = output.splitlines()[0].split(":")
return parts[5]
except:
return None
else:
return None
def dorecentmaildircall(username,warning,critical):
try:
if warning==None or warning=="":
warning = 48
else:
warning = float(warning)
if critical==None or critical=="":
critical = 72
else:
critical = float(critical)
userhomepath = get_home_path(username)
if userhomepath==None or userhomepath=="" or os.path.isdir(userhomepath)==False:
exitnagios("CRITICAL","the home folder '"+userhomepath+"' is not valid | maxage=-1")
maildirpath = os.path.join(userhomepath,"Maildir")
infomsg(maildirpath)
inbox = mailbox.Maildir(maildirpath, factory=None)
newest_date = most_recent_date_recursive(inbox)
newest_date = newest_date.astimezone(datetime.timezone.utc)
infomsg(newest_date)
if (newest_date)==None:
exitnagios("CRITICAL","no emails found | maxage=-1")
else:
now = datetime.datetime.now(datetime.timezone.utc)
warninglimit = now - datetime.timedelta(hours=warning)
criticallimit = now - datetime.timedelta(hours=critical)
maxage = now - newest_date
if newest_date < criticallimit:
exitnagios("CRITICAL","no email in the last "+str(critical)+" hours - newest is "+str(newest_date)+" | maxage="+str(maxage.total_seconds()))
elif newest_date < warninglimit:
exitnagios("WARNING","no email in the last "+str(warning)+" hours - newest is "+str(newest_date)+" | maxage="+str(maxage.total_seconds()))
else:
exitnagios("OK","no issues found - newest is "+str(newest_date)+" | maxage="+str(maxage.total_seconds()))
# Remove original message
#inbox.lock()
#inbox.discard(key)
#inbox.flush()
#inbox.unlock()
except Exception as e:
exitnagios("CRITICAL","there was an exception - "+str(e)+" | maxage=-1")
#-------------------------------------------------------------------------------
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", action="store", dest="username", default="", help="username to check")
parser.add_argument("-w", "--warning", action="store", dest="warning", default="", help="warning hours")
parser.add_argument("-c", "--critical", action="store", dest="critical", default="", help="critical hours")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
return args
def main():
args = parse_args()
setcandebug(args.debug)
dorecentmaildircall(args.username,args.warning,args.critical)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------