|
1
|
|
|
# |
|
2
|
|
|
# This file is part of Glances. |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
|
5
|
|
|
# |
|
6
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
|
7
|
|
|
# |
|
8
|
|
|
|
|
9
|
|
|
"""Load plugin.""" |
|
10
|
|
|
|
|
11
|
|
|
import os |
|
12
|
|
|
|
|
13
|
|
|
import psutil |
|
14
|
|
|
|
|
15
|
|
|
from glances.logger import logger |
|
16
|
|
|
from glances.plugins.core import CorePlugin |
|
17
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel |
|
18
|
|
|
|
|
19
|
|
|
# Fields description |
|
20
|
|
|
fields_description = { |
|
21
|
|
|
'min1': { |
|
22
|
|
|
'description': 'Average sum of the number of processes \ |
|
23
|
|
|
waiting in the run-queue plus the number currently executing \ |
|
24
|
|
|
over 1 minute.', |
|
25
|
|
|
'unit': 'float', |
|
26
|
|
|
}, |
|
27
|
|
|
'min5': { |
|
28
|
|
|
'description': 'Average sum of the number of processes \ |
|
29
|
|
|
waiting in the run-queue plus the number currently executing \ |
|
30
|
|
|
over 5 minutes.', |
|
31
|
|
|
'unit': 'float', |
|
32
|
|
|
}, |
|
33
|
|
|
'min15': { |
|
34
|
|
|
'description': 'Average sum of the number of processes \ |
|
35
|
|
|
waiting in the run-queue plus the number currently executing \ |
|
36
|
|
|
over 15 minutes.', |
|
37
|
|
|
'unit': 'float', |
|
38
|
|
|
}, |
|
39
|
|
|
'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'}, |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
# SNMP OID |
|
43
|
|
|
# 1 minute Load: .1.3.6.1.4.1.2021.10.1.3.1 |
|
44
|
|
|
# 5 minute Load: .1.3.6.1.4.1.2021.10.1.3.2 |
|
45
|
|
|
# 15 minute Load: .1.3.6.1.4.1.2021.10.1.3.3 |
|
46
|
|
|
snmp_oid = { |
|
47
|
|
|
'min1': '1.3.6.1.4.1.2021.10.1.3.1', |
|
48
|
|
|
'min5': '1.3.6.1.4.1.2021.10.1.3.2', |
|
49
|
|
|
'min15': '1.3.6.1.4.1.2021.10.1.3.3', |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
# Define the history items list |
|
53
|
|
|
# All items in this list will be historised if the --enable-history tag is set |
|
54
|
|
|
items_history_list = [ |
|
55
|
|
|
{'name': 'min1', 'description': '1 minute load'}, |
|
56
|
|
|
{'name': 'min5', 'description': '5 minutes load'}, |
|
57
|
|
|
{'name': 'min15', 'description': '15 minutes load'}, |
|
58
|
|
|
] |
|
59
|
|
|
|
|
60
|
|
|
# Get the number of logical CPU core only once |
|
61
|
|
|
# the variable is also shared with the QuickLook plugin |
|
62
|
|
|
nb_log_core = 1 |
|
63
|
|
|
nb_phys_core = 1 |
|
64
|
|
|
try: |
|
65
|
|
|
core = CorePlugin().update() |
|
66
|
|
|
except Exception as e: |
|
67
|
|
|
logger.warning(f'Error: Can not retrieve the CPU core number (set it to 1) ({e})') |
|
68
|
|
|
else: |
|
69
|
|
|
if 'log' in core: |
|
70
|
|
|
nb_log_core = core['log'] |
|
71
|
|
|
if 'phys' in core: |
|
72
|
|
|
nb_phys_core = core['phys'] |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
class LoadPlugin(GlancesPluginModel): |
|
76
|
|
|
"""Glances load plugin. |
|
77
|
|
|
|
|
78
|
|
|
stats is a dict |
|
79
|
|
|
""" |
|
80
|
|
|
|
|
81
|
|
|
def __init__(self, args=None, config=None): |
|
82
|
|
|
"""Init the plugin.""" |
|
83
|
|
|
super().__init__( |
|
84
|
|
|
args=args, config=config, items_history_list=items_history_list, fields_description=fields_description |
|
85
|
|
|
) |
|
86
|
|
|
|
|
87
|
|
|
# We want to display the stat in the curse interface |
|
88
|
|
|
self.display_curse = True |
|
89
|
|
|
|
|
90
|
|
|
@GlancesPluginModel._check_decorator |
|
91
|
|
|
@GlancesPluginModel._log_result_decorator |
|
92
|
|
|
def update(self): |
|
93
|
|
|
"""Update load stats.""" |
|
94
|
|
|
# Init new stats |
|
95
|
|
|
stats = self.get_init_value() |
|
96
|
|
|
|
|
97
|
|
|
if self.input_method == 'local': |
|
98
|
|
|
# Update stats using the standard system lib |
|
99
|
|
|
|
|
100
|
|
|
# Get the load using the os standard lib |
|
101
|
|
|
load = load_average() |
|
102
|
|
|
if load is None: |
|
103
|
|
|
stats = self.get_init_value() |
|
104
|
|
|
else: |
|
105
|
|
|
stats = {'min1': load[0], 'min5': load[1], 'min15': load[2], 'cpucore': log_core()} |
|
106
|
|
|
|
|
107
|
|
|
elif self.input_method == 'snmp': |
|
108
|
|
|
# Update stats using SNMP |
|
109
|
|
|
stats = self.get_stats_snmp(snmp_oid=snmp_oid) |
|
110
|
|
|
|
|
111
|
|
|
if stats['min1'] == '': |
|
112
|
|
|
return self.get_init_value() |
|
113
|
|
|
|
|
114
|
|
|
# Python 3 return a dict like: |
|
115
|
|
|
# {'min1': "b'0.08'", 'min5': "b'0.12'", 'min15': "b'0.15'"} |
|
116
|
|
|
for k, v in stats.items(): |
|
117
|
|
|
stats[k] = float(v) |
|
118
|
|
|
|
|
119
|
|
|
stats['cpucore'] = log_core() |
|
120
|
|
|
|
|
121
|
|
|
# Update the stats |
|
122
|
|
|
self.stats = stats |
|
123
|
|
|
|
|
124
|
|
|
return self.stats |
|
125
|
|
|
|
|
126
|
|
|
def update_views(self): |
|
127
|
|
|
"""Update stats views.""" |
|
128
|
|
|
# Call the father's method |
|
129
|
|
|
super().update_views() |
|
130
|
|
|
|
|
131
|
|
|
# Add specifics information |
|
132
|
|
|
try: |
|
133
|
|
|
# Alert and log |
|
134
|
|
|
self.views['min15']['decoration'] = self.get_alert_log( |
|
135
|
|
|
self.stats['min15'], maximum=100 * self.stats['cpucore'] |
|
136
|
|
|
) |
|
137
|
|
|
# Alert only |
|
138
|
|
|
self.views['min5']['decoration'] = self.get_alert(self.stats['min5'], maximum=100 * self.stats['cpucore']) |
|
139
|
|
|
except KeyError: |
|
140
|
|
|
# try/except mandatory for Windows compatibility (no load stats) |
|
141
|
|
|
pass |
|
142
|
|
|
|
|
143
|
|
|
def msg_curse(self, args=None, max_width=None): |
|
144
|
|
|
"""Return the dict to display in the curse interface.""" |
|
145
|
|
|
# Init the return message |
|
146
|
|
|
ret = [] |
|
147
|
|
|
|
|
148
|
|
|
# Only process if stats exist, not empty (issue #871) and plugin not disabled |
|
149
|
|
|
if not self.stats or (self.stats == {}) or self.is_disabled(): |
|
150
|
|
|
return ret |
|
151
|
|
|
|
|
152
|
|
|
# Build the string message |
|
153
|
|
|
# Header |
|
154
|
|
|
msg = '{:4}'.format('LOAD') |
|
155
|
|
|
ret.append(self.curse_add_line(msg, "TITLE")) |
|
156
|
|
|
msg = ' {:1}'.format(self.trend_msg(self.get_trend('min1'))) |
|
157
|
|
|
ret.append(self.curse_add_line(msg)) |
|
158
|
|
|
# Core number |
|
159
|
|
|
if 'cpucore' in self.stats and self.stats['cpucore'] > 0: |
|
160
|
|
|
msg = '{:3}core'.format(int(self.stats['cpucore'])) |
|
161
|
|
|
ret.append(self.curse_add_line(msg)) |
|
162
|
|
|
# Loop over 1min, 5min and 15min load |
|
163
|
|
|
for load_time in ['1', '5', '15']: |
|
164
|
|
|
ret.append(self.curse_new_line()) |
|
165
|
|
|
msg = '{:7}'.format(f'{load_time} min') |
|
166
|
|
|
ret.append(self.curse_add_line(msg)) |
|
167
|
|
|
if args.disable_irix and log_core() != 0: |
|
168
|
|
|
# Enable Irix mode for load (see issue #1554) |
|
169
|
|
|
load_stat = self.stats[f'min{load_time}'] / log_core() * 100 |
|
170
|
|
|
msg = f'{load_stat:>5.1f}%' |
|
171
|
|
|
else: |
|
172
|
|
|
# Default mode for load |
|
173
|
|
|
load_stat = self.stats[f'min{load_time}'] |
|
174
|
|
|
msg = f'{load_stat:>6.2f}' |
|
175
|
|
|
ret.append(self.curse_add_line(msg, self.get_views(key=f'min{load_time}', option='decoration'))) |
|
176
|
|
|
|
|
177
|
|
|
return ret |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
def log_core(): |
|
181
|
|
|
"""Get the number of logical CPU core.""" |
|
182
|
|
|
return nb_log_core |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
def phys_core(): |
|
186
|
|
|
"""Get the number of physical CPU core.""" |
|
187
|
|
|
return nb_phys_core |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
def load_average(percent: bool = False): |
|
191
|
|
|
"""Get load average. On both Linux and Windows thanks to PsUtil |
|
192
|
|
|
|
|
193
|
|
|
if percent is True, return the load average in percent |
|
194
|
|
|
Ex: if you only have one CPU core and the load average is 1.0, then return 100%""" |
|
195
|
|
|
ret = None |
|
196
|
|
|
try: |
|
197
|
|
|
ret = psutil.getloadavg() |
|
198
|
|
|
except (AttributeError, OSError): |
|
199
|
|
|
try: |
|
200
|
|
|
ret = os.getloadavg() |
|
201
|
|
|
except (AttributeError, OSError): |
|
202
|
|
|
pass |
|
203
|
|
|
|
|
204
|
|
|
if ret and percent: |
|
205
|
|
|
return tuple([round(i / log_core() * 100, 1) for i in ret]) |
|
206
|
|
|
return ret |
|
207
|
|
|
|