1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# SPDX-FileCopyrightText: 2023 Nicolas Hennion <[email protected]> |
6
|
|
|
# |
7
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
8
|
|
|
# |
9
|
|
|
|
10
|
|
|
"""File system plugin.""" |
11
|
|
|
from __future__ import unicode_literals |
12
|
|
|
|
13
|
|
|
import operator |
14
|
|
|
|
15
|
|
|
from glances.globals import u, nativestr, PermissionError |
16
|
|
|
from glances.logger import logger |
17
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel |
18
|
|
|
|
19
|
|
|
import psutil |
20
|
|
|
|
21
|
|
|
# Fields description |
22
|
|
|
# description: human readable description |
23
|
|
|
# short_name: shortname to use un UI |
24
|
|
|
# unit: unit type |
25
|
|
|
# rate: is it a rate ? If yes, // by time_since_update when displayed, |
26
|
|
|
# min_symbol: Auto unit should be used if value > than 1 'X' (K, M, G)... |
27
|
|
|
fields_description = { |
28
|
|
|
'device_name': { |
29
|
|
|
'description': 'Device name.' |
30
|
|
|
}, |
31
|
|
|
'fs_type': { |
32
|
|
|
'description': 'File system type.' |
33
|
|
|
}, |
34
|
|
|
'mnt_point': { |
35
|
|
|
'description': 'Mount point.' |
36
|
|
|
}, |
37
|
|
|
'size': { |
38
|
|
|
'description': 'Total size.', |
39
|
|
|
'unit': 'byte', |
40
|
|
|
}, |
41
|
|
|
'used': { |
42
|
|
|
'description': 'Used size.', |
43
|
|
|
'unit': 'byte', |
44
|
|
|
}, |
45
|
|
|
'free': { |
46
|
|
|
'description': 'Free size.', |
47
|
|
|
'unit': 'byte', |
48
|
|
|
}, |
49
|
|
|
'percent': { |
50
|
|
|
'description': 'File system usage in percent.', |
51
|
|
|
'unit': 'percent', |
52
|
|
|
}, |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
# SNMP OID |
56
|
|
|
# The snmpd.conf needs to be edited. |
57
|
|
|
# Add the following to enable it on all disk |
58
|
|
|
# ... |
59
|
|
|
# includeAllDisks 10% |
60
|
|
|
# ... |
61
|
|
|
# The OIDs are as follows (for the first disk) |
62
|
|
|
# Path where the disk is mounted: .1.3.6.1.4.1.2021.9.1.2.1 |
63
|
|
|
# Path of the device for the partition: .1.3.6.1.4.1.2021.9.1.3.1 |
64
|
|
|
# Total size of the disk/partition (kBytes): .1.3.6.1.4.1.2021.9.1.6.1 |
65
|
|
|
# Available space on the disk: .1.3.6.1.4.1.2021.9.1.7.1 |
66
|
|
|
# Used space on the disk: .1.3.6.1.4.1.2021.9.1.8.1 |
67
|
|
|
# Percentage of space used on disk: .1.3.6.1.4.1.2021.9.1.9.1 |
68
|
|
|
# Percentage of inodes used on disk: .1.3.6.1.4.1.2021.9.1.10.1 |
69
|
|
|
snmp_oid = { |
70
|
|
|
'default': { |
71
|
|
|
'mnt_point': '1.3.6.1.4.1.2021.9.1.2', |
72
|
|
|
'device_name': '1.3.6.1.4.1.2021.9.1.3', |
73
|
|
|
'size': '1.3.6.1.4.1.2021.9.1.6', |
74
|
|
|
'used': '1.3.6.1.4.1.2021.9.1.8', |
75
|
|
|
'percent': '1.3.6.1.4.1.2021.9.1.9', |
76
|
|
|
}, |
77
|
|
|
'windows': { |
78
|
|
|
'mnt_point': '1.3.6.1.2.1.25.2.3.1.3', |
79
|
|
|
'alloc_unit': '1.3.6.1.2.1.25.2.3.1.4', |
80
|
|
|
'size': '1.3.6.1.2.1.25.2.3.1.5', |
81
|
|
|
'used': '1.3.6.1.2.1.25.2.3.1.6', |
82
|
|
|
}, |
83
|
|
|
'netapp': { |
84
|
|
|
'mnt_point': '1.3.6.1.4.1.789.1.5.4.1.2', |
85
|
|
|
'device_name': '1.3.6.1.4.1.789.1.5.4.1.10', |
86
|
|
|
'size': '1.3.6.1.4.1.789.1.5.4.1.3', |
87
|
|
|
'used': '1.3.6.1.4.1.789.1.5.4.1.4', |
88
|
|
|
'percent': '1.3.6.1.4.1.789.1.5.4.1.6', |
89
|
|
|
}, |
90
|
|
|
} |
91
|
|
|
snmp_oid['esxi'] = snmp_oid['windows'] |
92
|
|
|
|
93
|
|
|
# Define the history items list |
94
|
|
|
# All items in this list will be historised if the --enable-history tag is set |
95
|
|
|
items_history_list = [{'name': 'percent', 'description': 'File system usage in percent', 'y_unit': '%'}] |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
class PluginModel(GlancesPluginModel): |
99
|
|
|
"""Glances file system plugin. |
100
|
|
|
|
101
|
|
|
stats is a list |
102
|
|
|
""" |
103
|
|
|
|
104
|
|
|
def __init__(self, args=None, config=None): |
105
|
|
|
"""Init the plugin.""" |
106
|
|
|
super(PluginModel, self).__init__( |
107
|
|
|
args=args, config=config, |
108
|
|
|
items_history_list=items_history_list, |
109
|
|
|
stats_init_value=[], |
110
|
|
|
fields_description=fields_description |
111
|
|
|
) |
112
|
|
|
|
113
|
|
|
# We want to display the stat in the curse interface |
114
|
|
|
self.display_curse = True |
115
|
|
|
|
116
|
|
|
def get_key(self): |
117
|
|
|
"""Return the key of the list.""" |
118
|
|
|
return 'mnt_point' |
119
|
|
|
|
120
|
|
|
@GlancesPluginModel._check_decorator |
121
|
|
|
@GlancesPluginModel._log_result_decorator |
122
|
|
|
def update(self): |
123
|
|
|
"""Update the FS stats using the input method.""" |
124
|
|
|
# Init new stats |
125
|
|
|
stats = self.get_init_value() |
126
|
|
|
|
127
|
|
|
if self.input_method == 'local': |
128
|
|
|
# Update stats using the standard system lib |
129
|
|
|
|
130
|
|
|
# Grab the stats using the psutil disk_partitions |
131
|
|
|
# If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys) |
132
|
|
|
# and ignore all others (e.g. memory partitions such as /dev/shm) |
133
|
|
|
try: |
134
|
|
|
fs_stat = psutil.disk_partitions(all=False) |
135
|
|
|
except (UnicodeDecodeError, PermissionError): |
136
|
|
|
logger.debug("Plugin - fs: PsUtil fetch failed") |
137
|
|
|
return self.stats |
138
|
|
|
|
139
|
|
|
# Optional hack to allow logical mounts points (issue #448) |
140
|
|
|
allowed_fs_types = self.get_conf_value('allow') |
141
|
|
|
if allowed_fs_types: |
142
|
|
|
# Avoid Psutil call unless mounts need to be allowed |
143
|
|
|
try: |
144
|
|
|
all_mounted_fs = psutil.disk_partitions(all=True) |
145
|
|
|
except (UnicodeDecodeError, PermissionError): |
146
|
|
|
logger.debug("Plugin - fs: PsUtil extended fetch failed") |
147
|
|
|
else: |
148
|
|
|
# Discard duplicates (#2299) and add entries matching allowed fs types |
149
|
|
|
tracked_mnt_points = set(f.mountpoint for f in fs_stat) |
150
|
|
|
for f in all_mounted_fs: |
151
|
|
|
if ( |
152
|
|
|
any(f.fstype.find(fs_type) >= 0 for fs_type in allowed_fs_types) |
153
|
|
|
and f.mountpoint not in tracked_mnt_points |
154
|
|
|
): |
155
|
|
|
fs_stat.append(f) |
156
|
|
|
|
157
|
|
|
# Loop over fs |
158
|
|
|
for fs in fs_stat: |
159
|
|
|
# Hide the stats if the mount point is in the exclude list |
160
|
|
|
if not self.is_display(fs.mountpoint): |
161
|
|
|
continue |
162
|
|
|
|
163
|
|
|
# Grab the disk usage |
164
|
|
|
try: |
165
|
|
|
fs_usage = psutil.disk_usage(fs.mountpoint) |
166
|
|
|
except OSError: |
167
|
|
|
# Correct issue #346 |
168
|
|
|
# Disk is ejected during the command |
169
|
|
|
continue |
170
|
|
|
fs_current = { |
171
|
|
|
'device_name': fs.device, |
172
|
|
|
'fs_type': fs.fstype, |
173
|
|
|
# Manage non breaking space (see issue #1065) |
174
|
|
|
'mnt_point': u(fs.mountpoint).replace(u'\u00A0', ' '), |
175
|
|
|
'size': fs_usage.total, |
176
|
|
|
'used': fs_usage.used, |
177
|
|
|
'free': fs_usage.free, |
178
|
|
|
'percent': fs_usage.percent, |
179
|
|
|
'key': self.get_key(), |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
# Hide the stats if the device name is in the exclude list |
183
|
|
|
# Correct issue: glances.conf FS hide not applying #1666 |
184
|
|
|
if not self.is_display(fs_current['device_name']): |
185
|
|
|
continue |
186
|
|
|
|
187
|
|
|
# Add alias if exist (define in the configuration file) |
188
|
|
|
if self.has_alias(fs_current['mnt_point']) is not None: |
189
|
|
|
fs_current['alias'] = self.has_alias(fs_current['mnt_point']) |
190
|
|
|
|
191
|
|
|
stats.append(fs_current) |
192
|
|
|
|
193
|
|
|
elif self.input_method == 'snmp': |
194
|
|
|
# Update stats using SNMP |
195
|
|
|
|
196
|
|
|
# SNMP bulk command to get all file system in one shot |
197
|
|
|
try: |
198
|
|
|
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) |
199
|
|
|
except KeyError: |
200
|
|
|
fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'], bulk=True) |
201
|
|
|
|
202
|
|
|
# Loop over fs |
203
|
|
|
if self.short_system_name in ('windows', 'esxi'): |
204
|
|
|
# Windows or ESXi tips |
205
|
|
|
for fs in fs_stat: |
206
|
|
|
# Memory stats are grabbed in the same OID table (ignore it) |
207
|
|
|
if fs == 'Virtual Memory' or fs == 'Physical Memory' or fs == 'Real Memory': |
208
|
|
|
continue |
209
|
|
|
size = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit']) |
210
|
|
|
used = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit']) |
211
|
|
|
percent = float(used * 100 / size) |
212
|
|
|
fs_current = { |
213
|
|
|
'device_name': '', |
214
|
|
|
'mnt_point': fs.partition(' ')[0], |
215
|
|
|
'size': size, |
216
|
|
|
'used': used, |
217
|
|
|
'percent': percent, |
218
|
|
|
'key': self.get_key(), |
219
|
|
|
} |
220
|
|
|
# Do not take hidden file system into account |
221
|
|
|
if self.is_hide(fs_current['mnt_point']): |
222
|
|
|
continue |
223
|
|
|
else: |
224
|
|
|
stats.append(fs_current) |
225
|
|
|
else: |
226
|
|
|
# Default behavior |
227
|
|
|
for fs in fs_stat: |
228
|
|
|
fs_current = { |
229
|
|
|
'device_name': fs_stat[fs]['device_name'], |
230
|
|
|
'mnt_point': fs, |
231
|
|
|
'size': int(fs_stat[fs]['size']) * 1024, |
232
|
|
|
'used': int(fs_stat[fs]['used']) * 1024, |
233
|
|
|
'percent': float(fs_stat[fs]['percent']), |
234
|
|
|
'key': self.get_key(), |
235
|
|
|
} |
236
|
|
|
# Do not take hidden file system into account |
237
|
|
|
if self.is_hide(fs_current['mnt_point']) or self.is_hide(fs_current['device_name']): |
238
|
|
|
continue |
239
|
|
|
else: |
240
|
|
|
stats.append(fs_current) |
241
|
|
|
|
242
|
|
|
# Update the stats |
243
|
|
|
self.stats = stats |
244
|
|
|
|
245
|
|
|
return self.stats |
246
|
|
|
|
247
|
|
|
def update_views(self): |
248
|
|
|
"""Update stats views.""" |
249
|
|
|
# Call the father's method |
250
|
|
|
super(PluginModel, self).update_views() |
251
|
|
|
|
252
|
|
|
# Add specifics information |
253
|
|
|
# Alert |
254
|
|
|
for i in self.stats: |
255
|
|
|
self.views[i[self.get_key()]]['used']['decoration'] = self.get_alert( |
256
|
|
|
current=i['size'] - i['free'], maximum=i['size'], header=i['mnt_point'] |
257
|
|
|
) |
258
|
|
|
|
259
|
|
|
def msg_curse(self, args=None, max_width=None): |
260
|
|
|
"""Return the dict to display in the curse interface.""" |
261
|
|
|
# Init the return message |
262
|
|
|
ret = [] |
263
|
|
|
|
264
|
|
|
# Only process if stats exist and display plugin enable... |
265
|
|
|
if not self.stats or self.is_disabled(): |
266
|
|
|
return ret |
267
|
|
|
|
268
|
|
|
# Max size for the interface name |
269
|
|
|
if max_width: |
270
|
|
|
name_max_width = max_width - 13 |
271
|
|
|
else: |
272
|
|
|
# No max_width defined, return an emptu curse message |
273
|
|
|
logger.debug("No max_width defined for the {} plugin, it will not be displayed.".format(self.plugin_name)) |
274
|
|
|
return ret |
275
|
|
|
|
276
|
|
|
# Build the string message |
277
|
|
|
# Header |
278
|
|
|
msg = '{:{width}}'.format('FILE SYS', width=name_max_width) |
279
|
|
|
ret.append(self.curse_add_line(msg, "TITLE")) |
280
|
|
|
if args.fs_free_space: |
281
|
|
|
msg = '{:>8}'.format('Free') |
282
|
|
|
else: |
283
|
|
|
msg = '{:>8}'.format('Used') |
284
|
|
|
ret.append(self.curse_add_line(msg)) |
285
|
|
|
msg = '{:>7}'.format('Total') |
286
|
|
|
ret.append(self.curse_add_line(msg)) |
287
|
|
|
|
288
|
|
|
# Filesystem list (sorted by name) |
289
|
|
|
for i in sorted(self.stats, key=operator.itemgetter(self.get_key())): |
290
|
|
|
# New line |
291
|
|
|
ret.append(self.curse_new_line()) |
292
|
|
|
mnt_point = i['alias'] if 'alias' in i else i['mnt_point'] |
293
|
|
|
if len(mnt_point) + len(i['device_name'].split('/')[-1]) <= name_max_width - 3: |
294
|
|
|
# If possible concatenate mode info... Glances touch inside :) |
295
|
|
|
mnt_point = i['mnt_point'] + ' (' + i['device_name'].split('/')[-1] + ')' |
296
|
|
|
elif len(mnt_point) > name_max_width: |
297
|
|
|
mnt_point = mnt_point[:name_max_width] + '_' |
298
|
|
|
msg = '{:{width}}'.format(nativestr(mnt_point), width=name_max_width + 1) |
299
|
|
|
ret.append(self.curse_add_line(msg)) |
300
|
|
|
if args.fs_free_space: |
301
|
|
|
msg = '{:>7}'.format(self.auto_unit(i['free'])) |
302
|
|
|
else: |
303
|
|
|
msg = '{:>7}'.format(self.auto_unit(i['used'])) |
304
|
|
|
ret.append( |
305
|
|
|
self.curse_add_line(msg, self.get_views(item=i[self.get_key()], key='used', option='decoration')) |
306
|
|
|
) |
307
|
|
|
msg = '{:>7}'.format(self.auto_unit(i['size'])) |
308
|
|
|
ret.append(self.curse_add_line(msg)) |
309
|
|
|
|
310
|
|
|
return ret |
311
|
|
|
|