clean up
This commit is contained in:
26
monitor2.py
26
monitor2.py
@@ -5,8 +5,6 @@ import paramiko
|
|||||||
import threading
|
import threading
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
out_buf = []
|
|
||||||
|
|
||||||
def string_to_float(data):
|
def string_to_float(data):
|
||||||
return float(data.strip().split(' ', 2)[0].strip())
|
return float(data.strip().split(' ', 2)[0].strip())
|
||||||
|
|
||||||
@@ -42,7 +40,7 @@ class Host(threading.Thread):
|
|||||||
self.host_length = host_length
|
self.host_length = host_length
|
||||||
self.idx = idx
|
self.idx = idx
|
||||||
self.status = 'none'
|
self.status = 'none'
|
||||||
self.done = False
|
self.row = ''
|
||||||
|
|
||||||
def render_status(self):
|
def render_status(self):
|
||||||
if self.status == 'none':
|
if self.status == 'none':
|
||||||
@@ -68,22 +66,20 @@ class Host(threading.Thread):
|
|||||||
return load + ' (' + '{:3.1f}'.format(cpu_percent).rjust(5) + '%) ' + '{:3.1f}'.format(ram_used).rjust(5) + ' / ' + '{:3.1f}'.format(ram_total).rjust(5) + ' GiB (' + '{:3.1f}'.format(ram_used / ram_total * 100).rjust(5) + '%)'
|
return load + ' (' + '{:3.1f}'.format(cpu_percent).rjust(5) + '%) ' + '{:3.1f}'.format(ram_used).rjust(5) + ' / ' + '{:3.1f}'.format(ram_total).rjust(5) + ' GiB (' + '{:3.1f}'.format(ram_used / ram_total * 100).rjust(5) + '%)'
|
||||||
|
|
||||||
def render_row(self):
|
def render_row(self):
|
||||||
return self.render_status() + self.host.ljust(self.host_length + 3) + self.render_top()
|
self.row = self.render_status() + self.host.ljust(self.host_length + 3) + self.render_top()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
try:
|
||||||
self.status = 'none'
|
self.status = 'none'
|
||||||
out_buf[self.idx] = self.render_row()
|
self.render_row()
|
||||||
self.client = paramiko.SSHClient()
|
self.client = paramiko.SSHClient()
|
||||||
self.client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
|
self.client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
|
||||||
self.client.connect(self.host, port = self.port, username = 'root', timeout = 1)
|
self.client.connect(self.host, port = self.port, username = 'root', timeout = 1)
|
||||||
self.status = 'ok'
|
self.status = 'ok'
|
||||||
out_buf[self.idx] = self.render_row()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#print(e)
|
#print(e)
|
||||||
self.status = 'fail'
|
self.status = 'fail'
|
||||||
out_buf[self.idx] = self.render_row()
|
self.render_row()
|
||||||
self.done = True
|
|
||||||
|
|
||||||
def read_config():
|
def read_config():
|
||||||
with open('config.yaml', 'r') as stream:
|
with open('config.yaml', 'r') as stream:
|
||||||
@@ -124,7 +120,6 @@ def main():
|
|||||||
continue
|
continue
|
||||||
if not 'port' in host:
|
if not 'port' in host:
|
||||||
host['port'] = 22
|
host['port'] = 22
|
||||||
out_buf.append('')
|
|
||||||
hosts.append(Host(host['host'], host['port'] or 22, host_length, i))
|
hosts.append(Host(host['host'], host['port'] or 22, host_length, i))
|
||||||
|
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
@@ -135,13 +130,8 @@ def main():
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
screen = []
|
screen = []
|
||||||
for row in out_buf:
|
|
||||||
screen.append(row)
|
|
||||||
|
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
if not host.done:
|
screen.append(host.row)
|
||||||
break
|
|
||||||
end = True
|
|
||||||
|
|
||||||
if first:
|
if first:
|
||||||
first = False
|
first = False
|
||||||
@@ -153,6 +143,12 @@ def main():
|
|||||||
if end:
|
if end:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
for host in hosts:
|
||||||
|
if host.is_alive():
|
||||||
|
break
|
||||||
|
end = True
|
||||||
|
break
|
||||||
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user