-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadavg.py
90 lines (67 loc) · 2.22 KB
/
loadavg.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
#!/usr/bin/env python3
"""loadavg plugin for sysmon"""
import time
from datetime import datetime
from util.util import en_open
from util.logger import setup_logger
class Loadavg:
"""
Loadavg class - get load times and uptime
Usage:
call get_data() to get data
returns dict
DO:
NOT CALL print_data(). That function
is intended to be used by sysmon. (might change in the future...?)
CALL close_files() when your program ends
to avoid opened files
"""
def __init__(self):
"""
initializing important stuff
"""
self.logger = setup_logger(__name__)
self.logger.debug("[init] initializing")
self.loadavg_file = en_open("/proc/loadavg")
self.logger.debug("[init] /proc/loadavg")
self.uptime_file = en_open("/proc/uptime")
self.logger.debug("[init] /proc/uptime")
self.files_opened = [self.loadavg_file, self.uptime_file]
def close_files(self):
"""
closing the opened files. always call this
when ending the program
"""
for file in self.files_opened:
try:
self.logger.debug(f"[close_files] {file.name}")
file.close()
except:
pass
def get_data(self):
"""
returns a json dict with data
"""
for file in self.files_opened:
self.logger.debug(f"[get_data] {file.name}")
file.seek(0)
loadavg_data = self.loadavg_file.read().split()
uptime_seconds = int(float(self.uptime_file.readline().split()[0]))
uptime_timestamp = time.time() - uptime_seconds
data = {
"load_times": {
"1": float(loadavg_data[0]),
"5": float(loadavg_data[1]),
"15": float(loadavg_data[2]),
},
"entities": {
"active": int(loadavg_data[3].split("/")[0]),
"total": int(loadavg_data[3].split("/")[1]),
},
"uptime": {
"timestamp": uptime_timestamp,
"seconds": uptime_seconds,
},
}
self.logger.debug("[get_data] return data")
return data