2020-03-06 14:46:33 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
"""Displays available RAM, total amount of RAM and percentage available.
|
2020-03-06 14:46:33 +01:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* memory.warning : Warning threshold in % of memory used (defaults to 80%)
|
|
|
|
* memory.critical: Critical threshold in % of memory used (defaults to 90%)
|
2020-03-06 14:48:11 +01:00
|
|
|
* memory.format: Format string (defaults to '{used}/{total} ({percent:05.02f}%)')
|
|
|
|
* memory.usedonly: Only show the amount of RAM in use (defaults to False). Same as memory.format='{used}'
|
2020-05-03 11:15:52 +02:00
|
|
|
"""
|
2020-03-06 14:46:33 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2020-03-06 14:48:11 +01:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
2020-03-06 14:46:33 +01:00
|
|
|
|
2020-03-06 14:52:16 +01:00
|
|
|
import util.format
|
2020-03-06 14:46:33 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-06 14:48:11 +01:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.memory_usage))
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.LEFT_MOUSE, cmd="gnome-system-monitor"
|
|
|
|
)
|
2020-03-06 14:46:33 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _format(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
if util.format.asbool(self.parameter("usedonly", False)):
|
|
|
|
return "{used}"
|
2020-03-06 14:46:33 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
return self.parameter("format", "{used}/{total} ({percent:05.02f}%)")
|
2020-03-06 14:46:33 +01:00
|
|
|
|
|
|
|
def memory_usage(self, widget):
|
|
|
|
return self._format.format(**self._mem)
|
|
|
|
|
2020-03-06 14:52:16 +01:00
|
|
|
def update(self):
|
2020-03-06 14:46:33 +01:00
|
|
|
data = {}
|
2020-05-03 11:15:52 +02:00
|
|
|
with open("/proc/meminfo", "r") as f:
|
2020-03-06 14:46:33 +01:00
|
|
|
for line in f:
|
2020-05-03 11:15:52 +02:00
|
|
|
tmp = re.split(r"[:\s]+", line)
|
2020-03-06 14:46:33 +01:00
|
|
|
value = int(tmp[1])
|
2020-05-03 11:15:52 +02:00
|
|
|
if tmp[2] == "kB":
|
|
|
|
value = value * 1024
|
|
|
|
if tmp[2] == "mB":
|
|
|
|
value = value * 1024 * 1024
|
|
|
|
if tmp[2] == "gB":
|
|
|
|
value = value * 1024 * 1024 * 1024
|
2020-03-06 14:46:33 +01:00
|
|
|
data[tmp[0]] = value
|
2020-05-03 11:15:52 +02:00
|
|
|
if "MemAvailable" in data:
|
|
|
|
used = data["MemTotal"] - data["MemAvailable"]
|
2020-03-06 14:46:33 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
used = (
|
|
|
|
data["MemTotal"]
|
|
|
|
- data["MemFree"]
|
|
|
|
- data["Buffers"]
|
|
|
|
- data["Cached"]
|
|
|
|
- data["Slab"]
|
|
|
|
)
|
2020-03-06 14:46:33 +01:00
|
|
|
self._mem = {
|
2020-05-03 11:15:52 +02:00
|
|
|
"total": util.format.byte(data["MemTotal"]),
|
|
|
|
"available": util.format.byte(data["MemAvailable"]),
|
|
|
|
"free": util.format.byte(data["MemFree"]),
|
|
|
|
"used": util.format.byte(used),
|
|
|
|
"percent": float(used) / float(data["MemTotal"]) * 100.0,
|
2020-03-06 14:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def state(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
if self._mem["percent"] > float(self.parameter("critical", 90)):
|
|
|
|
return "critical"
|
|
|
|
if self._mem["percent"] > float(self.parameter("warning", 80)):
|
|
|
|
return "warning"
|
2020-03-06 14:46:33 +01:00
|
|
|
return None
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-06 14:46:33 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|