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
|
|
|
"""CPU plugin.""" |
21
|
|
|
|
22
|
|
|
from glances.timer import getTimeSinceLastUpdate |
23
|
|
|
from glances.compat import iterkeys |
24
|
|
|
from glances.cpu_percent import cpu_percent |
25
|
|
|
from glances.globals import LINUX |
26
|
|
|
from glances.plugins.glances_core import Plugin as CorePlugin |
27
|
|
|
from glances.plugins.glances_plugin import GlancesPlugin |
28
|
|
|
|
29
|
|
|
import psutil |
30
|
|
|
|
31
|
|
|
# SNMP OID |
32
|
|
|
# percentage of user CPU time: .1.3.6.1.4.1.2021.11.9.0 |
33
|
|
|
# percentages of system CPU time: .1.3.6.1.4.1.2021.11.10.0 |
34
|
|
|
# percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0 |
35
|
|
|
snmp_oid = {'default': {'user': '1.3.6.1.4.1.2021.11.9.0', |
36
|
|
|
'system': '1.3.6.1.4.1.2021.11.10.0', |
37
|
|
|
'idle': '1.3.6.1.4.1.2021.11.11.0'}, |
38
|
|
|
'windows': {'percent': '1.3.6.1.2.1.25.3.3.1.2'}, |
39
|
|
|
'esxi': {'percent': '1.3.6.1.2.1.25.3.3.1.2'}, |
40
|
|
|
'netapp': {'system': '1.3.6.1.4.1.789.1.2.1.3.0', |
41
|
|
|
'idle': '1.3.6.1.4.1.789.1.2.1.5.0', |
42
|
|
|
'nb_log_core': '1.3.6.1.4.1.789.1.2.1.6.0'}} |
43
|
|
|
|
44
|
|
|
# Define the history items list |
45
|
|
|
# - 'name' define the stat identifier |
46
|
|
|
# - 'color' define the graph color in #RGB format |
47
|
|
|
# - 'y_unit' define the Y label |
48
|
|
|
# All items in this list will be historised if the --enable-history tag is set |
49
|
|
|
items_history_list = [{'name': 'user', |
50
|
|
|
'description': 'User CPU usage', |
51
|
|
|
'color': '#00FF00', |
52
|
|
|
'y_unit': '%'}, |
53
|
|
|
{'name': 'system', |
54
|
|
|
'description': 'System CPU usage', |
55
|
|
|
'color': '#FF0000', |
56
|
|
|
'y_unit': '%'}] |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
class Plugin(GlancesPlugin): |
60
|
|
|
|
61
|
|
|
"""Glances CPU plugin. |
62
|
|
|
|
63
|
|
|
'stats' is a dictionary that contains the system-wide CPU utilization as a |
64
|
|
|
percentage. |
65
|
|
|
""" |
66
|
|
|
|
67
|
|
|
def __init__(self, args=None): |
68
|
|
|
"""Init the CPU plugin.""" |
69
|
|
|
super(Plugin, self).__init__(args=args, items_history_list=items_history_list) |
70
|
|
|
|
71
|
|
|
# We want to display the stat in the curse interface |
72
|
|
|
self.display_curse = True |
73
|
|
|
|
74
|
|
|
# Init stats |
75
|
|
|
self.reset() |
76
|
|
|
|
77
|
|
|
# Call CorePlugin in order to display the core number |
78
|
|
|
try: |
79
|
|
|
self.nb_log_core = CorePlugin(args=self.args).update()["log"] |
80
|
|
|
except Exception: |
81
|
|
|
self.nb_log_core = 1 |
82
|
|
|
|
83
|
|
|
def reset(self): |
84
|
|
|
"""Reset/init the stats.""" |
85
|
|
|
self.stats = {} |
86
|
|
|
|
87
|
|
|
@GlancesPlugin._log_result_decorator |
88
|
|
|
def update(self): |
89
|
|
|
"""Update CPU stats using the input method.""" |
90
|
|
|
# Reset stats |
91
|
|
|
self.reset() |
92
|
|
|
|
93
|
|
|
# Grab stats into self.stats |
94
|
|
|
if self.input_method == 'local': |
95
|
|
|
self.update_local() |
96
|
|
|
elif self.input_method == 'snmp': |
97
|
|
|
self.update_snmp() |
98
|
|
|
|
99
|
|
|
# Update the history list |
100
|
|
|
self.update_stats_history() |
101
|
|
|
|
102
|
|
|
# Update the view |
103
|
|
|
self.update_views() |
104
|
|
|
|
105
|
|
|
return self.stats |
106
|
|
|
|
107
|
|
|
def update_local(self): |
108
|
|
|
"""Update CPU stats using PSUtil.""" |
109
|
|
|
# Grab CPU stats using psutil's cpu_percent and cpu_times_percent |
110
|
|
|
# Get all possible values for CPU stats: user, system, idle, |
111
|
|
|
# nice (UNIX), iowait (Linux), irq (Linux, FreeBSD), steal (Linux 2.6.11+) |
112
|
|
|
# The following stats are returned by the API but not displayed in the UI: |
113
|
|
|
# softirq (Linux), guest (Linux 2.6.24+), guest_nice (Linux 3.2.0+) |
114
|
|
|
self.stats['total'] = cpu_percent.get() |
115
|
|
|
cpu_times_percent = psutil.cpu_times_percent(interval=0.0) |
116
|
|
|
for stat in ['user', 'system', 'idle', 'nice', 'iowait', |
117
|
|
|
'irq', 'softirq', 'steal', 'guest', 'guest_nice']: |
118
|
|
|
if hasattr(cpu_times_percent, stat): |
119
|
|
|
self.stats[stat] = getattr(cpu_times_percent, stat) |
120
|
|
|
|
121
|
|
|
# Additionnal CPU stats (number of events / not as a %) |
122
|
|
|
# ctx_switches: number of context switches (voluntary + involuntary) per second |
123
|
|
|
# interrupts: number of interrupts per second |
124
|
|
|
# soft_interrupts: number of software interrupts per second. Always set to 0 on Windows and SunOS. |
125
|
|
|
# syscalls: number of system calls since boot. Always set to 0 on Linux. |
126
|
|
|
try: |
127
|
|
|
cpu_stats = psutil.cpu_stats() |
128
|
|
|
except AttributeError: |
129
|
|
|
# cpu_stats only available with PSUtil 4.1 or + |
130
|
|
|
pass |
131
|
|
|
else: |
132
|
|
|
# By storing time data we enable Rx/s and Tx/s calculations in the |
133
|
|
|
# XML/RPC API, which would otherwise be overly difficult work |
134
|
|
|
# for users of the API |
135
|
|
|
time_since_update = getTimeSinceLastUpdate('cpu') |
136
|
|
|
|
137
|
|
|
# Previous CPU stats are stored in the cpu_stats_old variable |
138
|
|
|
if not hasattr(self, 'cpu_stats_old'): |
139
|
|
|
# First call, we init the cpu_stats_old var |
140
|
|
|
self.cpu_stats_old = cpu_stats |
141
|
|
|
else: |
142
|
|
|
for stat in cpu_stats._fields: |
143
|
|
|
self.stats[stat] = getattr(cpu_stats, stat) - getattr(self.cpu_stats_old, stat) |
144
|
|
|
|
145
|
|
|
self.stats['time_since_update'] = time_since_update |
146
|
|
|
|
147
|
|
|
# Core number is needed to compute the CTX switch limit |
148
|
|
|
self.stats['cpucore'] = self.nb_log_core |
149
|
|
|
|
150
|
|
|
# Save stats to compute next step |
151
|
|
|
self.cpu_stats_old = cpu_stats |
152
|
|
|
|
153
|
|
|
def update_snmp(self): |
154
|
|
|
"""Update CPU stats using SNMP.""" |
155
|
|
|
# Update stats using SNMP |
156
|
|
|
if self.short_system_name in ('windows', 'esxi'): |
157
|
|
|
# Windows or VMWare ESXi |
158
|
|
|
# You can find the CPU utilization of windows system by querying the oid |
159
|
|
|
# Give also the number of core (number of element in the table) |
160
|
|
|
try: |
161
|
|
|
cpu_stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], |
162
|
|
|
bulk=True) |
163
|
|
|
except KeyError: |
164
|
|
|
self.reset() |
165
|
|
|
|
166
|
|
|
# Iter through CPU and compute the idle CPU stats |
167
|
|
|
self.stats['nb_log_core'] = 0 |
168
|
|
|
self.stats['idle'] = 0 |
169
|
|
|
for c in cpu_stats: |
170
|
|
|
if c.startswith('percent'): |
171
|
|
|
self.stats['idle'] += float(cpu_stats['percent.3']) |
172
|
|
|
self.stats['nb_log_core'] += 1 |
173
|
|
|
if self.stats['nb_log_core'] > 0: |
174
|
|
|
self.stats['idle'] = self.stats[ |
175
|
|
|
'idle'] / self.stats['nb_log_core'] |
176
|
|
|
self.stats['idle'] = 100 - self.stats['idle'] |
177
|
|
|
self.stats['total'] = 100 - self.stats['idle'] |
178
|
|
|
|
179
|
|
|
else: |
180
|
|
|
# Default behavor |
181
|
|
|
try: |
182
|
|
|
self.stats = self.get_stats_snmp( |
183
|
|
|
snmp_oid=snmp_oid[self.short_system_name]) |
184
|
|
|
except KeyError: |
185
|
|
|
self.stats = self.get_stats_snmp( |
186
|
|
|
snmp_oid=snmp_oid['default']) |
187
|
|
|
|
188
|
|
|
if self.stats['idle'] == '': |
189
|
|
|
self.reset() |
190
|
|
|
return self.stats |
191
|
|
|
|
192
|
|
|
# Convert SNMP stats to float |
193
|
|
View Code Duplication |
for key in iterkeys(self.stats): |
|
|
|
|
194
|
|
|
self.stats[key] = float(self.stats[key]) |
195
|
|
|
self.stats['total'] = 100 - self.stats['idle'] |
196
|
|
|
|
197
|
|
|
def update_views(self): |
198
|
|
|
"""Update stats views.""" |
199
|
|
|
# Call the father's method |
200
|
|
|
super(Plugin, self).update_views() |
201
|
|
|
|
202
|
|
|
# Add specifics informations |
203
|
|
|
# Alert and log |
204
|
|
|
for key in ['user', 'system', 'iowait']: |
205
|
|
|
if key in self.stats: |
206
|
|
|
self.views[key]['decoration'] = self.get_alert_log(self.stats[key], header=key) |
207
|
|
|
# Alert only |
208
|
|
|
for key in ['steal', 'total']: |
209
|
|
|
if key in self.stats: |
210
|
|
|
self.views[key]['decoration'] = self.get_alert(self.stats[key], header=key) |
211
|
|
|
# Alert only but depend on Core number |
212
|
|
|
for key in ['ctx_switches']: |
213
|
|
View Code Duplication |
if key in self.stats: |
|
|
|
|
214
|
|
|
self.views[key]['decoration'] = self.get_alert(self.stats[key], maximum=100 * self.stats['cpucore'], header=key) |
215
|
|
|
# Optional |
216
|
|
|
for key in ['nice', 'irq', 'iowait', 'steal', 'ctx_switches', 'interrupts', 'soft_interrupts', 'syscalls']: |
217
|
|
|
if key in self.stats: |
218
|
|
|
self.views[key]['optional'] = True |
219
|
|
|
|
220
|
|
|
def msg_curse(self, args=None): |
221
|
|
|
"""Return the list to display in the UI.""" |
222
|
|
|
# Init the return message |
223
|
|
|
ret = [] |
224
|
|
|
|
225
|
|
|
# Only process if stats exist and plugin not disable |
226
|
|
|
if not self.stats or args.disable_cpu: |
227
|
|
|
return ret |
228
|
|
|
|
229
|
|
|
# Build the string message |
230
|
|
|
# If user stat is not here, display only idle / total CPU usage (for |
231
|
|
|
# exemple on Windows OS) |
232
|
|
|
idle_tag = 'user' not in self.stats |
233
|
|
|
|
234
|
|
|
# Header |
235
|
|
|
msg = '{:8}'.format('CPU') |
236
|
|
|
ret.append(self.curse_add_line(msg, "TITLE")) |
237
|
|
|
# Total CPU usage |
238
|
|
|
msg = '{:>5}%'.format(self.stats['total']) |
239
|
|
|
if idle_tag: |
240
|
|
|
ret.append(self.curse_add_line( |
241
|
|
|
msg, self.get_views(key='total', option='decoration'))) |
242
|
|
|
else: |
243
|
|
|
ret.append(self.curse_add_line(msg)) |
244
|
|
|
# Nice CPU |
245
|
|
|
if 'nice' in self.stats: |
246
|
|
|
msg = ' {:8}'.format('nice:') |
247
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='nice', option='optional'))) |
248
|
|
|
msg = '{:>5}%'.format(self.stats['nice']) |
249
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='nice', option='optional'))) |
250
|
|
|
# ctx_switches |
251
|
|
View Code Duplication |
if 'ctx_switches' in self.stats: |
|
|
|
|
252
|
|
|
msg = ' {:8}'.format('ctx_sw:') |
253
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='ctx_switches', option='optional'))) |
254
|
|
|
msg = '{:>5}'.format(int(self.stats['ctx_switches'] // self.stats['time_since_update'])) |
255
|
|
|
ret.append(self.curse_add_line( |
256
|
|
|
msg, self.get_views(key='ctx_switches', option='decoration'), |
257
|
|
|
optional=self.get_views(key='ctx_switches', option='optional'))) |
258
|
|
|
|
259
|
|
|
# New line |
260
|
|
|
ret.append(self.curse_new_line()) |
261
|
|
|
# User CPU |
262
|
|
|
if 'user' in self.stats: |
263
|
|
|
msg = '{:8}'.format('user:') |
264
|
|
|
ret.append(self.curse_add_line(msg)) |
265
|
|
|
msg = '{:>5}%'.format(self.stats['user']) |
266
|
|
|
ret.append(self.curse_add_line( |
267
|
|
|
msg, self.get_views(key='user', option='decoration'))) |
268
|
|
|
elif 'idle' in self.stats: |
269
|
|
|
msg = '{:8}'.format('idle:') |
270
|
|
|
ret.append(self.curse_add_line(msg)) |
271
|
|
|
msg = '{:>5}%'.format(self.stats['idle']) |
272
|
|
|
ret.append(self.curse_add_line(msg)) |
273
|
|
|
# IRQ CPU |
274
|
|
|
if 'irq' in self.stats: |
275
|
|
|
msg = ' {:8}'.format('irq:') |
276
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='irq', option='optional'))) |
277
|
|
|
msg = '{:>5}%'.format(self.stats['irq']) |
278
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='irq', option='optional'))) |
279
|
|
|
# interrupts |
280
|
|
View Code Duplication |
if 'interrupts' in self.stats: |
|
|
|
|
281
|
|
|
msg = ' {:8}'.format('inter:') |
282
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='interrupts', option='optional'))) |
283
|
|
|
msg = '{:>5}'.format(int(self.stats['interrupts'] // self.stats['time_since_update'])) |
284
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='interrupts', option='optional'))) |
285
|
|
|
|
286
|
|
|
# New line |
287
|
|
|
ret.append(self.curse_new_line()) |
288
|
|
|
# System CPU |
289
|
|
|
if 'system' in self.stats and not idle_tag: |
290
|
|
|
msg = '{:8}'.format('system:') |
291
|
|
|
ret.append(self.curse_add_line(msg)) |
292
|
|
|
msg = '{:>5}%'.format(self.stats['system']) |
293
|
|
|
ret.append(self.curse_add_line( |
294
|
|
|
msg, self.get_views(key='system', option='decoration'))) |
295
|
|
|
else: |
296
|
|
|
msg = '{:8}'.format('core:') |
297
|
|
|
ret.append(self.curse_add_line(msg)) |
298
|
|
|
msg = '{:>6}'.format(self.stats['nb_log_core']) |
299
|
|
|
ret.append(self.curse_add_line(msg)) |
300
|
|
|
# IOWait CPU |
301
|
|
View Code Duplication |
if 'iowait' in self.stats: |
|
|
|
|
302
|
|
|
msg = ' {:8}'.format('iowait:') |
303
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='iowait', option='optional'))) |
304
|
|
|
msg = '{:>5}%'.format(self.stats['iowait']) |
305
|
|
|
ret.append(self.curse_add_line( |
306
|
|
|
msg, self.get_views(key='iowait', option='decoration'), |
307
|
|
|
optional=self.get_views(key='iowait', option='optional'))) |
308
|
|
|
# soft_interrupts |
309
|
|
View Code Duplication |
if 'soft_interrupts' in self.stats: |
|
|
|
|
310
|
|
|
msg = ' {:8}'.format('sw_int:') |
311
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='soft_interrupts', option='optional'))) |
312
|
|
|
msg = '{:>5}'.format(int(self.stats['soft_interrupts'] // self.stats['time_since_update'])) |
313
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='soft_interrupts', option='optional'))) |
314
|
|
|
|
315
|
|
|
# New line |
316
|
|
|
ret.append(self.curse_new_line()) |
317
|
|
|
# Idle CPU |
318
|
|
|
if 'idle' in self.stats and not idle_tag: |
319
|
|
|
msg = '{:8}'.format('idle:') |
320
|
|
|
ret.append(self.curse_add_line(msg)) |
321
|
|
|
msg = '{:>5}%'.format(self.stats['idle']) |
322
|
|
|
ret.append(self.curse_add_line(msg)) |
323
|
|
|
# Steal CPU usage |
324
|
|
View Code Duplication |
if 'steal' in self.stats: |
|
|
|
|
325
|
|
|
msg = ' {:8}'.format('steal:') |
326
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='steal', option='optional'))) |
327
|
|
|
msg = '{:>5}%'.format(self.stats['steal']) |
328
|
|
|
ret.append(self.curse_add_line( |
329
|
|
|
msg, self.get_views(key='steal', option='decoration'), |
330
|
|
|
optional=self.get_views(key='steal', option='optional'))) |
331
|
|
|
# syscalls |
332
|
|
|
# syscalls: number of system calls since boot. Always set to 0 on Linux. (do not display) |
333
|
|
View Code Duplication |
if 'syscalls' in self.stats and not LINUX: |
|
|
|
|
334
|
|
|
msg = ' {:8}'.format('syscal:') |
335
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='syscalls', option='optional'))) |
336
|
|
|
msg = '{:>5}'.format(int(self.stats['syscalls'] // self.stats['time_since_update'])) |
337
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='syscalls', option='optional'))) |
338
|
|
|
|
339
|
|
|
# Return the message with decoration |
340
|
|
|
return ret |
341
|
|
|
|