-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_systemd
186 lines (163 loc) · 7.3 KB
/
check_systemd
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import os
import subprocess
import pwd
import time
#-------------------------------------------------------------------------------
_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 execute_systemd_call(system,service,user):
cmdline = ["/usr/bin/systemctl"]
if not system:
cmdline = cmdline + ["-M",user+"@"]
if system:
cmdline = cmdline + ["--system"]
else:
cmdline = cmdline + ["--user"]
if service!=None:
cmdline = cmdline + ["is-active", service]
else:
cmdline = cmdline + ["--no-legend"]
infomsg(" ".join(cmdline))
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8").strip()
errors = completedproc.stderr.decode("utf-8").strip()
exitcode = completedproc.returncode
infomsg(output)
infomsg(errors)
infomsg(exitcode)
return output
#-------------------------------------------------------------------------------
def parse_systemd_list(output,toleratelist,ignorelist):
oks = {}
warnings = {}
criticals = {}
for line in output.splitlines():
parts = line[2:].split()
unit = parts[0]
status = parts[2]
if status == "active":
oks[unit]=status
else:
if unit in ignorelist:
pass
elif unit in toleratelist:
warnings[unit]=status
elif status in ["failed"]:
criticals[unit]=status
else:
warnings[unit]=status
return oks,warnings,criticals
def generate_dicts(system,service,user,toleratelist,ignorelist):
valid = True
oks = {}
warnings = {}
criticals = {}
infomsg("calling first")
firstoutput = execute_systemd_call(system,service,user)
if firstoutput=="":
valid = False
else:
infomsg("parsing first")
firstoks,firstwarnings,firstcriticals = parse_systemd_list(firstoutput,toleratelist,ignorelist)
if len(firstwarnings)>0 or len(firstcriticals)>0:
infomsg("waiting")
time.sleep(10)
infomsg("calling second")
secondoutput = execute_systemd_call(system,service,user)
if secondoutput=="":
valid = False
else:
infomsg("parsing second")
secondoks,secondwarnings,secondcriticals = parse_systemd_list(secondoutput,toleratelist,ignorelist)
infomsg("matching first and second")
for key,value in secondoks.items():
if key in firstoks.keys():
oks[key]=value
for key,value in secondwarnings.items():
if key in firstwarnings.keys():
warnings[key]=value
for key,value in secondcriticals.items():
if key in firstcriticals.keys():
criticals[key]=value
else:
infomsg("no need for second")
oks = firstoks
warnings = firstwarnings
criticals = firstcriticals
infomsg("finished matching dicts")
return valid,oks,warnings,criticals
def systemd_check(service,system,user,toleratelist,ignorelist):
if service==None:
valid,oks,warnings,criticals = generate_dicts(system,service,user,toleratelist,ignorelist)
if valid!=True:
exitnagios("CRITICAL","issue calling systemd")
else:
infomsg("oks -> "+str(oks))
infomsg("warnings -> "+str(warnings))
infomsg("criticals -> "+str(criticals))
if len(criticals)>0:
if len(warnings)==0:
exitnagios("CRITICAL","there are criticals "+str(criticals)+" | oks="+str(len(oks))+" warnings="+str(len(warnings))+" criticals="+str(len(criticals)))
else:
exitnagios("CRITICAL","there are criticals "+str(criticals)+" and warnings "+str(warnings)+" | oks="+str(len(oks))+" warnings="+str(len(warnings))+" criticals="+str(len(criticals)))
elif len(warnings)>0:
exitnagios("WARNING","there are warnings "+str(warnings)+" | oks="+str(len(oks))+" warnings="+str(len(warnings))+" criticals="+str(len(criticals)))
else:
exitnagios("OK","no issues found | oks="+str(len(oks))+" warnings="+str(len(warnings))+" criticals="+str(len(criticals)))
else:
output = execute_systemd_call(system,service,user)
if output=="":
exitnagios("CRITICAL","issue calling systemd")
else:
status = output
if status=="active":
exitnagios("OK","unit "+service+" is active")
elif service in ignorelist:
exitnagios("OK","unit "+service+" is ignored")
elif service in toleratelist:
exitnagios("WARNING","unit "+service+" is "+status+" instead of active")
else:
exitnagios("CRITICAL","unit "+service+" is "+status+" instead of active")
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--service", dest="service", default=None, help="Name of the Service that is beeing tested")
parser.add_argument("-m", "--system", action="store_true", dest="system", default=False, help="Check the gobal systemd (default)")
parser.add_argument("-u", "--user", dest="user", default="root", help="Check at userlevel (linger) systemd (default root)")
parser.add_argument("-l", "--tolerate", default="", help="Systemd units to mark as warning instead of critical")
parser.add_argument("-g", "--ignore", default="", help="Systemd units to fully ignore")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
toleratelist=args.tolerate.replace(" ",";").replace(",",";").split(";")
ignorelist=args.ignore.replace(" ",";").replace(",",";").split(";")
systemd_check(args.service, args.system, args.user, toleratelist, ignorelist)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------