-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathsystemctl_info.py
More file actions
61 lines (49 loc) · 1.79 KB
/
systemctl_info.py
File metadata and controls
61 lines (49 loc) · 1.79 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
import os
import subprocess
service_enabled_cache = {}
#==================================
# Service Status, Enabled, Logs, etc...
#==================================
def clear_service_enabled_cache():
global service_enabled_cache
service_enabled_cache = {}
def is_service_enabled(service_name):
global service_enabled_cache
if service_name in service_enabled_cache:
return service_enabled_cache[service_name]
code = os.system("systemctl is-enabled {} > /dev/null".format(service_name))
if code == 0:
service_enabled_cache[service_name] = True
return True
service_enabled_cache[service_name] = False
return False
def is_service_active(service_name):
try:
if subprocess.check_output("systemctl is-active {}".format(service_name), shell=True).decode('utf8') == 'active\n':
return True
except:
return False
return False
def get_service_status_code(service_name):
code = os.system("systemctl status {} --no-pager > /dev/null".format(service_name))
return code
def get_service_status_basic_text(service_name):
if not is_service_enabled(service_name):
return "Disabled"
code = os.systm("systemctl status {} --no-pager > /dev/null".format(service_name))
if code == 0:
return "Running"
return "Error"
def get_service_status_color(service_name):
if not is_service_enabled(service_name):
return "gray"
code = os.system("systemctl status {} --no-pager > /dev/null".format(service_name))
if code == 0:
return "green"
return "red"
def get_journalctl_log(service_name):
try:
log = subprocess.check_output("journalctl -r --unit={} --no-pager | head -n 300".format(service_name), shell=True).decode("utf8")
except:
log = "ERROR"
return log