Completed
Push — master ( 9e1b86...12bab0 )
by Nicolas
01:03
created

glances.plugins.Plugin.update()   B

Complexity

Conditions 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 22
rs 8.9197
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2015 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Uptime plugin."""
21
22
from datetime import datetime, timedelta
23
24
from glances.plugins.glances_plugin import GlancesPlugin
25
26
import psutil
27
28
# SNMP OID
29
snmp_oid = {'_uptime': '1.3.6.1.2.1.1.3.0'}
30
31
32
class Plugin(GlancesPlugin):
33
34
    """Glances uptime plugin.
35
36
    stats is date (string)
37
    """
38
39
    def __init__(self, args=None):
40
        """Init the plugin."""
41
        super(Plugin, self).__init__(args=args)
42
43
        # We want to display the stat in the curse interface
44
        self.display_curse = True
45
46
        # Set the message position
47
        self.align = 'right'
48
49
        # Init the stats
50
        self.reset()
51
52
    def reset(self):
53
        """Reset/init the stats."""
54
        self.stats = {}
55
56
    def update(self):
57
        """Update uptime stat using the input method."""
58
        # Reset stats
59
        self.reset()
60
61
        if self.input_method == 'local':
62
            # Update stats using the standard system lib
63
            uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
64
65
            # Convert uptime to string (because datetime is not JSONifi)
66
            self.stats = str(uptime).split('.')[0]
67
        elif self.input_method == 'snmp':
68
            # Update stats using SNMP
69
            uptime = self.get_stats_snmp(snmp_oid=snmp_oid)['_uptime']
70
            try:
71
                # In hundredths of seconds
72
                self.stats = str(timedelta(seconds=int(uptime) / 100))
73
            except Exception:
74
                pass
75
76
        # Return the result
77
        return self.stats
78
79
    def msg_curse(self, args=None):
80
        """Return the string to display in the curse interface."""
81
        return [self.curse_add_line('Uptime: {0}'.format(self.stats))]
82