This repository was archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsystemd.py
More file actions
executable file
·62 lines (50 loc) · 1.39 KB
/
systemd.py
File metadata and controls
executable file
·62 lines (50 loc) · 1.39 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
#!/usr/bin/env python3
"""Script to monitor Systemd services.
"""
import subprocess
import argparse
def check_service(service_name):
"""Checks the status of a systemd service.
This function can return a value between 0 and 2.
0: service is active
1: service is not active
2: service has failed
3: service in unkown state
255: an error in the process occured
:return: int active
"""
command = [
'systemctl',
'is-active',
service_name,
]
active = 255
try:
process = subprocess.run(
command,
stdout=subprocess.PIPE)
output = str(process.stdout, 'utf-8')
output = output.replace('\n', '')
if output == 'active':
active = 0
elif output == 'inactive':
active = 1
elif output == 'failed':
active = 2
else:
active = 3
except subprocess.CalledProcessError:
active = 255
return active
def main():
"""Main function.
"""
parser = argparse.ArgumentParser()
parser.add_argument('service', type=str, nargs=1)
arguments = parser.parse_args()
service_name = arguments.service[0]
service_name = service_name.replace('--at--', '@')
service_name = service_name.replace('--backslash--', '\\')
return check_service(service_name)
if __name__ == '__main__':
print(main())