-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloadavg.py
More file actions
70 lines (48 loc) · 1.57 KB
/
loadavg.py
File metadata and controls
70 lines (48 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
65
66
67
68
69
70
#!/usr/bin/env python3
"""
formatter for loadavg plugin
"""
from datetime import datetime
from plugins import loadavg
loadavg_class = loadavg.Loadavg()
def main():
"""
returns the data, but formatted.
"""
data = loadavg_class.get_data()
intervals = (("week", 604800), ("day", 86400), ("hour", 3600), ("minute", 60))
result = []
seconds = data["uptime"]["seconds"]
original_seconds = seconds
if seconds < 60:
result.append(f"{seconds} seconds")
else:
for time_type, count in intervals:
value = seconds // count
if value:
seconds -= value * count
result.append(f"{value} {time_type if value == 1 else time_type + 's'}")
if len(result) > 1:
result[-1] = "and " + result[-1]
uptime_formatted = ", ".join(result)
uptime_since = datetime.fromtimestamp(data["uptime"]["timestamp"]).strftime(
"%A, %B %d %Y, %I:%M:%S %p"
)
output_list = [
f" --- /proc/loadavg {'-' * 47}\n",
f" Load: {data['load_times']['1']}, {data['load_times']['5']}, {data['load_times']['15']}",
f"\n Uptime: {uptime_formatted}\n Booted: {uptime_since}\n",
]
output_list[1] = output_list[1] = (
output_list[1]
+ " " * max(0, 35 - len(output_list[1]))
+ f"| Procs: {data['entities']['active']} active, {data['entities']['total']} total"
)
return "".join(output_list)
def end():
"""
clean up when the program exits
"""
loadavg_class.close_files()
if __name__ == "__main__":
main()