glances_fs.py 10.0 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4
#
5
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
A
Alessio Sergi 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

A
PEP 257  
Alessio Sergi 已提交
20 21
"""File system plugin."""

22 23
import operator

A
flake8  
Alessio Sergi 已提交
24 25
from glances.plugins.glances_plugin import GlancesPlugin

A
Alessio Sergi 已提交
26 27
import psutil

28

N
Nicolargo 已提交
29
# SNMP OID
A
Alessio Sergi 已提交
30
# The snmpd.conf needs to be edited.
N
Nicolargo 已提交
31 32 33 34 35 36 37 38 39 40 41 42
# Add the following to enable it on all disk
# ...
# includeAllDisks 10%
# ...
# The OIDs are as follows (for the first disk)
# Path where the disk is mounted: .1.3.6.1.4.1.2021.9.1.2.1
# Path of the device for the partition: .1.3.6.1.4.1.2021.9.1.3.1
# Total size of the disk/partion (kBytes): .1.3.6.1.4.1.2021.9.1.6.1
# Available space on the disk: .1.3.6.1.4.1.2021.9.1.7.1
# Used space on the disk: .1.3.6.1.4.1.2021.9.1.8.1
# Percentage of space used on disk: .1.3.6.1.4.1.2021.9.1.9.1
# Percentage of inodes used on disk: .1.3.6.1.4.1.2021.9.1.10.1
N
Nicolargo 已提交
43 44 45 46 47 48 49 50
snmp_oid = {'default': {'mnt_point': '1.3.6.1.4.1.2021.9.1.2',
                        'device_name': '1.3.6.1.4.1.2021.9.1.3',
                        'size': '1.3.6.1.4.1.2021.9.1.6',
                        'used': '1.3.6.1.4.1.2021.9.1.8',
                        'percent': '1.3.6.1.4.1.2021.9.1.9'},
            'windows': {'mnt_point': '1.3.6.1.2.1.25.2.3.1.3',
                        'alloc_unit': '1.3.6.1.2.1.25.2.3.1.4',
                        'size': '1.3.6.1.2.1.25.2.3.1.5',
N
Nicolargo 已提交
51
                        'used': '1.3.6.1.2.1.25.2.3.1.6'},
N
Nicolargo 已提交
52 53 54 55 56
            'netapp': {'mnt_point': '1.3.6.1.4.1.789.1.5.4.1.2',
                       'device_name': '1.3.6.1.4.1.789.1.5.4.1.10',
                       'size': '1.3.6.1.4.1.789.1.5.4.1.3',
                       'used': '1.3.6.1.4.1.789.1.5.4.1.4',
                       'percent': '1.3.6.1.4.1.789.1.5.4.1.6'}}
N
nicolargo 已提交
57
snmp_oid['esxi'] = snmp_oid['windows']
N
Nicolas Hennion 已提交
58

59 60 61 62 63
# Define the history items list
# All items in this list will be historised if the --enable-history tag is set
# 'color' define the graph color in #RGB format
items_history_list = [{'name': 'percent', 'color': '#00FF00'}]

A
Alessio Sergi 已提交
64 65

class Plugin(GlancesPlugin):
A
PEP 257  
Alessio Sergi 已提交
66

A
PEP 257  
Alessio Sergi 已提交
67
    """Glances file system plugin.
A
Alessio Sergi 已提交
68 69 70 71

    stats is a list
    """

72
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
73
        """Init the plugin."""
A
Alessio Sergi 已提交
74
        super(Plugin, self).__init__(args=args, items_history_list=items_history_list)
A
Alessio Sergi 已提交
75 76 77 78

        # We want to display the stat in the curse interface
        self.display_curse = True

79
        # Init the stats
A
Alessio Sergi 已提交
80
        self.reset()
81

82
    def get_key(self):
A
PEP 257  
Alessio Sergi 已提交
83
        """Return the key of the list."""
84 85
        return 'mnt_point'

86
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
87
        """Reset/init the stats."""
88 89
        self.stats = []

90
    @GlancesPlugin._log_result_decorator
91
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
92
        """Update the FS stats using the input method."""
N
Nicolargo 已提交
93 94
        # Reset the list
        self.reset()
95

96
        if self.input_method == 'local':
97
            # Update stats using the standard system lib
N
Nicolargo 已提交
98 99 100 101 102 103 104 105 106

            # Grab the stats using the PsUtil disk_partitions
            # If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys)
            # and ignore all others (e.g. memory partitions such as /dev/shm)
            try:
                fs_stat = psutil.disk_partitions(all=False)
            except UnicodeDecodeError:
                return self.stats

107 108 109 110 111 112 113 114 115
            # Optionnal hack to allow logicals mounts points (issue #448)
            # Ex: Had to put 'allow=zfs' in the [fs] section of the conf file
            #     to allow zfs monitoring
            for fstype in self.get_conf_value('allow'):
                try:
                    fs_stat += [f for f in psutil.disk_partitions(all=True) if f.fstype.find(fstype) >= 0]
                except UnicodeDecodeError:
                    return self.stats

N
Nicolargo 已提交
116
            # Loop over fs
117
            for fs in fs_stat:
118
                # Do not take hidden file system into account
119 120
                if self.is_hide(fs.mountpoint):
                    continue
N
Nicolargo 已提交
121 122
                # Grab the disk usage
                try:
123
                    fs_usage = psutil.disk_usage(fs.mountpoint)
N
Nicolargo 已提交
124 125 126 127
                except OSError:
                    # Correct issue #346
                    # Disk is ejected during the command
                    continue
128 129 130 131 132 133
                fs_current = {
                    'device_name': fs.device,
                    'fs_type': fs.fstype,
                    'mnt_point': fs.mountpoint,
                    'size': fs_usage.total,
                    'used': fs_usage.used,
134
                    'free': fs_usage.free,
135 136
                    'percent': fs_usage.percent,
                    'key': self.get_key()}
N
Nicolargo 已提交
137 138
                self.stats.append(fs_current)

139
        elif self.input_method == 'snmp':
140
            # Update stats using SNMP
N
Nicolargo 已提交
141

142
            # SNMP bulk command to get all file system in one shot
N
Nicolargo 已提交
143
            try:
144
                fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name],
N
Nicolargo 已提交
145 146
                                              bulk=True)
            except KeyError:
147
                fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'],
N
Nicolargo 已提交
148
                                              bulk=True)
149 150

            # Loop over fs
151
            if self.short_system_name in ('windows', 'esxi'):
N
nicolargo 已提交
152
                # Windows or ESXi tips
N
Nicolargo 已提交
153
                for fs in fs_stat:
154
                    # Memory stats are grabbed in the same OID table (ignore it)
N
nicolargo 已提交
155
                    if fs == 'Virtual Memory' or fs == 'Physical Memory' or fs == 'Real Memory':
N
Nicolargo 已提交
156
                        continue
157 158 159 160 161 162 163 164 165 166
                    size = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit'])
                    used = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit'])
                    percent = float(used * 100 / size)
                    fs_current = {
                        'device_name': '',
                        'mnt_point': fs.partition(' ')[0],
                        'size': size,
                        'used': used,
                        'percent': percent,
                        'key': self.get_key()}
167
                    self.stats.append(fs_current)
N
Nicolargo 已提交
168
            else:
169
                # Default behavior
N
Nicolargo 已提交
170
                for fs in fs_stat:
171 172 173 174 175 176 177
                    fs_current = {
                        'device_name': fs_stat[fs]['device_name'],
                        'mnt_point': fs,
                        'size': int(fs_stat[fs]['size']) * 1024,
                        'used': int(fs_stat[fs]['used']) * 1024,
                        'percent': float(fs_stat[fs]['percent']),
                        'key': self.get_key()}
N
Nicolargo 已提交
178
                    self.stats.append(fs_current)
N
Nicolargo 已提交
179

180 181 182
        # Update the history list
        self.update_stats_history('mnt_point')

183 184 185
        # Update the view
        self.update_views()

186
        return self.stats
A
Alessio Sergi 已提交
187

188
    def update_views(self):
A
PEP 257  
Alessio Sergi 已提交
189
        """Update stats views."""
190
        # Call the father's method
A
Alessio Sergi 已提交
191
        super(Plugin, self).update_views()
192 193 194

        # Add specifics informations
        # 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

N
Nicolargo 已提交
199
    def msg_curse(self, args=None, max_width=None):
A
PEP 257  
Alessio Sergi 已提交
200
        """Return the dict to display in the curse interface."""
A
Alessio Sergi 已提交
201 202 203
        # Init the return message
        ret = []

204
        # Only process if stats exist and display plugin enable...
D
desbma 已提交
205
        if not self.stats or args.disable_fs:
206 207
            return ret

N
Nicolargo 已提交
208 209 210 211 212 213 214
        # Max size for the fsname name
        if max_width is not None and max_width >= 23:
            # Interface size name = max_width - space for interfaces bitrate
            fsname_max_width = max_width - 14
        else:
            fsname_max_width = 9

A
Alessio Sergi 已提交
215 216
        # Build the string message
        # Header
217
        msg = '{:{width}}'.format('FILE SYS', width=fsname_max_width)
A
Alessio Sergi 已提交
218
        ret.append(self.curse_add_line(msg, "TITLE"))
219
        if args.fs_free_space:
220
            msg = '{:>7}'.format('Free')
221
        else:
222
            msg = '{:>7}'.format('Used')
A
Alessio Sergi 已提交
223
        ret.append(self.curse_add_line(msg))
224
        msg = '{:>7}'.format('Total')
A
Alessio Sergi 已提交
225 226
        ret.append(self.curse_add_line(msg))

N
nicolargo 已提交
227
        # Filesystem list (sorted by name)
228
        for i in sorted(self.stats, key=operator.itemgetter(self.get_key())):
A
Alessio Sergi 已提交
229 230
            # New line
            ret.append(self.curse_new_line())
N
Nicolargo 已提交
231
            if i['device_name'] == '' or i['device_name'] == 'none':
A
Alessio Sergi 已提交
232
                mnt_point = i['mnt_point'][-fsname_max_width + 1:]
N
Nicolargo 已提交
233
            elif len(i['mnt_point']) + len(i['device_name'].split('/')[-1]) <= fsname_max_width - 3:
A
Alessio Sergi 已提交
234
                # If possible concatenate mode info... Glances touch inside :)
N
Nicolargo 已提交
235 236
                mnt_point = i['mnt_point'] + \
                    ' (' + i['device_name'].split('/')[-1] + ')'
N
Nicolargo 已提交
237
            elif len(i['mnt_point']) > fsname_max_width:
N
Nicolas Hennion 已提交
238
                # Cut mount point name if it is too long
A
Alessio Sergi 已提交
239
                mnt_point = '_' + i['mnt_point'][-fsname_max_width + 1:]
A
Alessio Sergi 已提交
240 241
            else:
                mnt_point = i['mnt_point']
242
            msg = '{:{width}}'.format(mnt_point, width=fsname_max_width)
A
Alessio Sergi 已提交
243
            ret.append(self.curse_add_line(msg))
244
            if args.fs_free_space:
245
                msg = '{:>7}'.format(self.auto_unit(i['free']))
246
            else:
247
                msg = '{:>7}'.format(self.auto_unit(i['used']))
248 249 250
            ret.append(self.curse_add_line(msg, self.get_views(item=i[self.get_key()],
                                                               key='used',
                                                               option='decoration')))
251
            msg = '{:>7}'.format(self.auto_unit(i['size']))
A
Alessio Sergi 已提交
252 253 254
            ret.append(self.curse_add_line(msg))

        return ret