aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/uptime.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/uptime.py')
-rw-r--r--src/plugins/uptime.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/plugins/uptime.py b/src/plugins/uptime.py
new file mode 100644
index 0000000..0979b7b
--- /dev/null
+++ b/src/plugins/uptime.py
@@ -0,0 +1,75 @@
+# -*- python -*-
+'''
+xpybar – xmobar replacement written in python
+Copyright © 2014 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/>.
+'''
+
+from util import *
+
+
+class Uptime:
+ '''
+ The current uptime of the machine and how long it has been idle
+
+ @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 = Uptime.split_time(uptime)
+ self.total_idle = Uptime.split_time(idle)
+ self.average_idle = Uptime.split_time(idle / Uptime.cpu_count)
+
+
+ @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)
+