forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemdsvc.py
More file actions
65 lines (45 loc) · 1.57 KB
/
systemdsvc.py
File metadata and controls
65 lines (45 loc) · 1.57 KB
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
#!/usr/bin/python
from subprocess import Popen, PIPE
import sys
from pynag.Plugins import PluginHelper
def main():
p = PluginHelper()
# Warn on inactive
level = 2
service_status = get_service_status(sys.argv[1])
if loaded(service_status)[0] is False:
p.exit(3,
"%s - %s" % (service_status['name'],
loaded(service_status)[1]),
"\n" + service_status['unparsed'])
active = service_status['headers']['Active'][0]
if active.startswith("inactive") or active.startswith('failed'):
p.add_status(level)
elif active.startswith("active"):
p.add_status(0)
else:
p.add_status(3)
p.add_summary("%s - %s" % ( service_status['name'], active))
p.add_long_output("\n" + service_status['unparsed'])
p.exit()
def loaded(stat):
if stat['headers']['Loaded']:
if stat['headers']['Loaded'][0].startswith("error"):
return False, stat['headers']['Loaded'][0]
return True, stat['headers']['Loaded'][0]
def get_service_status(service):
stdout = Popen(["systemctl", "status", service], stdout=PIPE).communicate()[0]
stdout_lines = stdout.split("\n")
name = stdout_lines.pop(0).split()[0]
headers = {}
while len(stdout_lines):
l = stdout_lines.pop(0).strip()
if l == "":
break
k, v = l.split(': ', 1)
if k in headers:
headers[k].append(v)
else:
headers[k] = [v]
return {"name": name, "headers": headers, "journal": stdout_lines, "unparsed": stdout}
main()