Completed
Push — master ( 9e1b86...12bab0 )
by Nicolas
01:03
created

glances.plugins.Plugin.update_views()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 10
rs 9.4285
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
"""File system plugin."""
21
22
import operator
23
24
from glances.plugins.glances_plugin import GlancesPlugin
25
26
import psutil
27
28
29
# SNMP OID
30
# The snmpd.conf needs to be edited.
31
# Add the following to enable it on all disk
32
# ...
33
# includeAllDisks 10%
34
# ...
35
# The OIDs are as follows (for the first disk)
36
# Path where the disk is mounted: .1.3.6.1.4.1.2021.9.1.2.1
37
# Path of the device for the partition: .1.3.6.1.4.1.2021.9.1.3.1
38
# Total size of the disk/partion (kBytes): .1.3.6.1.4.1.2021.9.1.6.1
39
# Available space on the disk: .1.3.6.1.4.1.2021.9.1.7.1
40
# Used space on the disk: .1.3.6.1.4.1.2021.9.1.8.1
41
# Percentage of space used on disk: .1.3.6.1.4.1.2021.9.1.9.1
42
# Percentage of inodes used on disk: .1.3.6.1.4.1.2021.9.1.10.1
43
snmp_oid = {'default': {'mnt_point': '1.3.6.1.4.1.2021.9.1.2',
44
                        'device_name': '1.3.6.1.4.1.2021.9.1.3',
45
                        'size': '1.3.6.1.4.1.2021.9.1.6',
46
                        'used': '1.3.6.1.4.1.2021.9.1.8',
47
                        'percent': '1.3.6.1.4.1.2021.9.1.9'},
48
            'windows': {'mnt_point': '1.3.6.1.2.1.25.2.3.1.3',
49
                        'alloc_unit': '1.3.6.1.2.1.25.2.3.1.4',
50
                        'size': '1.3.6.1.2.1.25.2.3.1.5',
51
                        'used': '1.3.6.1.2.1.25.2.3.1.6'},
52
            'netapp': {'mnt_point': '1.3.6.1.4.1.789.1.5.4.1.2',
53
                       'device_name': '1.3.6.1.4.1.789.1.5.4.1.10',
54
                       'size': '1.3.6.1.4.1.789.1.5.4.1.3',
55
                       'used': '1.3.6.1.4.1.789.1.5.4.1.4',
56
                       'percent': '1.3.6.1.4.1.789.1.5.4.1.6'}}
57
snmp_oid['esxi'] = snmp_oid['windows']
58
59
# Define the history items list
60
# All items in this list will be historised if the --enable-history tag is set
61
# 'color' define the graph color in #RGB format
62
items_history_list = [{'name': 'percent', 'color': '#00FF00'}]
63
64
65
class Plugin(GlancesPlugin):
66
67
    """Glances file system plugin.
68
69
    stats is a list
70
    """
71
72
    def __init__(self, args=None):
73
        """Init the plugin."""
74
        super(Plugin, self).__init__(args=args, items_history_list=items_history_list)
75
76
        # We want to display the stat in the curse interface
77
        self.display_curse = True
78
79
        # Init the stats
80
        self.reset()
81
82
    def get_key(self):
83
        """Return the key of the list."""
84
        return 'mnt_point'
85
86
    def reset(self):
87
        """Reset/init the stats."""
88
        self.stats = []
89
90
    @GlancesPlugin._log_result_decorator
91
    def update(self):
92
        """Update the FS stats using the input method."""
93
        # Reset the list
94
        self.reset()
95
96
        if self.input_method == 'local':
97
            # Update stats using the standard system lib
98
99
            # Grab the stats using the PsUtil disk_partitions
100
            # If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys)
101
            # and ignore all others (e.g. memory partitions such as /dev/shm)
102
            try:
103
                fs_stat = psutil.disk_partitions(all=False)
104
            except UnicodeDecodeError:
105
                return self.stats
106
107
            # Optionnal hack to allow logicals mounts points (issue #448)
108
            # Ex: Had to put 'allow=zfs' in the [fs] section of the conf file
109
            #     to allow zfs monitoring
110
            for fstype in self.get_conf_value('allow'):
111
                try:
112
                    fs_stat += [f for f in psutil.disk_partitions(all=True) if f.fstype.find(fstype) >= 0]
113
                except UnicodeDecodeError:
114
                    return self.stats
115
116
            # Loop over fs
117
            for fs in fs_stat:
118
                # Do not take hidden file system into account
119
                if self.is_hide(fs.mountpoint):
120
                    continue
121
                # Grab the disk usage
122
                try:
123
                    fs_usage = psutil.disk_usage(fs.mountpoint)
124
                except OSError:
125
                    # Correct issue #346
126
                    # Disk is ejected during the command
127
                    continue
128
                fs_current = {
129
                    'device_name': fs.device,
130
                    'fs_type': fs.fstype,
131
                    'mnt_point': fs.mountpoint,
132
                    'size': fs_usage.total,
133
                    'used': fs_usage.used,
134
                    'free': fs_usage.free,
135
                    'percent': fs_usage.percent,
136
                    'key': self.get_key()}
137
                self.stats.append(fs_current)
138
139
        elif self.input_method == 'snmp':
140
            # Update stats using SNMP
141
142
            # SNMP bulk command to get all file system in one shot
143
            try:
144
                fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
145
                                              bulk=True)
146
            except KeyError:
147
                fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'],
148
                                              bulk=True)
149
150
            # Loop over fs
151
            if self.short_system_name in ('windows', 'esxi'):
152
                # Windows or ESXi tips
153
                for fs in fs_stat:
154
                    # Memory stats are grabbed in the same OID table (ignore it)
155
                    if fs == 'Virtual Memory' or fs == 'Physical Memory' or fs == 'Real Memory':
156
                        continue
157
                    size = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit'])
158
                    used = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit'])
159
                    percent = float(used * 100 / size)
160
                    fs_current = {
161
                        'device_name': '',
162
                        'mnt_point': fs.partition(' ')[0],
163
                        'size': size,
164
                        'used': used,
165
                        'percent': percent,
166
                        'key': self.get_key()}
167
                    self.stats.append(fs_current)
168
            else:
169
                # Default behavior
170
                for fs in fs_stat:
171
                    fs_current = {
172
                        'device_name': fs_stat[fs]['device_name'],
173
                        'mnt_point': fs,
174
                        'size': int(fs_stat[fs]['size']) * 1024,
175
                        'used': int(fs_stat[fs]['used']) * 1024,
176
                        'percent': float(fs_stat[fs]['percent']),
177
                        'key': self.get_key()}
178
                    self.stats.append(fs_current)
179
180
        # Update the history list
181
        self.update_stats_history('mnt_point')
182
183
        # Update the view
184
        self.update_views()
185
186
        return self.stats
187
188
    def update_views(self):
189
        """Update stats views."""
190
        # Call the father's method
191
        super(Plugin, self).update_views()
192
193
        # Add specifics informations
194
        # Alert
195
        for i in self.stats:
196
            self.views[i[self.get_key()]]['used']['decoration'] = self.get_alert(
197
                i['used'], maximum=i['size'], header=i['mnt_point'])
198
199
    def msg_curse(self, args=None, max_width=None):
200
        """Return the dict to display in the curse interface."""
201
        # Init the return message
202
        ret = []
203
204
        # Only process if stats exist and display plugin enable...
205
        if not self.stats or args.disable_fs:
206
            return ret
207
208
        # Max size for the fsname name
209
        if max_width is not None and max_width >= 23:
210
            # Interface size name = max_width - space for interfaces bitrate
211
            fsname_max_width = max_width - 14
212
        else:
213
            fsname_max_width = 9
214
215
        # Build the string message
216
        # Header
217
        msg = '{0:{width}}'.format('FILE SYS', width=fsname_max_width)
218
        ret.append(self.curse_add_line(msg, "TITLE"))
219
        if args.fs_free_space:
220
            msg = '{0:>7}'.format('Free')
221
        else:
222
            msg = '{0:>7}'.format('Used')
223
        ret.append(self.curse_add_line(msg))
224
        msg = '{0:>7}'.format('Total')
225
        ret.append(self.curse_add_line(msg))
226
227
        # Filesystem list (sorted by name)
228
        for i in sorted(self.stats, key=operator.itemgetter(self.get_key())):
229
            # New line
230
            ret.append(self.curse_new_line())
231
            if i['device_name'] == '' or i['device_name'] == 'none':
232
                mnt_point = i['mnt_point'][-fsname_max_width + 1:]
233
            elif len(i['mnt_point']) + len(i['device_name'].split('/')[-1]) <= fsname_max_width - 3:
234
                # If possible concatenate mode info... Glances touch inside :)
235
                mnt_point = i['mnt_point'] + \
236
                    ' (' + i['device_name'].split('/')[-1] + ')'
237
            elif len(i['mnt_point']) > fsname_max_width:
238
                # Cut mount point name if it is too long
239
                mnt_point = '_' + i['mnt_point'][-fsname_max_width + 1:]
240
            else:
241
                mnt_point = i['mnt_point']
242
            msg = '{0:{width}}'.format(mnt_point, width=fsname_max_width)
243
            ret.append(self.curse_add_line(msg))
244
            if args.fs_free_space:
245
                msg = '{0:>7}'.format(self.auto_unit(i['free']))
246
            else:
247
                msg = '{0:>7}'.format(self.auto_unit(i['used']))
248
            ret.append(self.curse_add_line(msg, self.get_views(item=i[self.get_key()],
249
                                                               key='used',
250
                                                               option='decoration')))
251
            msg = '{0:>7}'.format(self.auto_unit(i['size']))
252
            ret.append(self.curse_add_line(msg))
253
254
        return ret
255