You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.2 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Glances.
  4. #
  5. # Copyright (C) 2018 Nicolargo <nicolas@nicolargo.com>
  6. #
  7. # Glances is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Glances is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Thresholds classes: OK, CAREFUL, WARNING, CRITICAL
  21. """
  22. import sys
  23. from functools import total_ordering
  24. class GlancesThresholds(object):
  25. """Class to manage thresholds dict for all Glances plugins:
  26. key: Glances stats (example: cpu_user)
  27. value: Threasold* instance
  28. """
  29. threshold_list = ['OK', 'CAREFUL', 'WARNING', 'CRITICAL']
  30. def __init__(self):
  31. self.current_module = sys.modules[__name__]
  32. self._thresholds = {}
  33. def get(self, stat_name=None):
  34. """Return the threshold dict.
  35. If stat_name is None, return the threshold for all plugins (dict of Threshold*)
  36. Else return the Threshold* instance for the given plugin
  37. """
  38. if stat_name is None:
  39. return self._thresholds
  40. if stat_name in self._thresholds:
  41. return self._thresholds[stat_name]
  42. else:
  43. return {}
  44. def add(self, stat_name, threshold_description):
  45. """Add a new threshold to the dict (key = stat_name)"""
  46. if threshold_description not in self.threshold_list:
  47. return False
  48. else:
  49. self._thresholds[stat_name] = getattr(self.current_module,
  50. 'GlancesThreshold' + threshold_description.capitalize())()
  51. return True
  52. # Global variable uses to share thresholds between Glances componants
  53. glances_thresholds = GlancesThresholds()
  54. @total_ordering
  55. class _GlancesThreshold(object):
  56. """Father class for all other Thresholds"""
  57. def description(self):
  58. return self._threshold['description']
  59. def value(self):
  60. return self._threshold['value']
  61. def __repr__(self):
  62. return str(self._threshold)
  63. def __str__(self):
  64. return self.description()
  65. def __lt__(self, other):
  66. return self.value() < other.value()
  67. def __eq__(self, other):
  68. return self.value() == other.value()
  69. class GlancesThresholdOk(_GlancesThreshold):
  70. """Ok Threshold class"""
  71. _threshold = {'description': 'OK',
  72. 'value': 0}
  73. class GlancesThresholdCareful(_GlancesThreshold):
  74. """Careful Threshold class"""
  75. _threshold = {'description': 'CAREFUL',
  76. 'value': 1}
  77. class GlancesThresholdWarning(_GlancesThreshold):
  78. """Warning Threshold class"""
  79. _threshold = {'description': 'WARNING',
  80. 'value': 2}
  81. class GlancesThresholdCritical(_GlancesThreshold):
  82. """Warning Threshold class"""
  83. _threshold = {'description': 'CRITICAL',
  84. 'value': 3}