1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2015 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 u |
23
|
|
|
from glances.monitor_list import MonitorList as glancesMonitorList |
24
|
|
|
from glances.plugins.glances_plugin import GlancesPlugin |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class Plugin(GlancesPlugin): |
28
|
|
|
|
29
|
|
|
"""Glances monitor plugin.""" |
30
|
|
|
|
31
|
|
|
def __init__(self, args=None): |
32
|
|
|
"""Init the plugin.""" |
33
|
|
|
super(Plugin, self).__init__(args=args) |
34
|
|
|
|
35
|
|
|
# We want to display the stat in the curse interface |
36
|
|
|
self.display_curse = True |
37
|
|
|
|
38
|
|
|
# Init stats |
39
|
|
|
self.glances_monitors = None |
40
|
|
|
self.stats = [] |
41
|
|
|
|
42
|
|
|
def load_limits(self, config): |
43
|
|
|
"""Load the monitored list from the config file, if it exists.""" |
44
|
|
|
self.glances_monitors = glancesMonitorList(config) |
45
|
|
|
|
46
|
|
|
def update(self): |
47
|
|
|
"""Update the monitored list.""" |
48
|
|
|
if self.input_method == 'local': |
49
|
|
|
# Monitor list only available in a full Glances environment |
50
|
|
|
# Check if the glances_monitor instance is init |
51
|
|
|
if self.glances_monitors is None: |
52
|
|
|
return self.stats |
53
|
|
|
|
54
|
|
|
# Update the monitored list (result of command) |
55
|
|
|
self.glances_monitors.update() |
56
|
|
|
|
57
|
|
|
# Put it on the stats var |
58
|
|
|
self.stats = self.glances_monitors.get() |
59
|
|
|
else: |
60
|
|
|
pass |
61
|
|
|
|
62
|
|
|
return self.stats |
63
|
|
|
|
64
|
|
|
def get_alert(self, nbprocess=0, countmin=None, countmax=None, header="", log=False): |
65
|
|
|
"""Return the alert status relative to the process number.""" |
66
|
|
|
if nbprocess is None: |
67
|
|
|
return 'OK' |
68
|
|
|
if countmin is None: |
69
|
|
|
countmin = nbprocess |
70
|
|
|
if countmax is None: |
71
|
|
|
countmax = nbprocess |
72
|
|
|
if nbprocess > 0: |
73
|
|
|
if int(countmin) <= int(nbprocess) <= int(countmax): |
74
|
|
|
return 'OK' |
75
|
|
|
else: |
76
|
|
|
return 'WARNING' |
77
|
|
|
else: |
78
|
|
|
if int(countmin) == 0: |
79
|
|
|
return 'OK' |
80
|
|
|
else: |
81
|
|
|
return 'CRITICAL' |
82
|
|
|
|
83
|
|
|
def msg_curse(self, args=None): |
84
|
|
|
"""Return the dict to display in the curse interface.""" |
85
|
|
|
# Init the return message |
86
|
|
|
ret = [] |
87
|
|
|
|
88
|
|
|
# Only process if stats exist and display plugin enable... |
89
|
|
|
if not self.stats or args.disable_process: |
90
|
|
|
return ret |
91
|
|
|
|
92
|
|
|
# Build the string message |
93
|
|
|
for m in self.stats: |
94
|
|
|
msg = '{0:<16} '.format(m['description']) |
95
|
|
|
ret.append(self.curse_add_line( |
96
|
|
|
msg, self.get_alert(m['count'], m['countmin'], m['countmax']))) |
97
|
|
|
msg = '{0:<3} '.format(m['count'] if m['count'] > 1 else '') |
98
|
|
|
ret.append(self.curse_add_line(msg)) |
99
|
|
|
msg = '{0:13} '.format('RUNNING' if m['count'] >= 1 else 'NOT RUNNING') |
100
|
|
|
ret.append(self.curse_add_line(msg)) |
101
|
|
|
# Decode to UTF-8 (for Python 2) |
102
|
|
|
try: |
103
|
|
|
msg = u(m['result']) if m['count'] >= 1 else '' |
104
|
|
|
except UnicodeEncodeError: |
105
|
|
|
# Hack if return message contains non UTF-8 compliant char |
106
|
|
|
msg = u(m['default_result']) if m['count'] >= 1 else '' |
107
|
|
|
ret.append(self.curse_add_line(msg, optional=True, splittable=True)) |
108
|
|
|
ret.append(self.curse_new_line()) |
109
|
|
|
|
110
|
|
|
# Delete the last empty line |
111
|
|
|
try: |
112
|
|
|
ret.pop() |
113
|
|
|
except IndexError: |
114
|
|
|
pass |
115
|
|
|
|
116
|
|
|
return ret |
117
|
|
|
|