glances_batpercent.py 3.2 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
"""
Batinfo (batterie) plugin
"""
A
Alessio Sergi 已提交
22

A
Alessio Sergi 已提交
23
# Batinfo library (optional; Linux-only)
A
Alessio Sergi 已提交
24 25
try:
    import batinfo
N
Nicolas Hennion 已提交
26
except ImportError:
A
Alessio Sergi 已提交
27 28
    pass

N
Nicolas Hennion 已提交
29 30
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
31 32 33 34 35 36 37 38 39


class Plugin(GlancesPlugin):
    """
    Glances's batterie capacity Plugin

    stats is a list
    """

40 41
    def __init__(self, args=None):
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
42

N
Nicolas Hennion 已提交
43 44
        #!!! TODO: display plugin...

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

48 49 50 51 52 53 54 55 56 57 58
        # Init stats
        self.reset()        

    def reset(self):
        """
        Reset/init the stats
        """
        self.stats = []

    def update(self, input='local'):

A
Alessio Sergi 已提交
59
        """
60 61
        Update batterie capacity stats using the input method
        Input method could be: local (mandatory) or snmp (optionnal)
A
Alessio Sergi 已提交
62 63
        """

64 65 66 67 68 69 70 71 72 73 74 75
        # Reset stats
        self.reset()

        if input == 'local':
            # Update stats using the standard system lib

            self.stats = self.glancesgrabbat.getcapacitypercent()

        elif input == 'snmp':
            # Update stats using SNMP
            # Not avalaible
            pass
A
Alessio Sergi 已提交
76

77
        return self.stats
A
Alessio Sergi 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90

class glancesGrabBat:
    """
    Get batteries stats using the Batinfo librairie
    """

    def __init__(self):
        """
        Init batteries stats
        """
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
91
            self.bat_list = []
A
Alessio Sergi 已提交
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
            self.__update__()
        except Exception:
            self.initok = False

    def __update__(self):
        """
        Update the stats
        """
        if self.initok:
            try:
                self.bat.update()
            except Exception:
                self.bat_list = []
            else:
                self.bat_list = self.bat.stat
        else:
            self.bat_list = []

    def get(self):
        # Update the stats
        self.__update__()
        return self.bat_list

    def getcapacitypercent(self):
        if not self.initok or self.bat_list == []:
            return []
        # Init the bsum (sum of percent) and bcpt (number of batteries)
        # and Loop over batteries (yes a computer could have more than 1 battery)
        bsum = 0
        for bcpt in range(len(self.get())):
            try:
                bsum = bsum + int(self.bat_list[bcpt].capacity)
            except ValueError:
                return []
N
Nicolas Hennion 已提交
126
            bcpt = bcpt + 1
A
Alessio Sergi 已提交
127 128
        # Return the global percent
        return int(bsum / bcpt)