mem.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from i3pystatus import IntervalModule
  2. import psutil
  3. from .core.util import round_dict
  4. class Mem(IntervalModule):
  5. """
  6. Shows memory load
  7. .. rubric:: Available formatters
  8. * {avail_mem}
  9. * {percent_used_mem}
  10. * {used_mem}
  11. * {total_mem}
  12. Requires psutil (from PyPI)
  13. """
  14. format = "{avail_mem} MiB"
  15. divisor = 1024 ** 2
  16. color = "#00FF00"
  17. warn_color = "#FFFF00"
  18. alert_color = "#FF0000"
  19. warn_percentage = 50
  20. alert_percentage = 80
  21. round_size = 1
  22. settings = (
  23. ("format", "format string used for output."),
  24. ("divisor",
  25. "divide all byte values by this value, default is 1024**2 (megabytes)"),
  26. ("warn_percentage", "minimal percentage for warn state"),
  27. ("alert_percentage", "minimal percentage for alert state"),
  28. ("color", "standard color"),
  29. ("warn_color",
  30. "defines the color used when warn percentage is exceeded"),
  31. ("alert_color",
  32. "defines the color used when alert percentage is exceeded"),
  33. ("round_size", "defines number of digits in round"),
  34. )
  35. def run(self):
  36. memory_usage = psutil.virtual_memory()
  37. if memory_usage.percent >= self.alert_percentage:
  38. color = self.alert_color
  39. elif memory_usage.percent >= self.warn_percentage:
  40. color = self.warn_color
  41. else:
  42. color = self.color
  43. cdict = {
  44. "used_mem": max(0, memory_usage.used) / self.divisor,
  45. "avail_mem": memory_usage.available / self.divisor,
  46. "total_mem": memory_usage.total / self.divisor,
  47. "percent_used_mem": memory_usage.percent,
  48. }
  49. round_dict(cdict, self.round_size)
  50. self.data = cdict
  51. self.output = {
  52. "full_text": self.format.format(**cdict),
  53. "color": color
  54. }