-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeminfo.py
More file actions
140 lines (114 loc) · 4.33 KB
/
meminfo.py
File metadata and controls
140 lines (114 loc) · 4.33 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
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
#!/usr/bin/env python3
"""meminfo plugin for sysmon"""
from util.util import en_open
from util.logger import setup_logger
class Meminfo:
"""
Meminfo class - get physical and swap memory usage
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.meminfo_file = en_open("/proc/meminfo")
self.logger.debug("[init] /proc/meminfo")
self.files_opened = [self.meminfo_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)
# thanks to https://stackoverflow.com/a/28161352
meminfo_data = dict(
(i.split()[0].rstrip(":"), int(i.split()[1]) * 1024)
for i in self.meminfo_file.readlines()
)
phy_memory_total = meminfo_data.get("MemTotal", 0)
phy_memory_available = meminfo_data.get("MemAvailable", 0)
phy_memory_free = meminfo_data.get("MemFree", 0)
phy_memory_raw_cached = meminfo_data.get("Cached", 0)
phy_memory_sreclaimable = meminfo_data.get("SReclaimable", 0)
phy_memory_buffers = meminfo_data.get("Buffers", 0)
phy_memory_cached = (
phy_memory_raw_cached + phy_memory_buffers + phy_memory_sreclaimable
)
phy_memory_actual_used = round(
phy_memory_total
- phy_memory_free
- phy_memory_buffers
- phy_memory_raw_cached
- phy_memory_sreclaimable
)
phy_memory_used = round(phy_memory_total - phy_memory_available)
phy_memory_used_percent = round(
(int(phy_memory_used) / int(phy_memory_total)) * 100, 1
)
swap_memory_total = meminfo_data.get("SwapTotal", 0)
swap_memory_free = meminfo_data.get("SwapFree", 0)
swap_memory_cached = meminfo_data.get("SwapCached", 0)
swap_memory_used = round(swap_memory_total - swap_memory_free)
try:
swap_memory_used_percent = round(
(int(swap_memory_used) / int(swap_memory_total)) * 100, 1
) # TODO: fix ZeroDivisionError
except ZeroDivisionError:
swap_memory_used_percent = 0
data = {
"physical": {
"values": {
"total": phy_memory_total,
"used": phy_memory_used,
"actual_used": phy_memory_actual_used,
"available": phy_memory_available,
"free": phy_memory_free,
"cached": phy_memory_cached,
},
"percentage": {
"used": phy_memory_used_percent,
"actual_used": round(
(int(phy_memory_actual_used) / int(phy_memory_total)) * 100, 1
),
"available": round(100 - phy_memory_used_percent, 1),
"free": round(
(int(phy_memory_free) / int(phy_memory_total)) * 100, 1
),
"cached": round((phy_memory_cached / phy_memory_total) * 100, 1),
},
},
"virtual": {
"values": {
"total": swap_memory_total,
"used": swap_memory_used,
"free": swap_memory_free,
"cached": swap_memory_cached,
},
"percentage": {
"used": swap_memory_used_percent,
"free": round(100 - swap_memory_used_percent, 1),
},
},
}
return data