glances_batpercent.py 3.4 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
#
# 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 已提交
19 20

"""Battery plugin."""
A
Alessio Sergi 已提交
21

N
Nicolargo 已提交
22
# Import Glances libs
A
Alessio Sergi 已提交
23
from glances.core.glances_logging import logger
N
Nicolargo 已提交
24 25
from glances.plugins.glances_plugin import GlancesPlugin

A
Alessio Sergi 已提交
26
# Batinfo library (optional; Linux-only)
A
Alessio Sergi 已提交
27 28
try:
    import batinfo
N
Nicolas Hennion 已提交
29
except ImportError:
A
Alessio Sergi 已提交
30
    logger.debug("Batinfo library not found. Glances cannot grab battery info.")
A
Alessio Sergi 已提交
31 32 33


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

A
PEP 257  
Alessio Sergi 已提交
35
    """Glances battery capacity plugin.
A
Alessio Sergi 已提交
36 37 38 39

    stats is a list
    """

40
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
41
        """Init the plugin."""
42
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
43 44

        # Init the sensor class
A
Alessio Sergi 已提交
45
        self.glancesgrabbat = GlancesGrabBat()
A
Alessio Sergi 已提交
46

N
Nicolargo 已提交
47 48 49 50
        # We do not want to display the stat in a dedicated area
        # The HDD temp is displayed within the sensors plugin
        self.display_curse = False

51
        # Init stats
A
Alessio Sergi 已提交
52
        self.reset()
53 54

    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
55
        """Reset/init the stats."""
56 57
        self.stats = []

N
Nicolargo 已提交
58
    @GlancesPlugin._log_result_decorator
59
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
60
        """Update battery capacity stats using the input method."""
61 62 63
        # Reset stats
        self.reset()

64
        if self.input_method == 'local':
N
Nicolargo 已提交
65 66
            # Update stats
            self.glancesgrabbat.update()
N
Nicolargo 已提交
67
            self.stats = self.glancesgrabbat.get()
68

69
        elif self.input_method == 'snmp':
70 71 72
            # Update stats using SNMP
            # Not avalaible
            pass
A
Alessio Sergi 已提交
73

74
        return self.stats
A
Alessio Sergi 已提交
75

A
Alessio Sergi 已提交
76

A
Alessio Sergi 已提交
77
class GlancesGrabBat(object):
A
PEP 257  
Alessio Sergi 已提交
78 79

    """Get batteries stats using the batinfo library."""
A
Alessio Sergi 已提交
80 81

    def __init__(self):
A
PEP 257  
Alessio Sergi 已提交
82
        """Init batteries stats."""
A
Alessio Sergi 已提交
83 84 85
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
86
            self.bat_list = []
87
            self.update()
88
        except Exception as e:
A
Alessio Sergi 已提交
89
            self.initok = False
A
Alessio Sergi 已提交
90
            logger.debug("Cannot init GlancesGrabBat class (%s)" % e)
A
Alessio Sergi 已提交
91

92
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
93
        """Update the stats."""
A
Alessio Sergi 已提交
94
        if self.initok:
95
            self.bat.update()
A
Alessio Sergi 已提交
96 97 98 99
            self.bat_list = [{
                'label': 'Battery',
                'value': self.battery_percent,
                'unit': '%'}]
A
Alessio Sergi 已提交
100 101 102 103
        else:
            self.bat_list = []

    def get(self):
A
PEP 257  
Alessio Sergi 已提交
104
        """Get the stats."""
A
Alessio Sergi 已提交
105 106
        return self.bat_list

107 108
    @property
    def battery_percent(self):
A
PEP 257  
Alessio Sergi 已提交
109
        """Get batteries capacity percent."""
D
desbma 已提交
110
        if not self.initok or not self.bat.stat:
A
Alessio Sergi 已提交
111
            return []
N
Nicolargo 已提交
112

N
Nicolargo 已提交
113
        # Init the bsum (sum of percent)
A
Alessio Sergi 已提交
114 115
        # and Loop over batteries (yes a computer could have more than 1 battery)
        bsum = 0
N
Nicolargo 已提交
116
        for b in self.bat.stat:
A
Alessio Sergi 已提交
117
            try:
118
                bsum += int(b.capacity)
A
Alessio Sergi 已提交
119 120
            except ValueError:
                return []
N
Nicolargo 已提交
121

A
Alessio Sergi 已提交
122
        # Return the global percent
N
Nicolargo 已提交
123
        return int(bsum / len(self.bat.stat))