-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_mail_aliases
127 lines (110 loc) · 4.71 KB
/
check_mail_aliases
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
##aptitude install python-nagiosplugin
import os
import sys
import subprocess
import argparse
#-------------------------------------------------------------------------------
_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)
#-------------------------------------------------------------------------------
def isfinal(parts):
finals = ["localaccount@localhost"]
return parts[0] in finals
def isexclusion(parts,customlines):
exceptions = []+customlines
exceptions.append(["mailer-daemon", "postmaster@localhost"])
exceptions.append(["root", "localaccount@localhost"])
#infomsg(exceptions)
for exception in exceptions:
if (exception[0] == parts[0]) and (exception[1] == parts[1]):
return True
return False
def isexpected(parts):
return parts[1] == "root@localhost"
def domailaliasescall(customlines):
try:
aliasesfile = "/etc/aliases"
if os.path.isfile(aliasesfile):
errors = []
exclusion = []
expected = []
handle = open(aliasesfile,"r")
try:
lines = handle.read().splitlines()
for line in lines:
line = line.strip()
if len(line)>0:
if line[0] not in [";","#"]:
parts = line.split(":",1)
if len(parts)==2:
parts[0] = parts[0].strip()
parts[1] = parts[1].strip()
if parts[1].endswith("@localhost") == False:
infomsg("ERROR -> "+str(parts))
errors.append(line)
elif isfinal(parts) == True:
infomsg("ERROR -> "+str(parts))
errors.append(line)
elif isexclusion(parts,customlines):
infomsg("EXCLUSION -> "+str(parts))
exclusion.append(line)
elif isexpected(parts):
infomsg("EXPECTED -> "+str(parts))
expected.append(line)
else:
infomsg("ERROR -> "+str(parts))
errors.append(line)
else:
errors.append(line)
#infomsg("->"+line+"<-")
finally:
handle.close()
if len(errors) == 0:
exitnagios("OK","no issues found in "+aliasesfile+" | errors="+str(len(errors))+" exclusion="+str(len(exclusion))+" expected="+str(len(expected)))
else:
exitnagios("CRITICAL","issues found in "+aliasesfile+" -> "+" - ".join(errors)+" | errors="+str(len(errors))+" exclusion="+str(len(exclusion))+" expected="+str(len(expected)))
else:
exitnagios("CRITICAL","cannot find "+aliasesfile+" | errors=-1 exclusion=-1 expected=-1")
except Exception as e:
exitnagios("CRITICAL","unexpected exception "+str(e)+" | errors=-1 exclusion=-1 expected=-1")
#-------------------------------------------------------------------------------
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
parser.add_argument("-l", "--customlines", dest="customlines", default="", help="expected custom lines")
args = parser.parse_args()
return args
def main():
args = parse_args()
setcandebug(args.debug)
customlines = []
for entry in args.customlines.split(","):
customlines.append(entry.strip().split(":"))
infomsg("The custom lines are "+str(customlines))
domailaliasescall(customlines)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------