From 165049862b3afbe0b1f4ffc4423a6d1d25a53302 Mon Sep 17 00:00:00 2001 From: Arvedui Date: Wed, 22 Jan 2014 20:22:55 +0100 Subject: [PATCH] added 2 warning states, color support, support for user defined devisor, configurable rounding of byte values for mem module and fixed used memory calculation in mem module --- i3pystatus/mem.py | 48 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/i3pystatus/mem.py b/i3pystatus/mem.py index daf6c54..13340a0 100644 --- a/i3pystatus/mem.py +++ b/i3pystatus/mem.py @@ -1,8 +1,6 @@ from i3pystatus import IntervalModule from psutil import virtual_memory -MEGABYTE = 1024 * 1024 - class Mem(IntervalModule): """ @@ -18,19 +16,49 @@ class Mem(IntervalModule): Requires psutil (from PyPI) """ - format = "{avail_mem} MB" + format = "{avail_mem} GB" + divisor = 1024 ** 3 + color = "#00FF00" + warn_color = "#FFFF00" + alert_color = "#FF0000" + warn_percantage = 50 + alert_percentage = 80 + _round = 2 + settings = ( ("format", "format string used for output."), + ("divisor", + "divide all byte values by this value, default 1024**2(mebibytes"), + ("warn_percantage", "minimal percantage for warn state"), + ("alert_percentage", "minimal percantage for alert state"), + ("color", "standard color"), + ("warn_color", + "defines the color used wann warn percentage ist exceeded"), + ("alert_color", + "defines the color used when alert percentage is exceeded"), + ("round", "round byte values to given length behind dot") + ) def run(self): - vm = virtual_memory() - # This follows the same algorithm used for vm-percent - used = vm.used - vm.cached + memory_usage = virtual_memory() + used = memory_usage.used - memory_usage.cached - memory_usage.buffers + + if memory_usage.percent >= self.alert_percentage: + color = self.alert_color + + elif memory_usage.percent >= self.warn_percantage: + color = self.warn_color + + else: + color = self.color + self.output = { "full_text": self.format.format( - used_mem=int(round(used / MEGABYTE, 0)), - avail_mem=int(round(vm.available / MEGABYTE, 0)), - total_mem=int(round(vm.total / MEGABYTE, 0)), - percent_used_mem=vm.percent) + used_mem=round(used / self.divisor, self._round), + avail_mem=round(memory_usage.available / self.divisor, + self._round), + total_mem=round(memory_usage.total / self.divisor, self._round), + percent_used_mem=int(memory_usage.percent)), + "color":color }