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
|
#!/usr/bin/env python3
import cgi, cgitb
import json
import os
import socket
import subprocess
from subprocess import PIPE, STDOUT
def headers():
print("Content-Type: text/html; charset=utf-8")
print()
def debugging():
# TODO: figure out how to include this on the web page
#cgitb.enable()
pass
def setup():
headers()
debugging()
def format_response(response):
return json.dumps(response, ensure_ascii=False)
def run(*args, **kwargs):
output = subprocess.run(args, stdout=PIPE, stderr=STDOUT, universal_newlines=True, **kwargs)
return output.stdout
def hostname():
return socket.gethostname()
def top():
return run('top', '-b', '-n', '1', '-w', '512')
systemd_env = os.environ.copy()
systemd_env['SYSTEMD_PAGER'] = ''
systemd_env['SYSTEMD_COLORS'] = 'no'
def run_systemd_command(*args):
return run(*args, env=systemd_env)
def run_systemctl(*args):
return run_systemd_command('systemctl', *args)
def system_status_overview():
return run_systemctl('--system', 'status', '--full')
def system_status_failed():
return run_systemctl('--system', 'list-units', '--failed', '--full')
def user_status_overview():
return run_systemctl('--user', 'status', '--full')
def user_status_failed():
return run_systemctl('--user', 'list-units', '--failed', '--full')
def system_timers():
return run_systemctl('--system', 'list-timers', '--all', '--full')
def user_timers():
return run_systemctl('--user', 'list-timers', '--all', '--full')
def status():
status = {
'hostname': hostname(),
'top': top(),
'system': {
'overview': system_status_overview(),
'failed': system_status_failed(),
'timers': system_timers(),
},
'user': {
'overview': user_status_overview(),
'failed': user_status_failed(),
'timers': user_timers(),
},
}
return status
def timers():
timers = {
'system': {
'timers': system_timers(),
},
'user': {
'timers': user_timers(),
},
}
return timers
def do():
params = cgi.FieldStorage()
what = params['what'].value
if what == 'status':
response = status()
elif what == 'timers':
response = timers()
elif what == 'top':
response = top()
else:
raise RuntimeError(f'invalid parameter "what": {what}')
print(format_response(response))
def main():
setup()
do()
if __name__ == '__main__':
main()
|