-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathups-remaining.py
executable file
·48 lines (35 loc) · 1.2 KB
/
ups-remaining.py
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
#!/usr/bin/python
import subprocess
import time
import datetime
import os
import sys
from termcolor import colored
# List of UPSes to monitor. <ups_name>@<hostname> format. Or just <ups_name> is for a locally connected unit.
upses = ('ups', 'ups@fileserver1', 'ups@testbox')
os.system('clear')
try:
refresh = 15
while True:
print
print "Refreshing every", refresh, "seconds"
print "==============================="
now = datetime.datetime.now().strftime("%a %B %d %Y %H:%M:%S")
print now
print
for ups in upses:
runtime_command = 'upsc ' + ups + ' battery.runtime 2>/dev/null'
charge_command = 'upsc ' + ups + ' battery.charge 2>/dev/null'
try:
remaining_seconds = subprocess.check_output(runtime_command, shell=True)
charge = subprocess.check_output(charge_command, shell=True)
except subprocess.CalledProcessError as e:
print "Error with ups", ups
continue
remaining_seconds = int(remaining_seconds, 10)
hms = str(datetime.timedelta(seconds=remaining_seconds))
print ('%s::' % ups), "Remain:", colored(hms, 'cyan'), "Charge:", colored(charge, 'yellow')
time.sleep(refresh)
os.system('clear')
except KeyboardInterrupt:
sys.exit(0) # or 1, or whatever