Added multiuser support
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
hosts:
|
hosts:
|
||||||
- google.com
|
- host: google.com
|
||||||
- amazon.com
|
users:
|
||||||
|
- root
|
||||||
|
- admin
|
||||||
|
- host: amazon.com
|
||||||
|
users:
|
||||||
|
- root
|
||||||
keys:
|
keys:
|
||||||
- name: johndoe@gmail.com
|
- name: johndoe@gmail.com
|
||||||
description: John Doe
|
description: John Doe
|
||||||
|
|||||||
23
monitor.py
23
monitor.py
@@ -5,13 +5,14 @@ import threading
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
class task_thread(threading.Thread):
|
class task_thread(threading.Thread):
|
||||||
def __init__(self, host, keys, host_length):
|
def __init__(self, host, user, keys, host_length):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.host = host
|
self.host = host
|
||||||
|
self.user = user
|
||||||
self.keys = keys
|
self.keys = keys
|
||||||
self.host_length = host_length
|
self.host_length = host_length
|
||||||
def run(self):
|
def run(self):
|
||||||
update_keys(self.host, self.keys, self.host_length)
|
update_keys(self.host, self.user, 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:
|
||||||
@@ -44,11 +45,11 @@ def parse_top_string(data):
|
|||||||
|
|
||||||
return load, cpu_percent, ram_total, ram_free
|
return load, cpu_percent, ram_total, ram_free
|
||||||
|
|
||||||
def update_keys(host, keys, host_length):
|
def update_keys(host, user, 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 = user)
|
||||||
|
|
||||||
stdin, stdout, stderr = client.exec_command('top -bn1 | grep "^top\\|^%Cpu\\|^.iB Mem"')
|
stdin, stdout, stderr = client.exec_command('top -bn1 | grep "^top\\|^%Cpu\\|^.iB Mem"')
|
||||||
stdin.close()
|
stdin.close()
|
||||||
@@ -59,18 +60,19 @@ def update_keys(host, keys, host_length):
|
|||||||
load, cpu_percent, ram_total, ram_free = parse_top_string(data)
|
load, cpu_percent, ram_total, ram_free = parse_top_string(data)
|
||||||
ram_used = ram_total - ram_free
|
ram_used = ram_total - ram_free
|
||||||
|
|
||||||
print(('✅ ' + host).ljust(host_length + 5) + 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) + '%)')
|
print(('✅ ' + user + '@' + host).ljust(host_length + 5) + 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) + '%)')
|
||||||
|
|
||||||
except:
|
except:
|
||||||
print('❌ ' + host)
|
print('❌ ' + user + '@' + host)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = read_config()
|
config = read_config()
|
||||||
|
|
||||||
host_length = 0
|
host_length = 0
|
||||||
for host in config['hosts']:
|
for host in config['hosts']:
|
||||||
if len(host) > host_length:
|
for user in host['users']:
|
||||||
host_length = len(host)
|
if len(user) + len(host['host']) > host_length:
|
||||||
|
host_length = len(user) + len(host['host'])
|
||||||
|
|
||||||
keys = []
|
keys = []
|
||||||
for key in config['keys']:
|
for key in config['keys']:
|
||||||
@@ -79,11 +81,12 @@ def main():
|
|||||||
print('Host'.center(host_length + 3) + ' ' + 'Load'.center(25) + ' ' + 'Ram Usage'.center(26))
|
print('Host'.center(host_length + 3) + ' ' + 'Load'.center(25) + ' ' + 'Ram Usage'.center(26))
|
||||||
|
|
||||||
for host in config['hosts']:
|
for host in config['hosts']:
|
||||||
|
for user in host['users']:
|
||||||
try:
|
try:
|
||||||
thread = task_thread(host, keys, host_length)
|
thread = task_thread(host['host'], user, keys, host_length)
|
||||||
thread.start()
|
thread.start()
|
||||||
except:
|
except:
|
||||||
print('❌ ' + host)
|
print('❌ ' + user + '@' + host['host'])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
18
sync.py
18
sync.py
@@ -5,27 +5,28 @@ 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, user, keys):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.host = host
|
self.host = host
|
||||||
|
self.user = user
|
||||||
self.keys = keys
|
self.keys = keys
|
||||||
def run(self):
|
def run(self):
|
||||||
update_keys(self.host, self.keys)
|
update_keys(self.host, self.user, self.keys)
|
||||||
|
|
||||||
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, user, keys):
|
||||||
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 = user)
|
||||||
client.exec_command('echo "###\n# Warning this file has been generated and will be overwritten!\n###\n\n' + '\n'.join(keys) + '" > ~/.ssh/authorized_keys2')
|
client.exec_command('echo "###\n# Warning this file has been generated and will be overwritten!\n###\n\n' + '\n'.join(keys) + '" > ~/.ssh/authorized_keys2')
|
||||||
client.close()
|
client.close()
|
||||||
print('✅ ' + host)
|
print('✅ ' + user + '@' + host)
|
||||||
except Exception:
|
except Exception:
|
||||||
print('❌ ' + host)
|
print('❌ ' + user + '@' + host)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = read_config()
|
config = read_config()
|
||||||
@@ -36,11 +37,12 @@ def main():
|
|||||||
keys.append(key['key'])
|
keys.append(key['key'])
|
||||||
|
|
||||||
for host in config['hosts']:
|
for host in config['hosts']:
|
||||||
|
for user in host['users']:
|
||||||
try:
|
try:
|
||||||
thread = task_thread(host, keys)
|
thread = task_thread(host['host'], user, keys)
|
||||||
thread.start()
|
thread.start()
|
||||||
except:
|
except:
|
||||||
print('❌ ' + host)
|
print('❌ ' + user + '@' + host['host'])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user