summaryrefslogtreecommitdiffstats
path: root/glances/plugins/glances_fs.py
blob: 898ff5dd621e1334e96b8efa98cdc184b26057fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# 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/>.

# Import system lib
from psutil import disk_partitions, disk_usage

# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin


class glancesGrabFs:
    """
    Get FS stats
    Did not exist in PSUtil, so had to create it from scratch
    """

    def __init__(self):
        """
        Init FS stats
        """

        # Init the stats
        self.fs_list = []

    def __update__(self):
        """
        Update the stats
        """
        # Reset the list
        fs_list = []

        # 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)
        fs_stat = disk_partitions(all=False)
        for fs in range(len(fs_stat)):
            fs_current = {}
            fs_current['device_name'] = fs_stat[fs].device
            fs_current['fs_type'] = fs_stat[fs].fstype
            fs_current['mnt_point'] = fs_stat[fs].mountpoint
            # Grab the disk usage
            fs_usage = disk_usage(fs_current['mnt_point'])
            fs_current['size'] = fs_usage.total
            fs_current['used'] = fs_usage.used
            fs_current['avail'] = fs_usage.free
            fs_current['percent'] = fs_usage.percent
            fs_list.append(fs_current)

        # Put the stats in the global var
        self.fs_list = fs_list

        return self.fs_list

    def get(self):
        """
        Update and return the stats
        """

        return self.__update__()


class Plugin(GlancesPlugin):
    """
    Glances's File System (fs) Plugin

    stats is a list
    """

    def __init__(self):
        GlancesPlugin.__init__(self)

        # Init the FS class
        self.glancesgrabfs = glancesGrabFs()

        # We want to display the stat in the curse interface
        self.display_curse = True
        # Set the message position
        # It is NOT the curse position but the Glances column/line
        # Enter -1 to right align
        self.column_curse = 0
        # Enter -1 to diplay bottom
        self.line_curse = 4

    def update(self):
        """
        Update  stats
        """
        self.stats = self.glancesgrabfs.get()

    def msg_curse(self, args=None):
        """
        Return the dict to display in the curse interface
        """
        # Init the return message
        ret = []

        # Only process if stats exist and display plugin enable...
        if ((self.stats == []) or (args.disable_fs)):
            return ret

        # Build the string message
        # Header
        msg = "{0:8}".format(_("FILE SYS"))
        ret.append(self.curse_add_line(msg, "TITLE"))
        msg = " {0:>6}".format(_("Used"))
        ret.append(self.curse_add_line(msg))
        msg = "  {0:>6}".format(_("Total"))
        ret.append(self.curse_add_line(msg))

        # Disk list (sorted by name)
        for i in sorted(self.stats, key=lambda fs: fs['mnt_point']):
            # New line
            ret.append(self.curse_new_line())
            if ((len(i['mnt_point']) + len(i['device_name'].split('/')[-1])) <= 5):
                # If possible concatenate mode info... Glances touch inside :) 
                mnt_point = i['mnt_point'] + ' (' + i['device_name'].split('/')[-1] + ')'
            elif (len(i['mnt_point']) > 8):
                # Cut mount point name if it is too long
                mnt_point = '_' + i['mnt_point'][-7:]
            else:
                mnt_point = i['mnt_point']
            msg = "{0:8}".format(mnt_point)
            ret.append(self.curse_add_line(msg))
            msg = " {0:>6}".format(self.auto_unit(i['used']))
            ret.append(self.curse_add_line(msg, self.get_alert(i['used'], max=i['size'])))
            msg = "  {0:>6}".format(self.auto_unit(i['size']))
            ret.append(self.curse_add_line(msg))

        return ret