-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_getent
95 lines (81 loc) · 3.75 KB
/
check_getent
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import os
import sys
import subprocess
import re
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 checkgetent(database, key, value, partial, outputlevel):
command = ["/usr/bin/getent", database]
infomsg(str(command))
output = ""
try:
result = subprocess.check_output(command)
output = result.decode("utf-8")
except Exception as e:
exitnagios("CRITICAL","exception during getent call")
if output == None:
exitnagios("CRITICAL","no output from getent")
else:
lines = output.splitlines()
isok = False
found = False
for line in lines:
infomsg("Evaluating line - "+line)
if line.startswith(key+":"):
found = True
if ((value==None) or (value=="")):
exitnagios("OK","found key and no expected value was set - "+line)
elif (line == key+value):
exitnagios("OK","found fully matching expected line "+line)
elif partial and (line.startswith(key+value)):
exitnagios("OK","found partial match with expected line "+line)
else:
exitnagios(outputlevel,"found unexpected line "+line+" but it was expected "+key+value)
break
if found == False:
exitnagios("CRITICAL","key not found "+key)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--database", dest="database", help="database to use (passwod, group, ...)")
parser.add_argument("-k", "--key", dest="key", help="key to search")
parser.add_argument("-l", "--value", dest="value", default=None, help="expected value")
parser.add_argument("-p", "--partial", action="store_true", dest="partial", default=False, help="is partial match")
parser.add_argument("-w", "--warning", action="store_false", dest="critical", help="Return warning if not matching")
parser.add_argument("-c", "--critical", action="store_true", dest="critical", default=True, help="Return critical if not matching (default)")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
if args.critical==True:
outputlevel = "CRITICAL"
else:
outputlevel = "WARNING"
result = checkgetent(args.database, args.key, args.value, args.partial, outputlevel)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------