Cleaned up output
This commit is contained in:
19
monitor.py
19
monitor.py
@@ -5,40 +5,45 @@ import threading
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
class task_thread(threading.Thread):
|
class task_thread(threading.Thread):
|
||||||
def __init__(self, host, keys):
|
def __init__(self, host, keys, host_length):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.host = host
|
self.host = host
|
||||||
self.keys = keys
|
self.keys = keys
|
||||||
|
self.host_length = host_length
|
||||||
def run(self):
|
def run(self):
|
||||||
update_keys(self.host, self.keys)
|
update_keys(self.host, self.keys, self.host_length)
|
||||||
|
|
||||||
def read_config():
|
def read_config():
|
||||||
with open('config.yaml', 'r') as stream:
|
with open('config.yaml', 'r') as stream:
|
||||||
return yaml.safe_load(stream)
|
return yaml.safe_load(stream)
|
||||||
|
|
||||||
def update_keys(host, keys):
|
def update_keys(host, keys, host_length):
|
||||||
try:
|
try:
|
||||||
client = paramiko.SSHClient()
|
client = paramiko.SSHClient()
|
||||||
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
|
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
|
||||||
client.connect(host, username = 'root')
|
client.connect(host, username = 'root')
|
||||||
stdin, stdout, stderr = client.exec_command('uptime')
|
stdin, stdout, stderr = client.exec_command('uptime')
|
||||||
stdin.close()
|
stdin.close()
|
||||||
print('✅ ' + host + ': ' + stdout.read().decode('utf-8').strip().split('load average: ', 2)[1])
|
print(('✅ ' + host + ': ').ljust(host_length + 5) + stdout.read().decode('utf-8').strip().split('load average: ', 2)[1])
|
||||||
client.close()
|
client.close()
|
||||||
except Exception:
|
except:
|
||||||
print('❌ ' + host)
|
print('❌ ' + host)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = read_config()
|
config = read_config()
|
||||||
|
|
||||||
keys = []
|
host_length = 0
|
||||||
|
for host in config['hosts']:
|
||||||
|
if len(host) > host_length:
|
||||||
|
host_length = len(host)
|
||||||
|
|
||||||
|
keys = []
|
||||||
for key in config['keys']:
|
for key in config['keys']:
|
||||||
keys.append(key['key'])
|
keys.append(key['key'])
|
||||||
|
|
||||||
for host in config['hosts']:
|
for host in config['hosts']:
|
||||||
try:
|
try:
|
||||||
thread = task_thread(host, keys)
|
thread = task_thread(host, keys, host_length)
|
||||||
thread.start()
|
thread.start()
|
||||||
except:
|
except:
|
||||||
print('❌ ' + host)
|
print('❌ ' + host)
|
||||||
|
|||||||
Reference in New Issue
Block a user