2015-02-27 08:53:51 +00:00
|
|
|
import time
|
2020-11-05 09:34:31 +00:00
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
from vrtManager.connection import wvmConnect
|
|
|
|
from vrtManager.util import get_xml_path
|
|
|
|
|
|
|
|
|
2018-06-27 07:20:51 +00:00
|
|
|
def cpu_version(doc):
|
2020-11-05 09:34:31 +00:00
|
|
|
for info in doc.xpath("/sysinfo/processor/entry"):
|
|
|
|
elem = info.xpath("@name")[0]
|
|
|
|
if elem == "version":
|
2018-06-29 12:09:01 +00:00
|
|
|
return info.text
|
2020-11-05 09:34:31 +00:00
|
|
|
return "Unknown"
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class wvmHostDetails(wvmConnect):
|
|
|
|
def get_memory_usage(self):
|
|
|
|
"""
|
|
|
|
Function return memory usage on node.
|
|
|
|
"""
|
2019-05-21 06:08:42 +00:00
|
|
|
all_mem = self.wvm.getInfo()[1] * 1048576
|
|
|
|
freemem = self.wvm.getMemoryStats(-1, 0)
|
2020-04-26 13:14:34 +00:00
|
|
|
if isinstance(freemem, dict):
|
2020-11-05 09:34:31 +00:00
|
|
|
free = (freemem["buffers"] + freemem["free"] + freemem["cached"]) * 1024
|
2022-08-22 12:12:33 +00:00
|
|
|
percent = abs(100 - free * 100 // all_mem)
|
2020-11-05 09:34:31 +00:00
|
|
|
usage = all_mem - free
|
2022-08-22 12:12:33 +00:00
|
|
|
return {"total": all_mem, "usage": usage, "percent": percent}
|
2015-02-27 08:53:51 +00:00
|
|
|
else:
|
2022-08-22 12:12:33 +00:00
|
|
|
return {"total": None, "usage": None, "percent": None}
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
def get_cpu_usage(self):
|
|
|
|
"""
|
|
|
|
Function return cpu usage on node.
|
|
|
|
"""
|
|
|
|
prev_idle = 0
|
|
|
|
prev_total = 0
|
|
|
|
cpu = self.wvm.getCPUStats(-1, 0)
|
2022-08-22 12:12:33 +00:00
|
|
|
if not isinstance(cpu, dict):
|
2020-11-05 09:34:31 +00:00
|
|
|
return {"usage": None}
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
for num in range(2):
|
|
|
|
idle = self.wvm.getCPUStats(-1, 0)["idle"]
|
|
|
|
total = sum(self.wvm.getCPUStats(-1, 0).values())
|
|
|
|
diff_idle = idle - prev_idle
|
|
|
|
diff_total = total - prev_total
|
|
|
|
diff_usage = (1000 * (diff_total - diff_idle) /
|
|
|
|
diff_total + 5) / 10
|
|
|
|
prev_total = total
|
|
|
|
prev_idle = idle
|
|
|
|
if num == 0:
|
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
diff_usage = max(diff_usage, 0)
|
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return {"usage": diff_usage}
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
def get_node_info(self):
|
|
|
|
"""
|
|
|
|
Function return host server information: hostname, cpu, memory, ...
|
|
|
|
"""
|
2022-08-22 12:12:33 +00:00
|
|
|
info = [self.wvm.getHostname()] # hostname
|
2019-09-10 06:48:31 +00:00
|
|
|
info.append(self.wvm.getInfo()[0]) # architecture
|
|
|
|
info.append(self.wvm.getInfo()[1] * 1048576) # memory
|
|
|
|
info.append(self.wvm.getInfo()[2]) # cpu core count
|
|
|
|
info.append(get_xml_path(self.wvm.getSysinfo(0), func=cpu_version)) # cpu version
|
|
|
|
info.append(self.wvm.getURI()) # uri
|
2015-02-27 08:53:51 +00:00
|
|
|
return info
|