parse /proc/meminfo to gather memory usage

This commit is contained in:
Cassiano Aquino 2019-10-24 20:29:09 +01:00
parent ec430a5cba
commit 0c176ca308
No known key found for this signature in database
GPG Key ID: 2480258091054B09

View File

@ -58,21 +58,25 @@ def uptime():
with open('/proc/uptime') as fp: with open('/proc/uptime') as fp:
return int(fp.read().split('.')[0]) return int(fp.read().split('.')[0])
def mem_usage(): def mem_usage():
out = subprocess.getoutput("free -m") with open('/proc/meminfo') as fp:
for line in out.split("\n"): for line in fp:
line = line.strip() line = line.strip()
if line.startswith("Mem:"): if line.startswith("MemTotal:"):
parts = list(map(int, line.split()[1:])) kb_mem_total = int(line.split()[1])
tot = parts[0] if line.startswith("MemFree:"):
used = parts[1] kb_mem_free = int(line.split()[1])
free = parts[2] if line.startswith("MemAvailable:"):
return used / tot kb_mem_available = int(line.split()[1])
if line.startswith("Buffers:"):
kb_main_buffers = int(line.split()[1])
if line.startswith("Cached:"):
kb_main_cached = int(line.split()[1])
kb_mem_used = kb_mem_total - kb_mem_free - kb_main_cached - kb_main_buffers
return round(kb_mem_used/kb_mem_total,2)
return 0 return 0
def cpu_load(): def cpu_load():
with open('/proc/stat', 'rt') as fp: with open('/proc/stat', 'rt') as fp:
for line in fp: for line in fp: