Fixed time script to make use of -p option for uptime
This commit is contained in:
parent
a819195b83
commit
6cc5a298f8
40
time.py
40
time.py
|
|
@ -3,44 +3,12 @@
|
||||||
import os
|
import os
|
||||||
from config import bcolors,env
|
from config import bcolors,env
|
||||||
|
|
||||||
# Sample output: 18:30:40 up 89 days, 19:23, 4 users, load average: etc
|
time = os.popen('uptime -p').read() # Execute command on system
|
||||||
# Sample output: 18:42:20 up 5:56, 1 user, load average: 0.64, 0.62, 0.69
|
|
||||||
def filter(lsinp):
|
|
||||||
res = []
|
|
||||||
for elem in lsinp:
|
|
||||||
if ':' in elem: res.append(elem)
|
|
||||||
return res
|
|
||||||
|
|
||||||
def parseDays(inp):
|
time = time.replace('up', '')
|
||||||
tmp = inp.split('user') # should be ['18:30:40 up 89 days, 19:23, 4 ', 's load average etc']
|
time = time.strip()
|
||||||
tmp2 = tmp[0].split(',')
|
|
||||||
tmpmin = tmp2[1].split(':')
|
|
||||||
hours = tmpmin[0].strip()
|
|
||||||
minutes = tmpmin[1].strip()
|
|
||||||
tmpday = tmp2[0].split(' ')
|
|
||||||
days = tmpday[3].strip()
|
|
||||||
return (days,hours,minutes)
|
|
||||||
|
|
||||||
def parseNoDays(inp):
|
|
||||||
tmp = inp.split(' ')
|
|
||||||
tmp = filter(tmp)
|
|
||||||
tmpmin = tmp[1].split(':')
|
|
||||||
hours = tmpmin[0].strip()
|
|
||||||
minutes = tmpmin[1].strip(',')
|
|
||||||
return (hours, minutes)
|
|
||||||
|
|
||||||
def prettyPrint(value, typeofvalue): return bcolors.EMPTY + value + ' ' + bcolors.RESET + typeofvalue
|
|
||||||
|
|
||||||
time = os.popen('uptime').read() # Execute command on system
|
|
||||||
|
|
||||||
print(bcolors.EMPTY + env.name + bcolors.RESET)
|
print(bcolors.EMPTY + env.name + bcolors.RESET)
|
||||||
print('Server has been running for:')
|
print('Server has been up for: [' + bcolors.EMPTY + time + bcolors.RESET + ']')
|
||||||
if 'days' in time:
|
|
||||||
days, hours, minutes = parseDays(time)
|
|
||||||
print('[' + prettyPrint(days, 'days') + ', ' + prettyPrint(hours, 'hours') + ' and ' + prettyPrint(minutes, 'minutes') + ']')
|
|
||||||
else:
|
|
||||||
hours, minutes = parseNoDays(time)
|
|
||||||
print('[' + prettyPrint(hours, 'hours') + ' and ' + prettyPrint(minutes, 'minutes') + ']')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue