glances_memswap.py 4.9 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# 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/>.
N
Nicolas Hennion 已提交
19 20 21
"""
Glances swap memory plugin
"""
A
Alessio Sergi 已提交
22

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

A
Alessio Sergi 已提交
25 26
import psutil

N
Nicolargo 已提交
27 28 29
# SNMP OID
# Total Swap Size: .1.3.6.1.4.1.2021.4.3.0
# Available Swap Space: .1.3.6.1.4.1.2021.4.4.0
A
Alessio Sergi 已提交
30 31
snmp_oid = {'total': '1.3.6.1.4.1.2021.4.3.0',
            'free': '1.3.6.1.4.1.2021.4.4.0'}
N
Nicolargo 已提交
32

A
Alessio Sergi 已提交
33 34 35 36 37 38 39 40

class Plugin(GlancesPlugin):
    """
    Glances's swap memory Plugin

    stats is a dict
    """

41 42
    def __init__(self, args=None):
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
43 44 45 46 47 48 49 50 51 52

        # 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 = 3
        # Enter -1 to diplay bottom
        self.line_curse = 1

53
        # Init the stats
A
Alessio Sergi 已提交
54
        self.reset()
55 56 57 58 59 60 61

    def reset(self):
        """
        Reset/init the stats
        """
        self.stats = {}

A
Alessio Sergi 已提交
62 63
    def update(self):
        """
64
        Update MEM (SWAP) stats using the input method
A
Alessio Sergi 已提交
65 66
        """

67 68 69
        # Reset stats
        self.reset()

70
        if self.get_input() == 'local':
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            # Update stats using the standard system lib
            # Grab SWAP using the PSUtil swap_memory method
            sm_stats = psutil.swap_memory()

            # Get all the swap stats (copy/paste of the PsUtil documentation)
            # total: total swap memory in bytes
            # used: used swap memory in bytes
            # free: free swap memory in bytes
            # percent: the percentage usage
            # sin: the number of bytes the system has swapped in from disk (cumulative)
            # sout: the number of bytes the system has swapped out from disk (cumulative)
            for swap in ['total', 'used', 'free', 'percent',
                         'sin', 'sout']:
                if hasattr(sm_stats, swap):
                    self.stats[swap] = getattr(sm_stats, swap)
86
        elif self.get_input() == 'snmp':
87
            # Update stats using SNMP
N
Nicolargo 已提交
88
            self.stats = self.set_stats_snmp(snmp_oid=snmp_oid)
89

A
Alessio Sergi 已提交
90
            if self.stats['total'] == '':
91 92 93
                self.reset()
                return self.stats

N
Nicolargo 已提交
94
            for key in self.stats.iterkeys():
95 96
                if self.stats[key] != '':
                    self.stats[key] = float(self.stats[key]) * 1024
N
Nicolargo 已提交
97 98 99 100 101 102

            # used=total-free
            self.stats['used'] = self.stats['total'] - self.stats['free']

            # percent: the percentage usage calculated as (total - available) / total * 100.
            self.stats['percent'] = float((self.stats['total'] - self.stats['free']) / self.stats['total'] * 100)
103 104

        return self.stats
A
Alessio Sergi 已提交
105 106 107 108 109 110 111 112

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

113
        # Only process if stats exist...
114
        if self.stats == {}:
115 116
            return ret

A
Alessio Sergi 已提交
117 118
        # Build the string message
        # Header
A
Alessio Sergi 已提交
119
        msg = '{0:7} '.format(_("SWAP"))
A
Alessio Sergi 已提交
120 121
        ret.append(self.curse_add_line(msg, "TITLE"))
        # Percent memory usage
A
Alessio Sergi 已提交
122
        msg = '{0:>6.1%}'.format(self.stats['percent'] / 100)
A
Alessio Sergi 已提交
123 124 125 126
        ret.append(self.curse_add_line(msg))
        # New line
        ret.append(self.curse_new_line())
        # Total memory usage
A
Alessio Sergi 已提交
127
        msg = '{0:8}'.format(_("total:"))
A
Alessio Sergi 已提交
128
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
129
        msg = '{0:>6}'.format(self.auto_unit(self.stats['total']))
A
Alessio Sergi 已提交
130 131 132 133
        ret.append(self.curse_add_line(msg))
        # New line
        ret.append(self.curse_new_line())
        # Used memory usage
A
Alessio Sergi 已提交
134
        msg = '{0:8}'.format(_("used:"))
A
Alessio Sergi 已提交
135
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
136
        msg = '{0:>6}'.format(self.auto_unit(self.stats['used']))
A
Alessio Sergi 已提交
137
        ret.append(self.curse_add_line(
138
            msg, self.get_alert_log(self.stats['used'], max=self.stats['total'])))
A
Alessio Sergi 已提交
139 140 141
        # New line
        ret.append(self.curse_new_line())
        # Free memory usage
A
Alessio Sergi 已提交
142
        msg = '{0:8}'.format(_("free:"))
A
Alessio Sergi 已提交
143
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
144
        msg = '{0:>6}'.format(self.auto_unit(self.stats['free']))
A
Alessio Sergi 已提交
145 146 147
        ret.append(self.curse_add_line(msg))

        return ret