summaryrefslogtreecommitdiffstats
path: root/glances/thresholds.py
blob: 782a0d13c2fa2ab7952c0642e096d4e562d6a15a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#
# This file is part of Glances.
#
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
#
# SPDX-License-Identifier: LGPL-3.0-only
#

"""
Thresholds classes: OK, CAREFUL, WARNING, CRITICAL
"""

import sys
from functools import total_ordering


class GlancesThresholds:
    """Class to manage thresholds dict for all Glances plugins:

    key: Glances stats (example: cpu_user)
    value: Threshold instance
    """

    threshold_list = ['OK', 'CAREFUL', 'WARNING', 'CRITICAL']

    def __init__(self):
        self.current_module = sys.modules[__name__]
        self._thresholds = {}

    def get(self, stat_name=None):
        """Return the threshold dict.
        If stat_name is None, return the threshold for all plugins (dict of Threshold*)
        Else return the Threshold* instance for the given plugin
        """
        if stat_name is None:
            return self._thresholds

        if stat_name in self._thresholds:
            return self._thresholds[stat_name]
        return {}

    def add(self, stat_name, threshold_description):
        """Add a new threshold to the dict (key = stat_name)"""
        if threshold_description not in self.threshold_list:
            return False

        self._thresholds[stat_name] = getattr(
            self.current_module, 'GlancesThreshold' + threshold_description.capitalize()
        )()
        return True


# Global variable uses to share thresholds between Glances components
glances_thresholds = GlancesThresholds()


@total_ordering
class _GlancesThreshold:
    """Father class for all other Thresholds"""

    def description(self):
        return self._threshold['description']

    def value(self):
        return self._threshold['value']

    def __repr__(self):
        return str(self._threshold)

    def __str__(self):
        return self.description()

    def __lt__(self, other):
        return self.value() < other.value()

    def __eq__(self, other):
        return self.value() == other.value()


class GlancesThresholdOk(_GlancesThreshold):
    """Ok Threshold class"""

    _threshold = {'description': 'OK', 'value': 0}


class GlancesThresholdCareful(_GlancesThreshold):
    """Careful Threshold class"""

    _threshold = {'description': 'CAREFUL', 'value': 1}


class GlancesThresholdWarning(_GlancesThreshold):
    """Warning Threshold class"""

    _threshold = {'description': 'WARNING', 'value': 2}


class GlancesThresholdCritical(_GlancesThreshold):
    """Warning Threshold class"""

    _threshold = {'description': 'CRITICAL', 'value': 3}