aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/uptime.py
blob: 71a77a3fbb25eb77ea2a25396373d0094c320f53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016  Mattias Andrée (maandree@member.fsf.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''


class Uptime:
    '''
    The current uptime of the machine and how long it has been idle
    
    @variable  uptime_seconds:float                                            Total uptime
    @variable  total_idle_seconds:float                                        Total processor idle time
    @variable  average_idle_seconds:float                                      Average processor idle time
    @variable  uptime:(days:int, hours:int, minutes:int, seconds:float)        Total uptime
    @variable  total_idle:(days:int, hours:int, minutes:int, seconds:float)    Total processor idle time
    @variable  average_idle:(days:int, hours:int, minutes:int, seconds:float)  Average processor idle time
    '''
    
    cpu_count = None
    '''
    :int?  The number of processors on the machine
    '''
    
    
    def __init__(self):
        '''
        Constructor
        '''
        uptime = None
        with open('/proc/uptime', 'rb') as file:
            uptime = file.read()
        uptime = uptime.decode('utf-8', 'replace')
        uptime = uptime.replace('\n', ' ').split(' ')
        uptime, idle = float(uptime[0]), float(uptime[1])
        
        if Uptime.cpu_count is None:
            with open('/proc/cpuinfo', 'rb') as file:
                Uptime.cpu_count = file.read()
            Uptime.cpu_count = Uptime.cpu_count.decode('utf-8', 'replace').split('\n')
            Uptime.cpu_count = filter(lambda line : 'processor' in line, Uptime.cpu_count)
            Uptime.cpu_count = len(list(Uptime.cpu_count))
        
        self.uptime_seconds = uptime
        self.total_idle_seconds = idle
        self.average_idle_seconds = idle / Uptime.cpu_count
        
        self.uptime = Uptime.split_time(self.uptime_seconds)
        self.total_idle = Uptime.split_time(self.total_idle_seconds)
        self.average_idle = Uptime.split_time(self.average_idle_seconds)
    
    
    @staticmethod
    def split_time(t):
        '''
        Split a large number of seconds to days, hours, minutes and seconds
        
        @param   t:float  The number of seconds
        @return  :(days:int, hours:int, minutes:int, seconds:float)
                     The time split into days, hours [0, 23], minutes [0, 59] and seconds [0, 60[
        '''
        seconds, t = t % 60, t // 60
        minutes, t = int(t % 60), t // 60
        hours, t = int(t % 24), t // 24
        days = int(t)
        return (days, hours, minutes, seconds)