glances_uptime.py 2.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 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
"""Uptime plugin."""

N
Nicolargo 已提交
22
from datetime import datetime, timedelta
A
Alessio Sergi 已提交
23

N
Nicolas Hennion 已提交
24
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
25

A
flake8  
Alessio Sergi 已提交
26 27
import psutil

N
Nicolargo 已提交
28
# SNMP OID
A
Alessio Sergi 已提交
29
snmp_oid = {'_uptime': '1.3.6.1.2.1.1.3.0'}
A
Alessio Sergi 已提交
30

A
Alessio Sergi 已提交
31

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

A
PEP 257  
Alessio Sergi 已提交
34
    """Glances uptime plugin.
A
Alessio Sergi 已提交
35 36 37 38

    stats is date (string)
    """

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

        # We want to display the stat in the curse interface
        self.display_curse = True
45

A
Alessio Sergi 已提交
46
        # Set the message position
47
        self.align = 'right'
N
Nicolargo 已提交
48

N
Nicolargo 已提交
49
        # Init the stats
A
Alessio Sergi 已提交
50
        self.reset()
A
Alessio Sergi 已提交
51

N
Nicolargo 已提交
52
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
53
        """Reset/init the stats."""
N
Nicolargo 已提交
54
        self.stats = {}
A
Alessio Sergi 已提交
55

56
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
57
        """Update uptime stat using the input method."""
N
Nicolargo 已提交
58 59 60
        # Reset stats
        self.reset()

61
        if self.input_method == 'local':
N
Nicolargo 已提交
62
            # Update stats using the standard system lib
63
            uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
N
Nicolargo 已提交
64 65 66

            # Convert uptime to string (because datetime is not JSONifi)
            self.stats = str(uptime).split('.')[0]
67
        elif self.input_method == 'snmp':
N
Nicolargo 已提交
68
            # Update stats using SNMP
69
            uptime = self.get_stats_snmp(snmp_oid=snmp_oid)['_uptime']
A
Alessio Sergi 已提交
70
            try:
N
Nicolargo 已提交
71 72
                # In hundredths of seconds
                self.stats = str(timedelta(seconds=int(uptime) / 100))
A
Alessio Sergi 已提交
73
            except Exception:
N
Nicolargo 已提交
74 75 76 77
                pass

        # Return the result
        return self.stats
A
Alessio Sergi 已提交
78 79

    def msg_curse(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
80
        """Return the string to display in the curse interface."""
81
        return [self.curse_add_line('Uptime: {}'.format(self.stats))]