forked from kuba-moo/nipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system-status.py
executable file
·159 lines (127 loc) · 4.6 KB
/
system-status.py
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
#!/usr/bin/env python3
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
import datetime
import lzma
import os
import re
import sys
import subprocess
import time
import json
char_filter = re.compile(r'["<>&;\n]+')
def kv_to_dict(lines):
data = {}
for line in lines:
entry = line.split("=", 1)
if len(entry) < 2:
continue
data[entry[0]] = entry[1]
return data
def add_one_service(result, name):
lines = subprocess.check_output(["systemctl", "show", name]).decode('utf-8').split('\n')
data = kv_to_dict(lines)
keys = ['CPUUsageNSec', 'MemoryCurrent', 'ActiveState', 'SubState', 'TasksCurrent', 'TriggeredBy', 'Result']
filtered = {}
for k in keys:
filtered[k] = data.get(k, 0)
result['services'][name] = filtered
def add_one_tree(result, pfx, name):
global char_filter
with open(os.path.join(pfx, name), 'r') as fp:
lines = fp.readlines()
last = None
sub = ''
for line in lines:
if 'Testing patch' in line:
last = re.sub(char_filter, "", line)
sub = ''
elif 'Running test ' in line:
sub = line[17:].strip()
if 'Checking barrier' in line:
last = None
if last:
last += f' ({sub})'
result['runners'][name] = last
def add_one_runtime(fname, total, res):
if fname.endswith('.xz'):
with lzma.open(fname) as fp:
lines = fp.readlines()
else:
with open(fname, 'r') as fp:
lines = fp.readlines()
t = {"start": None, "end": None}
test = None
cont = None
for line in lines:
if isinstance(line, bytes):
line = line.decode('utf-8')
if cont:
t[cont] = line.strip()
if cont == "end":
if test and t["start"]:
comb = datetime.datetime.combine
today = datetime.date.today()
start = comb(today, datetime.time.fromisoformat(t["start"]))
end = comb(today, datetime.time.fromisoformat(t["end"]))
if end < start:
start -= datetime.timedelta(days=1)
sec = (end - start).total_seconds()
if test not in res:
res[test] = {"cnt": 0, "sum": 0}
res[test]["cnt"] += 1
res[test]["sum"] += sec
total += sec
test = None
cont = None
if '* Running test ' in line:
test = line[line.find("Running test") + 13:].strip()
t = {"start": None, "end": None}
elif '**** START' in line:
cont = "start"
elif '**** END' in line:
cont = "end"
return total, res
def add_runtime(result, cfg):
reg = re.compile(cfg["regex"])
total = 0
res = {}
for f in os.listdir(cfg["path"]):
if not reg.match(f):
continue
fname = os.path.join(cfg["path"], f)
if time.time() - os.path.getmtime(fname) > (5 * 24 * 60 * 60):
continue
print("Building runtime from log", fname)
total, res = add_one_runtime(fname, total, res)
res = {k: {"pct": res[k]["sum"] / total * 100, "avg": res[k]["sum"] / res[k]["cnt"]} for k in res}
return res
def main():
with open(sys.argv[1], 'r') as fp:
cfg = json.load(fp)
run_logs = True
if os.path.isfile(sys.argv[2]):
with open(sys.argv[2], 'r') as fp:
prev = json.load(fp)
if "log-files" in prev and "prev-date" in prev["log-files"]:
prev_date = datetime.datetime.fromisoformat(prev["log-files"]["prev-date"])
run_logs = datetime.datetime.now() - prev_date > datetime.timedelta(hours=3)
print("Since log scan", datetime.datetime.now() - prev_date)
prev_date = prev["log-files"]["prev-date"]
log_files = {"prev-date": prev_date, "data": prev["log-files"]["data"]}
if run_logs:
prev_date = datetime.datetime.now().isoformat()
log_files = {"prev-date": prev_date, }
result = {'services': {}, 'runners': {},
'date': datetime.datetime.now().isoformat(),
"log-files": log_files}
for name in cfg["services"]:
add_one_service(result, name)
for name in cfg["trees"]:
add_one_tree(result, cfg["tree-path"], name)
if "log-files" in cfg and run_logs:
res = add_runtime(result, cfg["log-files"])
result["log-files"]["data"] = res
with open(sys.argv[2], 'w') as fp:
json.dump(result, fp)
if __name__ == "__main__":
main()