forked from isavcic/check_imap_login
/
check_imap_login
executable file
·93 lines (75 loc) · 2.24 KB
/
check_imap_login
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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# -*- Mode: Python -*-
#
# Copyright (C) 2006 Bertera Pietro <pietro@bertera.it>
# Response time monitoring with perfdata modification by Ivan Savcic <isavcic@gmail.com> and Milos Buncic, 2012.
# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
import sys, os, imaplib, getopt, ssl
from time import time
def usage():
print(sys.argv[0] + " -u <user> -p <password> -H <host> [-P <port>] [-s] -w <warning threshold (sec)> -c <critical threshold (sec)>\n -s is for using IMAPS")
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "u:p:sH:P:w:c:")
except getopt.GetoptError:
usage()
return 3
user = host = port = password = use_ssl = warning = critical = None
for o, a in opts:
if o == "-u":
user = a
elif o == "-p":
password = a
elif o == "-s":
use_ssl = True
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
ssl_ctx.check_hostname = True
ssl_ctx.load_default_certs()
elif o == "-H":
host = a
elif o == "-P":
port = a
elif o == "-w":
warning = float(a)
elif o == "-c":
critical = float(a)
if user == None or password == None or host == None or warning == None or critical == None:
usage()
return 1
if port == None:
port = 143
try:
if use_ssl:
M = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=ssl_ctx)
else:
M = imaplib.IMAP4(host, port=port)
except Exception as e:
print('CRITICAL IMAP connect Failed: %s' % e)
return 2
timestamp = time()
try:
M.login(user, password)
except Exception as e:
print("CRITICAL IMAP Login Failed: %s" % e)
return 2
M.logout()
timestamp = time() - timestamp
if timestamp < warning:
status = "OK"
exitcode = 0
if timestamp >= warning:
status = "WARNING"
exitcode = 1
if timestamp >= critical:
status = "CRITICAL"
exitcode = 2
print('%s IMAP Login | response_time=%.3fs;%.3f;%.3f' % (status, timestamp, warning, critical))
return exitcode
if __name__ == "__main__":
sys.exit(main())