|
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
|
|
|
"""Disk I/O plugin.""" |
|
21
|
|
|
from __future__ import unicode_literals |
|
22
|
|
|
|
|
23
|
|
|
from glances.compat import nativestr, n |
|
24
|
|
|
from glances.timer import getTimeSinceLastUpdate |
|
25
|
|
|
from glances.plugins.glances_plugin import GlancesPlugin |
|
26
|
|
|
from glances.logger import logger |
|
27
|
|
|
|
|
28
|
|
|
import psutil |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
# Define the history items list |
|
32
|
|
|
items_history_list = [{'name': 'read_bytes', |
|
33
|
|
|
'description': 'Bytes read per second', |
|
34
|
|
|
'y_unit': 'B/s'}, |
|
35
|
|
|
{'name': 'write_bytes', |
|
36
|
|
|
'description': 'Bytes write per second', |
|
37
|
|
|
'y_unit': 'B/s'}] |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class Plugin(GlancesPlugin): |
|
41
|
|
|
"""Glances disks I/O plugin. |
|
42
|
|
|
|
|
43
|
|
|
stats is a list |
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
|
View Code Duplication |
def __init__(self, args=None, config=None): |
|
|
|
|
|
|
47
|
|
|
"""Init the plugin.""" |
|
48
|
|
|
super(Plugin, self).__init__(args=args, |
|
49
|
|
|
config=config, |
|
50
|
|
|
items_history_list=items_history_list, |
|
51
|
|
|
stats_init_value=[]) |
|
52
|
|
|
|
|
53
|
|
|
# We want to display the stat in the curse interface |
|
54
|
|
|
self.display_curse = True |
|
55
|
|
|
|
|
56
|
|
|
# Hide stats if it has never been != 0 |
|
57
|
|
|
if config is not None: |
|
58
|
|
|
self.hide_zero = config.get_bool_value( |
|
59
|
|
|
self.plugin_name, 'hide_zero', default=False) |
|
60
|
|
|
else: |
|
61
|
|
|
self.hide_zero = False |
|
62
|
|
|
self.hide_zero_fields = ['read_bytes', 'write_bytes'] |
|
63
|
|
|
|
|
64
|
|
|
# Force a first update because we need two update to have the first stat |
|
65
|
|
|
self.update() |
|
66
|
|
|
self.refresh_timer.set(0) |
|
67
|
|
|
|
|
68
|
|
|
def get_key(self): |
|
69
|
|
|
"""Return the key of the list.""" |
|
70
|
|
|
return 'disk_name' |
|
71
|
|
|
|
|
72
|
|
|
@GlancesPlugin._check_decorator |
|
73
|
|
|
@GlancesPlugin._log_result_decorator |
|
74
|
|
|
def update(self): |
|
75
|
|
|
"""Update disk I/O stats using the input method.""" |
|
76
|
|
|
# Init new stats |
|
77
|
|
|
stats = self.get_init_value() |
|
78
|
|
|
|
|
79
|
|
|
if self.input_method == 'local': |
|
80
|
|
|
# Update stats using the standard system lib |
|
81
|
|
|
# Grab the stat using the psutil disk_io_counters method |
|
82
|
|
|
# read_count: number of reads |
|
83
|
|
|
# write_count: number of writes |
|
84
|
|
|
# read_bytes: number of bytes read |
|
85
|
|
|
# write_bytes: number of bytes written |
|
86
|
|
|
# read_time: time spent reading from disk (in milliseconds) |
|
87
|
|
|
# write_time: time spent writing to disk (in milliseconds) |
|
88
|
|
|
try: |
|
89
|
|
|
diskio = psutil.disk_io_counters(perdisk=True) |
|
90
|
|
|
except Exception: |
|
91
|
|
|
return stats |
|
92
|
|
|
|
|
93
|
|
|
# Previous disk IO stats are stored in the diskio_old variable |
|
94
|
|
|
# By storing time data we enable Rx/s and Tx/s calculations in the |
|
95
|
|
|
# XML/RPC API, which would otherwise be overly difficult work |
|
96
|
|
|
# for users of the API |
|
97
|
|
|
time_since_update = getTimeSinceLastUpdate('disk') |
|
98
|
|
|
|
|
99
|
|
|
diskio = diskio |
|
100
|
|
|
for disk in diskio: |
|
101
|
|
|
# By default, RamFS is not displayed (issue #714) |
|
102
|
|
|
if self.args is not None and not self.args.diskio_show_ramfs and disk.startswith('ram'): |
|
103
|
|
|
continue |
|
104
|
|
|
|
|
105
|
|
|
# Do not take hide disk into account |
|
106
|
|
|
if self.is_hide(disk): |
|
107
|
|
|
continue |
|
108
|
|
|
|
|
109
|
|
|
# Compute count and bit rate |
|
110
|
|
|
try: |
|
111
|
|
|
diskstat = { |
|
112
|
|
|
'time_since_update': time_since_update, |
|
113
|
|
|
'disk_name': n(disk), |
|
114
|
|
|
'read_count': diskio[disk].read_count - \ |
|
115
|
|
|
self.diskio_old[disk].read_count, |
|
116
|
|
|
'write_count': diskio[disk].write_count - \ |
|
117
|
|
|
self.diskio_old[disk].write_count, |
|
118
|
|
|
'read_bytes': diskio[disk].read_bytes - \ |
|
119
|
|
|
self.diskio_old[disk].read_bytes, |
|
120
|
|
|
'write_bytes': diskio[disk].write_bytes - \ |
|
121
|
|
|
self.diskio_old[disk].write_bytes |
|
122
|
|
|
} |
|
123
|
|
|
except (KeyError, AttributeError): |
|
124
|
|
|
diskstat = { |
|
125
|
|
|
'time_since_update': time_since_update, |
|
126
|
|
|
'disk_name': n(disk), |
|
127
|
|
|
'read_count': 0, |
|
128
|
|
|
'write_count': 0, |
|
129
|
|
|
'read_bytes': 0, |
|
130
|
|
|
'write_bytes': 0} |
|
131
|
|
|
|
|
132
|
|
|
# Add alias if exist (define in the configuration file) |
|
133
|
|
|
if self.has_alias(disk) is not None: |
|
134
|
|
|
diskstat['alias'] = self.has_alias(disk) |
|
135
|
|
|
|
|
136
|
|
|
# Add the dict key |
|
137
|
|
|
diskstat['key'] = self.get_key() |
|
138
|
|
|
|
|
139
|
|
|
# Ad dthe current disk stat to the list |
|
140
|
|
|
stats.append(diskstat) |
|
141
|
|
|
|
|
142
|
|
|
# Save stats to compute next bitrate |
|
143
|
|
|
try: |
|
144
|
|
|
self.diskio_old = diskio |
|
145
|
|
|
except (IOError, UnboundLocalError): |
|
146
|
|
|
pass |
|
147
|
|
|
elif self.input_method == 'snmp': |
|
148
|
|
|
# Update stats using SNMP |
|
149
|
|
|
# No standard way for the moment... |
|
150
|
|
|
pass |
|
151
|
|
|
|
|
152
|
|
|
# Update the stats |
|
153
|
|
|
self.stats = stats |
|
154
|
|
|
|
|
155
|
|
|
return self.stats |
|
156
|
|
|
|
|
157
|
|
|
def update_views(self): |
|
158
|
|
|
"""Update stats views.""" |
|
159
|
|
|
# Call the father's method |
|
160
|
|
|
super(Plugin, self).update_views() |
|
161
|
|
|
|
|
162
|
|
|
# Check if the stats should be hidden |
|
163
|
|
|
self.update_views_hidden() |
|
164
|
|
|
|
|
165
|
|
|
# Add specifics informations |
|
166
|
|
|
# Alert |
|
167
|
|
|
for i in self.get_raw(): |
|
168
|
|
|
disk_real_name = i['disk_name'] |
|
169
|
|
|
self.views[i[self.get_key()]]['read_bytes']['decoration'] = self.get_alert(int(i['read_bytes'] // i['time_since_update']), |
|
170
|
|
|
header=disk_real_name + '_rx') |
|
171
|
|
|
self.views[i[self.get_key()]]['write_bytes']['decoration'] = self.get_alert(int(i['write_bytes'] // i['time_since_update']), |
|
172
|
|
|
header=disk_real_name + '_tx') |
|
173
|
|
|
|
|
174
|
|
|
def msg_curse(self, args=None, max_width=None): |
|
175
|
|
|
"""Return the dict to display in the curse interface.""" |
|
176
|
|
|
# Init the return message |
|
177
|
|
|
ret = [] |
|
178
|
|
|
|
|
179
|
|
|
# Only process if stats exist and display plugin enable... |
|
180
|
|
|
if not self.stats or self.is_disable(): |
|
181
|
|
|
return ret |
|
182
|
|
|
|
|
183
|
|
|
# Max size for the interface name |
|
184
|
|
|
name_max_width = max_width - 13 |
|
185
|
|
|
|
|
186
|
|
|
# Header |
|
187
|
|
|
msg = '{:{width}}'.format('DISK I/O', width=name_max_width) |
|
188
|
|
|
ret.append(self.curse_add_line(msg, "TITLE")) |
|
189
|
|
|
if args.diskio_iops: |
|
190
|
|
|
msg = '{:>7}'.format('IOR/s') |
|
191
|
|
|
ret.append(self.curse_add_line(msg)) |
|
192
|
|
|
msg = '{:>7}'.format('IOW/s') |
|
193
|
|
|
ret.append(self.curse_add_line(msg)) |
|
194
|
|
|
else: |
|
195
|
|
|
msg = '{:>7}'.format('R/s') |
|
196
|
|
|
ret.append(self.curse_add_line(msg)) |
|
197
|
|
|
msg = '{:>7}'.format('W/s') |
|
198
|
|
|
ret.append(self.curse_add_line(msg)) |
|
199
|
|
|
# Disk list (sorted by name) |
|
200
|
|
|
for i in self.sorted_stats(): |
|
201
|
|
|
# Hide stats if never be different from 0 (issue #1787) |
|
202
|
|
|
if all([self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields]): |
|
203
|
|
|
continue |
|
204
|
|
|
# Is there an alias for the disk name ? |
|
205
|
|
|
disk_real_name = i['disk_name'] |
|
206
|
|
|
disk_name = self.has_alias(i['disk_name']) |
|
207
|
|
|
if disk_name is None: |
|
208
|
|
|
disk_name = disk_real_name |
|
209
|
|
|
# New line |
|
210
|
|
|
ret.append(self.curse_new_line()) |
|
211
|
|
|
if len(disk_name) > name_max_width: |
|
212
|
|
|
# Cut disk name if it is too long |
|
213
|
|
|
disk_name = '_' + disk_name[-name_max_width+1:] |
|
214
|
|
|
msg = '{:{width}}'.format(nativestr(disk_name), |
|
215
|
|
|
width=name_max_width+1) |
|
216
|
|
|
ret.append(self.curse_add_line(msg)) |
|
217
|
|
|
if args.diskio_iops: |
|
218
|
|
|
# count |
|
219
|
|
|
txps = self.auto_unit( |
|
220
|
|
|
int(i['read_count'] // i['time_since_update'])) |
|
221
|
|
|
rxps = self.auto_unit( |
|
222
|
|
|
int(i['write_count'] // i['time_since_update'])) |
|
223
|
|
|
msg = '{:>7}'.format(txps) |
|
224
|
|
|
ret.append(self.curse_add_line(msg, |
|
225
|
|
|
self.get_views(item=i[self.get_key()], |
|
226
|
|
|
key='read_count', |
|
227
|
|
|
option='decoration'))) |
|
228
|
|
|
msg = '{:>7}'.format(rxps) |
|
229
|
|
|
ret.append(self.curse_add_line(msg, |
|
230
|
|
|
self.get_views(item=i[self.get_key()], |
|
231
|
|
|
key='write_count', |
|
232
|
|
|
option='decoration'))) |
|
233
|
|
|
else: |
|
234
|
|
|
# Bitrate |
|
235
|
|
|
txps = self.auto_unit( |
|
236
|
|
|
int(i['read_bytes'] // i['time_since_update'])) |
|
237
|
|
|
rxps = self.auto_unit( |
|
238
|
|
|
int(i['write_bytes'] // i['time_since_update'])) |
|
239
|
|
|
msg = '{:>7}'.format(txps) |
|
240
|
|
|
ret.append(self.curse_add_line(msg, |
|
241
|
|
|
self.get_views(item=i[self.get_key()], |
|
242
|
|
|
key='read_bytes', |
|
243
|
|
|
option='decoration'))) |
|
244
|
|
|
msg = '{:>7}'.format(rxps) |
|
245
|
|
|
ret.append(self.curse_add_line(msg, |
|
246
|
|
|
self.get_views(item=i[self.get_key()], |
|
247
|
|
|
key='write_bytes', |
|
248
|
|
|
option='decoration'))) |
|
249
|
|
|
|
|
250
|
|
|
return ret |
|
251
|
|
|
|