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
|
|
|
"""Load plugin.""" |
21
|
|
|
|
22
|
|
|
import os |
23
|
|
|
|
24
|
|
|
from glances.compat import iteritems |
25
|
|
|
from glances.plugins.glances_core import Plugin as CorePlugin |
26
|
|
|
from glances.plugins.glances_plugin import GlancesPlugin |
27
|
|
|
|
28
|
|
|
# SNMP OID |
29
|
|
|
# 1 minute Load: .1.3.6.1.4.1.2021.10.1.3.1 |
30
|
|
|
# 5 minute Load: .1.3.6.1.4.1.2021.10.1.3.2 |
31
|
|
|
# 15 minute Load: .1.3.6.1.4.1.2021.10.1.3.3 |
32
|
|
|
snmp_oid = {'min1': '1.3.6.1.4.1.2021.10.1.3.1', |
33
|
|
|
'min5': '1.3.6.1.4.1.2021.10.1.3.2', |
34
|
|
|
'min15': '1.3.6.1.4.1.2021.10.1.3.3'} |
35
|
|
|
|
36
|
|
|
# Define the history items list |
37
|
|
|
# All items in this list will be historised if the --enable-history tag is set |
38
|
|
|
# 'color' define the graph color in #RGB format |
39
|
|
|
items_history_list = [{'name': 'min1', 'color': '#0000FF'}, |
40
|
|
|
{'name': 'min5', 'color': '#0000AA'}, |
41
|
|
|
{'name': 'min15', 'color': '#000044'}] |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class Plugin(GlancesPlugin): |
45
|
|
|
|
46
|
|
|
"""Glances load plugin. |
47
|
|
|
|
48
|
|
|
stats is a dict |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
def __init__(self, args=None): |
52
|
|
|
"""Init the plugin.""" |
53
|
|
|
super(Plugin, self).__init__(args=args, items_history_list=items_history_list) |
54
|
|
|
|
55
|
|
|
# We want to display the stat in the curse interface |
56
|
|
|
self.display_curse = True |
57
|
|
|
|
58
|
|
|
# Init stats |
59
|
|
|
self.reset() |
60
|
|
|
|
61
|
|
|
# Call CorePlugin in order to display the core number |
62
|
|
|
try: |
63
|
|
|
self.nb_log_core = CorePlugin(args=self.args).update()["log"] |
64
|
|
|
except Exception: |
65
|
|
|
self.nb_log_core = 0 |
66
|
|
|
|
67
|
|
|
def reset(self): |
68
|
|
|
"""Reset/init the stats.""" |
69
|
|
|
self.stats = {} |
70
|
|
|
|
71
|
|
|
@GlancesPlugin._log_result_decorator |
72
|
|
|
def update(self): |
73
|
|
|
"""Update load stats.""" |
74
|
|
|
# Reset stats |
75
|
|
|
self.reset() |
76
|
|
|
|
77
|
|
|
if self.input_method == 'local': |
78
|
|
|
# Update stats using the standard system lib |
79
|
|
|
|
80
|
|
|
# Get the load using the os standard lib |
81
|
|
|
try: |
82
|
|
|
load = os.getloadavg() |
83
|
|
|
except (OSError, AttributeError): |
84
|
|
|
self.stats = {} |
85
|
|
|
else: |
86
|
|
|
self.stats = {'min1': load[0], |
87
|
|
|
'min5': load[1], |
88
|
|
|
'min15': load[2], |
89
|
|
|
'cpucore': self.nb_log_core} |
90
|
|
|
elif self.input_method == 'snmp': |
91
|
|
|
# Update stats using SNMP |
92
|
|
|
self.stats = self.get_stats_snmp(snmp_oid=snmp_oid) |
93
|
|
|
|
94
|
|
|
if self.stats['min1'] == '': |
95
|
|
|
self.reset() |
96
|
|
|
return self.stats |
97
|
|
|
|
98
|
|
|
# Python 3 return a dict like: |
99
|
|
|
# {'min1': "b'0.08'", 'min5': "b'0.12'", 'min15': "b'0.15'"} |
100
|
|
|
for k, v in iteritems(self.stats): |
101
|
|
|
self.stats[k] = float(v) |
102
|
|
|
|
103
|
|
|
self.stats['cpucore'] = self.nb_log_core |
104
|
|
|
|
105
|
|
|
# Update the history list |
106
|
|
|
self.update_stats_history() |
107
|
|
|
|
108
|
|
|
# Update the view |
109
|
|
|
self.update_views() |
110
|
|
|
|
111
|
|
|
return self.stats |
112
|
|
|
|
113
|
|
|
def update_views(self): |
114
|
|
|
"""Update stats views.""" |
115
|
|
|
# Call the father's method |
116
|
|
|
super(Plugin, self).update_views() |
117
|
|
|
|
118
|
|
|
# Add specifics informations |
119
|
|
|
try: |
120
|
|
|
# Alert and log |
121
|
|
|
self.views['min15']['decoration'] = self.get_alert_log(self.stats['min15'], maximum=100 * self.stats['cpucore']) |
122
|
|
|
# Alert only |
123
|
|
|
self.views['min5']['decoration'] = self.get_alert(self.stats['min5'], maximum=100 * self.stats['cpucore']) |
124
|
|
|
except KeyError: |
125
|
|
|
# try/except mandatory for Windows compatibility (no load stats) |
126
|
|
|
pass |
127
|
|
|
|
128
|
|
|
def msg_curse(self, args=None): |
129
|
|
|
"""Return the dict to display in the curse interface.""" |
130
|
|
|
# Init the return message |
131
|
|
|
ret = [] |
132
|
|
|
|
133
|
|
|
# Only process if stats exist and plugin not disabled |
134
|
|
|
if not self.stats or args.disable_load: |
135
|
|
|
return ret |
136
|
|
|
|
137
|
|
|
# Build the string message |
138
|
|
|
# Header |
139
|
|
|
msg = '{0:8}'.format('LOAD') |
140
|
|
|
ret.append(self.curse_add_line(msg, "TITLE")) |
141
|
|
|
# Core number |
142
|
|
|
if 'cpucore' in self.stats and self.stats['cpucore'] > 0: |
143
|
|
|
msg = '{0}-core'.format(int(self.stats['cpucore'])) |
144
|
|
|
ret.append(self.curse_add_line(msg)) |
145
|
|
|
# New line |
146
|
|
|
ret.append(self.curse_new_line()) |
147
|
|
|
# 1min load |
148
|
|
|
msg = '{0:8}'.format('1 min:') |
149
|
|
|
ret.append(self.curse_add_line(msg)) |
150
|
|
|
msg = '{0:>6.2f}'.format(self.stats['min1']) |
151
|
|
|
ret.append(self.curse_add_line(msg)) |
152
|
|
|
# New line |
153
|
|
|
ret.append(self.curse_new_line()) |
154
|
|
|
# 5min load |
155
|
|
|
msg = '{0:8}'.format('5 min:') |
156
|
|
|
ret.append(self.curse_add_line(msg)) |
157
|
|
|
msg = '{0:>6.2f}'.format(self.stats['min5']) |
158
|
|
|
ret.append(self.curse_add_line( |
159
|
|
|
msg, self.get_views(key='min5', option='decoration'))) |
160
|
|
|
# New line |
161
|
|
|
ret.append(self.curse_new_line()) |
162
|
|
|
# 15min load |
163
|
|
|
msg = '{0:8}'.format('15 min:') |
164
|
|
|
ret.append(self.curse_add_line(msg)) |
165
|
|
|
msg = '{0:>6.2f}'.format(self.stats['min15']) |
166
|
|
|
ret.append(self.curse_add_line( |
167
|
|
|
msg, self.get_views(key='min15', option='decoration'))) |
168
|
|
|
|
169
|
|
|
return ret |
170
|
|
|
|