Test Failed
Push — master ( 183265...afa1da )
by Nicolas
03:15 queued 16s
created

glances.plugins.fs.PluginModel.update()   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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