1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # Copyright (C) 2019 Nicolargo <[email protected]> |
||
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 | """ |
||
21 | Thresholds classes: OK, CAREFUL, WARNING, CRITICAL |
||
22 | """ |
||
23 | |||
24 | import sys |
||
25 | from functools import total_ordering |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
26 | |||
27 | |||
28 | class GlancesThresholds(object): |
||
29 | """Class to manage thresholds dict for all Glances plugins: |
||
30 | key: Glances stats (example: cpu_user) |
||
31 | value: Threasold* instance |
||
32 | """ |
||
33 | |||
34 | threshold_list = ['OK', 'CAREFUL', 'WARNING', 'CRITICAL'] |
||
35 | |||
36 | def __init__(self): |
||
37 | self.current_module = sys.modules[__name__] |
||
38 | self._thresholds = {} |
||
39 | |||
40 | def get(self, stat_name=None): |
||
41 | """Return the threshold dict. |
||
42 | If stat_name is None, return the threshold for all plugins (dict of Threshold*) |
||
43 | Else return the Threshold* instance for the given plugin |
||
44 | """ |
||
45 | if stat_name is None: |
||
46 | return self._thresholds |
||
47 | |||
48 | if stat_name in self._thresholds: |
||
49 | return self._thresholds[stat_name] |
||
50 | else: |
||
51 | return {} |
||
52 | |||
53 | def add(self, stat_name, threshold_description): |
||
54 | """Add a new threshold to the dict (key = stat_name)""" |
||
55 | if threshold_description not in self.threshold_list: |
||
56 | return False |
||
57 | else: |
||
58 | self._thresholds[stat_name] = getattr(self.current_module, |
||
59 | 'GlancesThreshold' + threshold_description.capitalize())() |
||
60 | return True |
||
61 | |||
62 | |||
63 | # Global variable uses to share thresholds between Glances componants |
||
64 | glances_thresholds = GlancesThresholds() |
||
65 | |||
66 | |||
67 | @total_ordering |
||
0 ignored issues
–
show
|
|||
68 | class _GlancesThreshold(object): |
||
69 | |||
70 | """Father class for all other Thresholds""" |
||
71 | |||
72 | def description(self): |
||
73 | return self._threshold['description'] |
||
74 | |||
75 | def value(self): |
||
76 | return self._threshold['value'] |
||
77 | |||
78 | def __repr__(self): |
||
79 | return str(self._threshold) |
||
80 | |||
81 | def __str__(self): |
||
82 | return self.description() |
||
83 | |||
84 | def __lt__(self, other): |
||
85 | return self.value() < other.value() |
||
86 | |||
87 | def __eq__(self, other): |
||
88 | return self.value() == other.value() |
||
89 | |||
90 | |||
91 | class GlancesThresholdOk(_GlancesThreshold): |
||
92 | |||
93 | """Ok Threshold class""" |
||
94 | |||
95 | _threshold = {'description': 'OK', |
||
96 | 'value': 0} |
||
97 | |||
98 | |||
99 | class GlancesThresholdCareful(_GlancesThreshold): |
||
100 | |||
101 | """Careful Threshold class""" |
||
102 | |||
103 | _threshold = {'description': 'CAREFUL', |
||
104 | 'value': 1} |
||
105 | |||
106 | |||
107 | class GlancesThresholdWarning(_GlancesThreshold): |
||
108 | |||
109 | """Warning Threshold class""" |
||
110 | |||
111 | _threshold = {'description': 'WARNING', |
||
112 | 'value': 2} |
||
113 | |||
114 | |||
115 | class GlancesThresholdCritical(_GlancesThreshold): |
||
116 | |||
117 | """Warning Threshold class""" |
||
118 | |||
119 | _threshold = {'description': 'CRITICAL', |
||
120 | 'value': 3} |
||
121 |