glances_fs.py 10.1 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
Alessio Sergi 已提交
24 25
import psutil

26 27
from glances.plugins.glances_plugin import GlancesPlugin

N
Nicolargo 已提交
28
# SNMP OID
A
Alessio Sergi 已提交
29
# The snmpd.conf needs to be edited.
N
Nicolargo 已提交
30 31 32 33 34 35 36 37 38 39 40 41
# 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 已提交
42 43 44 45 46 47 48 49
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 已提交
50
                        'used': '1.3.6.1.2.1.25.2.3.1.6'},
N
Nicolargo 已提交
51 52 53 54 55
            '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 已提交
56
snmp_oid['esxi'] = snmp_oid['windows']
N
Nicolas Hennion 已提交
57

58 59 60 61 62
# 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 已提交
63 64

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

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

    stats is a list
    """

71
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
72
        """Init the plugin."""
N
Nicolargo 已提交
73 74
        GlancesPlugin.__init__(
            self, 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 83 84 85
    def get_key(self):
        """Return the key of the list"""
        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.get_input() == '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:
N
Nicolargo 已提交
118
                fs_current = {}
119 120 121
                fs_current['device_name'] = fs.device
                fs_current['fs_type'] = fs.fstype
                fs_current['mnt_point'] = fs.mountpoint
N
Nicolargo 已提交
122 123
                # Grab the disk usage
                try:
124
                    fs_usage = psutil.disk_usage(fs.mountpoint)
N
Nicolargo 已提交
125 126 127 128 129 130
                except OSError:
                    # Correct issue #346
                    # Disk is ejected during the command
                    continue
                fs_current['size'] = fs_usage.total
                fs_current['used'] = fs_usage.used
131
                fs_current['free'] = fs_usage.total - fs_usage.used
N
Nicolargo 已提交
132
                fs_current['percent'] = fs_usage.percent
133
                fs_current['key'] = self.get_key()
N
Nicolargo 已提交
134 135
                self.stats.append(fs_current)

136
        elif self.get_input() == 'snmp':
137
            # Update stats using SNMP
N
Nicolargo 已提交
138

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

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

177 178 179
        # Update the history list
        self.update_stats_history('mnt_point')

180 181 182
        # Update the view
        self.update_views()

183
        return self.stats
A
Alessio Sergi 已提交
184

185 186 187 188 189 190 191
    def update_views(self):
        """Update stats views"""
        # Call the father's method
        GlancesPlugin.update_views(self)

        # Add specifics informations
        # Alert
192
        for i in self.stats:
193
            self.views[i[self.get_key()]]['used']['decoration'] = self.get_alert(
194
                i['used'], maximum=i['size'], header=i['mnt_point'])
195

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

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

N
Nicolargo 已提交
205 206 207 208 209 210 211
        # 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 已提交
212 213
        # Build the string message
        # Header
N
Nicolargo 已提交
214
        msg = '{0:{width}}'.format(_("FILE SYS"), width=fsname_max_width)
A
Alessio Sergi 已提交
215
        ret.append(self.curse_add_line(msg, "TITLE"))
216 217 218 219
        if args.fs_free_space:
            msg = '{0:>7}'.format(_("Free"))
        else:
            msg = '{0:>7}'.format(_("Used"))
A
Alessio Sergi 已提交
220
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
221
        msg = '{0:>7}'.format(_("Total"))
A
Alessio Sergi 已提交
222 223 224
        ret.append(self.curse_add_line(msg))

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

        return ret