From 9c417073bc54619f549613cb13f3ce31b6c2e474 Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Thu, 4 Mar 2021 21:46:39 +0100 Subject: [PATCH] Added monitor script as a bonus --- monitor.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 monitor.py diff --git a/monitor.py b/monitor.py new file mode 100755 index 0000000..e71ce89 --- /dev/null +++ b/monitor.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import paramiko +import threading +import yaml + +class taskThread(threading.Thread): + def __init__(self, host, keys): + threading.Thread.__init__(self) + self.host = host + self.keys = keys + def run(self): + updateKeys(self.host, self.keys) + +def readConfig(): + with open('config.yaml', 'r') as stream: + return yaml.safe_load(stream) + +def updateKeys(host, keys): + try: + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) + client.connect(host, username = 'root') + stdin, stdout, stderr = client.exec_command('uptime') + stdin.close() + print('✅ ' + host + ': ' + stdout.read().decode('utf-8').strip().split('load average: ', 2)[1]) + client.close() + except Exception: + print('❌ ' + host) + +def main(): + config = readConfig() + + keys = [] + + for key in config['keys']: + keys.append(key['key']) + + for host in config['hosts']: + try: + thread = taskThread(host, keys) + thread.start() + except: + print('❌ ' + host) + +if __name__ == '__main__': + main() \ No newline at end of file