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

This commit is contained in:
Arvedui 2014-01-22 20:22:55 +01:00
parent 5fc8ed3f8e
commit 165049862b

View File

@ -1,8 +1,6 @@
from i3pystatus import IntervalModule from i3pystatus import IntervalModule
from psutil import virtual_memory from psutil import virtual_memory
MEGABYTE = 1024 * 1024
class Mem(IntervalModule): class Mem(IntervalModule):
""" """
@ -18,19 +16,49 @@ class Mem(IntervalModule):
Requires psutil (from PyPI) 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 = ( settings = (
("format", "format string used for output."), ("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): def run(self):
vm = virtual_memory() memory_usage = virtual_memory()
# This follows the same algorithm used for vm-percent used = memory_usage.used - memory_usage.cached - memory_usage.buffers
used = vm.used - vm.cached
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 = { self.output = {
"full_text": self.format.format( "full_text": self.format.format(
used_mem=int(round(used / MEGABYTE, 0)), used_mem=round(used / self.divisor, self._round),
avail_mem=int(round(vm.available / MEGABYTE, 0)), avail_mem=round(memory_usage.available / self.divisor,
total_mem=int(round(vm.total / MEGABYTE, 0)), self._round),
percent_used_mem=vm.percent) total_mem=round(memory_usage.total / self.divisor, self._round),
percent_used_mem=int(memory_usage.percent)),
"color":color
} }