From 36333c275f4d3a66c8f14383c3ada5a42a197bea Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Mon, 31 Oct 2016 12:11:39 +0100 Subject: [PATCH] [modules] Add module for displaying RAM usage Shows free RAM, total RAM, free RAM percentage --- bumblebee/modules/memory.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bumblebee/modules/memory.py diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py new file mode 100644 index 0000000..350effd --- /dev/null +++ b/bumblebee/modules/memory.py @@ -0,0 +1,30 @@ +import bumblebee.module +import psutil + +def fmt(num, suffix='B'): + for unit in [ "", "Ki", "Mi", "Gi" ]: + if num < 1024.0: + return "{:.2f}{}{}".format(num, unit, suffix) + num /= 1024.0 + return "{:05.2f%}{}{}".format(num, "Gi", suffix) + +class Module(bumblebee.module.Module): + def __init__(self, args): + super(Module, self).__init__(args) + self._mem = psutil.virtual_memory() + + def data(self): + self._mem = psutil.virtual_memory() + + free = self._mem.available + total = self._mem.total + + return "{}/{} ({:05.02f}%)".format(fmt(self._mem.available), fmt(self._mem.total), 100.0 - self._mem.percent) + + def warning(self): + return self._mem.percent < 20 + + def critical(self): + return self._mem.percent < 10 + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4