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
|
|
|
"""Monitor plugin.""" |
21
|
|
|
|
22
|
|
|
from glances.compat import iteritems |
23
|
|
|
from glances.amps_list import AmpsList as glancesAmpsList |
24
|
|
|
from glances.plugins.glances_plugin import GlancesPlugin |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class Plugin(GlancesPlugin): |
28
|
|
|
"""Glances AMPs plugin.""" |
29
|
|
|
|
30
|
|
|
def __init__(self, args=None, config=None): |
31
|
|
|
"""Init the plugin.""" |
32
|
|
|
super(Plugin, self).__init__(args=args, |
33
|
|
|
config=config, |
34
|
|
|
stats_init_value=[]) |
35
|
|
|
self.args = args |
36
|
|
|
self.config = config |
37
|
|
|
|
38
|
|
|
# We want to display the stat in the curse interface |
39
|
|
|
self.display_curse = True |
40
|
|
|
|
41
|
|
|
# Init the list of AMP (classe define in the glances/amps_list.py script) |
42
|
|
|
self.glances_amps = glancesAmpsList(self.args, self.config) |
43
|
|
|
|
44
|
|
|
@GlancesPlugin._check_decorator |
45
|
|
|
@GlancesPlugin._log_result_decorator |
46
|
|
|
def update(self): |
47
|
|
|
"""Update the AMP list.""" |
48
|
|
|
# Init new stats |
49
|
|
|
stats = self.get_init_value() |
50
|
|
|
|
51
|
|
|
if self.input_method == 'local': |
52
|
|
|
for k, v in iteritems(self.glances_amps.update()): |
53
|
|
|
stats.append({'key': k, |
54
|
|
|
'name': v.NAME, |
55
|
|
|
'result': v.result(), |
56
|
|
|
'refresh': v.refresh(), |
57
|
|
|
'timer': v.time_until_refresh(), |
58
|
|
|
'count': v.count(), |
59
|
|
|
'countmin': v.count_min(), |
60
|
|
|
'countmax': v.count_max(), |
61
|
|
|
'regex': v.regex() is not None}, |
62
|
|
|
) |
63
|
|
|
else: |
64
|
|
|
# Not available in SNMP mode |
65
|
|
|
pass |
66
|
|
|
|
67
|
|
|
# Update the stats |
68
|
|
|
self.stats = stats |
69
|
|
|
|
70
|
|
|
return self.stats |
71
|
|
|
|
72
|
|
|
def get_alert(self, nbprocess=0, countmin=None, countmax=None, header="", log=False): |
73
|
|
|
"""Return the alert status relative to the process number.""" |
74
|
|
|
if nbprocess is None: |
75
|
|
|
return 'OK' |
76
|
|
|
if countmin is None: |
77
|
|
|
countmin = nbprocess |
78
|
|
|
if countmax is None: |
79
|
|
|
countmax = nbprocess |
80
|
|
|
if nbprocess > 0: |
81
|
|
|
if int(countmin) <= int(nbprocess) <= int(countmax): |
82
|
|
|
return 'OK' |
83
|
|
|
else: |
84
|
|
|
return 'WARNING' |
85
|
|
|
else: |
86
|
|
|
if int(countmin) == 0: |
87
|
|
|
return 'OK' |
88
|
|
|
else: |
89
|
|
|
return 'CRITICAL' |
90
|
|
|
|
91
|
|
|
def msg_curse(self, args=None, max_width=None): |
92
|
|
|
"""Return the dict to display in the curse interface.""" |
93
|
|
|
# Init the return message |
94
|
|
|
# Only process if stats exist and display plugin enable... |
95
|
|
|
ret = [] |
96
|
|
|
|
97
|
|
|
if not self.stats or args.disable_process or self.is_disable(): |
98
|
|
|
return ret |
99
|
|
|
|
100
|
|
|
# Build the string message |
101
|
|
|
for m in self.stats: |
102
|
|
|
# Only display AMP if a result exist |
103
|
|
|
if m['result'] is None: |
104
|
|
|
continue |
105
|
|
|
# Display AMP |
106
|
|
|
first_column = '{}'.format(m['name']) |
107
|
|
|
first_column_style = self.get_alert(m['count'], m['countmin'], m['countmax']) |
108
|
|
|
second_column = '{}'.format(m['count'] if m['regex'] else '') |
109
|
|
|
for l in m['result'].split('\n'): |
110
|
|
|
# Display first column with the process name... |
111
|
|
|
msg = '{:<16} '.format(first_column) |
112
|
|
|
ret.append(self.curse_add_line(msg, first_column_style)) |
113
|
|
|
# ... and second column with the number of matching processes... |
114
|
|
|
msg = '{:<4} '.format(second_column) |
115
|
|
|
ret.append(self.curse_add_line(msg)) |
116
|
|
|
# ... only on the first line |
117
|
|
|
first_column = second_column = '' |
118
|
|
|
# Display AMP result in the third column |
119
|
|
|
ret.append(self.curse_add_line(l, splittable=True)) |
120
|
|
|
ret.append(self.curse_new_line()) |
121
|
|
|
|
122
|
|
|
# Delete the last empty line |
123
|
|
|
try: |
124
|
|
|
ret.pop() |
125
|
|
|
except IndexError: |
126
|
|
|
pass |
127
|
|
|
|
128
|
|
|
return ret |
129
|
|
|
|