-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutui.py
executable file
·146 lines (135 loc) · 6.82 KB
/
nutui.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
from nut2 import PyNUTClient, PyNUTError
from threading import Thread
from time import sleep, time
from math import floor
import json
import requests
import logging
import argparse
class nutui:
def __init__(self,client=True,nutHost='localhost',ups="",login=None,password=None,debug=False,timeout=5,interval=30,apiHost="localhost",apiPort=5000):
# Display Connection Parameters for DEBUG
logging.info(f" {floor(time())}: host: {nutHost}")
logindisplay = "**********" if login else "(empty)"
logging.info(f" {floor(time())}: login: {logindisplay}")
pwdisplay = "**********" if password else "(empty)"
logging.info(f" {floor(time())}: password: {pwdisplay}")
logging.info(f" {floor(time())}: debug: {debug}")
logging.info(f" {floor(time())}: timeout: {timeout}")
logging.info(f" {floor(time())}: interval: {interval}")
logging.info(f" {floor(time())}: apiHost: {apiHost}")
logging.info(f" {floor(time())}: apiPort: {apiPort}\n")
try:
self.nutclient = PyNUTClient(host=nutHost,login=login,password=password,debug=debug,timeout=timeout)
except PyNUTError as e:
logging.info(f" {floor(time())}: Error Initializing PyNUTClient: {e}")
quit()
self.upslist = self.nutclient.list_ups()
logging.info(f" {floor(time())}: Initializing nutclient...")
logging.info(f" {floor(time())}: Monitoring {nutHost}:")
for ups in self.upslist:
logging.info(f" {floor(time())}: {ups} ({self.upslist[ups]})\n")
logging.info(f" {floor(time())}: nutUI Host {apiHost}:{apiPort}")
self.interval = interval
self.nutHost = nutHost
self.ups = ups
self.apiHost = apiHost
self.apiPort = apiPort
self.client = client
self.heartbeat = time()
self.lastheartbeat = 0
self.timeout = self.interval * 2
self.killClient = False
self.restart = False
self.start()
def start(self):
logging.info(f" {floor(time())}: Starting Up Client")
if self.client:
logging.info(f" {floor(time())}: Client Enabled")
# Setup nutclient threading
self.clientThread = Thread(target=self.nutClient, args=())
self.clientThread.daemon = False
self.clientThread.start()
def nutClient(self):
sleep(5)
nextRun = 0
while True:
if self.killClient:
break
if time() > nextRun:
for ups in self.upslist:
try:
apiURL = f"http://{self.apiHost}:{self.apiPort}/api/data/add/{ups}"
logging.debug(f" Sending {ups} data to 'http://{self.apiHost}:{self.apiPort}/api/data/add/{ups}'")
data = self.nutclient.list_vars(ups)
logging.debug(f" {floor(time())}: UPS Data Returned: {json.dumps(data)}")
request = requests.post(apiURL, json=data)
logging.info(f" {floor(time())}: {self.nutHost}/{ups} -> {apiURL} | {request.text}")
self.heartbeat = time()
except Exception as error:
# If nut2 sees the telnet connection close, set restart to True
self.restart = True if "telnet connection closed" in error else False
logging.error(f" {floor(time())}: Error : {error}")
nextRun = floor(time() + self.interval)
sleep(0.1)
def initialize(self):
init = requests.get('https://localhost/api/init')
return init
def commandLineArgs():
cla = argparse.ArgumentParser(description="Starts nutclient and nutui flask server")
cla.add_argument('-v','--verbose',required=False,action='store_true',default=False,
help="Verbose Output")
cla.add_argument('-c','--client',required=False,action='store_true',default=False,
help="Runs nutclient only. Disables the flask server")
cla.add_argument('-n','--nuthost',required=False,type=str,default='127.0.0.1',
help="ip/hostname of nut server (defaults to 127.0.0.1)")
cla.add_argument('-ah','--apihost',required=False,type=str,default='localhost',
help="ip/hostname of nutUI api server")
cla.add_argument('-ap','--apiport',required=False,type=int,default=5000,
help="port number of nutUI api server")
cla.add_argument('-i','--interval',required=False,type=int,default=60,
help="number of seconds between polling nut-server")
cla.add_argument('-l','--nutlogin',required=False,type=str,default=None,
help="nut-server login (default: None)")
cla.add_argument('-p','--nutpassword',required=False,type=str,default=None,
help="nut-server login (default: None)")
cla.add_argument('-T','--testRestart',required=False,action='store_true',default=False,
help="Tests Threading restart. Sets nutui.timeout to 3 seconds which should trigger constant restarts.")
return vars(cla.parse_args())
def run(clArgs,timeout):
nutUI = nutui(client=clArgs['client'],
nutHost=clArgs['nuthost'],
login=clArgs['nutlogin'],
password=clArgs['nutpassword'],
debug=clArgs['verbose'],
interval=clArgs['interval'],
apiHost=clArgs['apihost'],
apiPort=clArgs['apiport'],
timeout=timeout
)
return nutUI
if __name__ == "__main__":
args = commandLineArgs()
if args['verbose']:
# Print DEBUG messages to the console.
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)
logging.info(f" {floor(time())}: nutUI Client by BeardedTek\n")
timeout = 3 if args['testRestart'] else args['interval']*2
nutUI = run(args,timeout)
nutUI.timeout = 3 if args['testRestart'] else nutUI.timeout
while True:
lastHeartbeat = floor(time() - nutUI.heartbeat)
if nutUI.timeout > lastHeartbeat > nutUI.timeout*0.6 and "TakingTooLong" not in locals():
logging.warning(f"{floor(time())}: Taking longer than expected ({floor(lastHeartbeat)}/{nutUI.timeout} seconds)")
TakingTooLong = True
if lastHeartbeat > nutUI.timeout or nutUI.restart:
numRestarts =+ 1 if "numRestarts" in locals() else 1
logging.info(f" {floor(time())}: Restarting Client. ({numRestarts})\n\n")
nutUI.killClient = True
nutUI.clientThread.join()
nutUI = None
nutUI = run(args,timeout)
sleep(0.1)