-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_cluster_info.py
66 lines (56 loc) · 2.06 KB
/
pull_cluster_info.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
import re
import json
import sys
import os
import multiprocessing
def setup():
os.system("sudo apt-get install build-essential libssl-dev libffi-dev python3-dev")
os.system("sudo apt-get install python3-setuptools && sudo easy_install3 pip")
os.system("sudo pip3 install cryptography")
os.system("sudo pip3 install spur")
try:
import spur
except ImportError:
setup()
def get_info(HOSTNAME,USERNAME,PASSWORD,FIELD):
shell = spur.SshShell(hostname=HOSTNAME,username = USERNAME ,password=PASSWORD,missing_host_key=spur.ssh.MissingHostKey.accept)
with shell:
result = shell.run(["cat","/proc/meminfo"])
res = result.output.decode("utf-8")
stripped_data = res.strip()
parsed_data = re.split(r'[\n]+',stripped_data)
info_dict = {}
for i in parsed_data:
splitted = re.split(r'\s{2,}',i)
info_dict[splitted[0].strip(":")] = float(splitted[1].split(" ")[0])
return info_dict[FIELD]
def get_total_free_memory_across_all_cluster(USERNAME, PASSWORD, FIELD):
host_names = get_cluster_hostnames()
host_memfree_dict = {}
for host in host_names:
host_memfree_dict[host] = get_info(host, USERNAME, PASSWORD, FIELD)
total_free_mem = round(sum(host_memfree_dict.values()))
total_free_mem_gigs = total_free_mem // (1024 * 1024)
return total_free_mem_gigs
def get_cpu_count():
return multiprocessing.cpu_count()
def get_cluster_hostnames():
hosts = list()
with open(os.getenv('CONTAINER_PROFILER_CLUSTERHOSTS_FILE_LOCATION')) as cluster_hostnames_file:
hosts = cluster_hostnames_file.readlines()
# removing the new line character at the end of each line
hosts = [line.strip() for line in hosts]
return hosts
if __name__ == "__main__":
if(len(sys.argv)<2):
print("USAGE: python3 pull_cluster_info.py <FIELD>")
elif(len(sys.argv)>3):
print("Too many Arguments !\n")
print("USAGE: python3 pull_cluster_info.py <FIELD>")
else:
Host_names = []
USERNAME = os.getenv('CONTAINER_PROFILER_CLUSTERAUTH_USERNAME')
PASSWORD = os.getenv('CONTAINER_PROFILER_CLUSTERAUTH_PASSWORD')
FIELD = sys.argv[1]
for host in Host_names:
print(get_info(host,USERNAME,PASSWORD,FIELD))