Commit 86a187cb by Karsa Zoltán István

bugfix

parent 6890d130
......@@ -9,7 +9,8 @@ MEM_ALLOCATED_TARGET = "RBstat?target=circle.*.memory.allocated&format=json&time
MEM_USAGE_TARGET = "RBstat?target=circle.*.memory.usage&format=json&time=-2min"
VMCOUNT_TARGET = "RBstat?target=circle.*.vmcount&format=json&time=-2min"
CPU_PERCENT_TARGET = "RBstat?target=circle.*.cpu.percent&format=json&time=-2min"
STATIC_INFO = "dashboard/acpi/nodesum/"
STATIC_INFO_NODE = "dashboard/acpi/nodesum/"
STATIC_INFO_VM = "dashboard/acpi/vmsum/"
STAT_TARGETS = {
"mem_allocated": MEM_ALLOCATED_TARGET,
......@@ -20,7 +21,8 @@ STAT_TARGETS = {
def get_static_info_from_dc(datacenter):
url: str = f"{datacenter}/{STATIC_INFO}"
url: str = f"{datacenter}/{STATIC_INFO_NODE}"
infos = {"core_num": 0, "max_ram": 0, "core_num_sum": 0}
req = requests.request(
method="GET",
url=url,
......@@ -30,16 +32,32 @@ def get_static_info_from_dc(datacenter):
logger.warning(
f"cannot retrieve the data, from dc: {datacenter} ({req.status_code})"
)
return 0, 0
else:
try:
data = json.loads(req.content)
core_num = data["core_num"]
max_ram = data["max_ram"]
return core_num, max_ram
infos["core_num"] = data["core_num"]
infos["max_ram"] = data["max_ram"]
except json.decoder.JSONDecodeError:
logger.warning("JSON decoding error: Static infos")
return 0, 0
url = f"{datacenter}/{STATIC_INFO_VM}"
req = requests.request(
method="GET",
url=url,
verify=False,
)
if int(req.status_code / 100) != 2:
logger.warning(
f"cannot retrieve the data, from dc: {datacenter} ({req.status_code})"
)
else:
try:
data = json.loads(req.content)
infos["core_num_sum"] = data["core_num_sum"]
except json.decoder.JSONDecodeError:
logger.warning("JSON decoding error: Static infos")
return infos
def get_stat_from_dc(datacenter, retry=False):
......
......@@ -54,4 +54,5 @@ class ResourceStat(BaseModel):
mem_used: int
cpu_usage: float
cpu_core: int
vcpu_num: int
vm_count: int
......@@ -142,6 +142,7 @@ def get_datacenter_stat(username=Depends(get_current_user)):
cpu_usage=x.cpu_usage,
vm_count=x.vm_count,
cpu_core=x.cpu_core,
vcpu_num=x.vcpu_num,
)
for x in stats
]
......
......@@ -30,3 +30,4 @@ class DataCenterResource(HashModel):
cpu_usage: float = Field()
vm_count: int = Field()
cpu_core: int = Field()
vcpu_num: int = Field()
......@@ -63,21 +63,25 @@ def update_datacenter_stats():
for dc in all_keys.values():
logger.info(f"Get stats from {dc}")
stats = get_stat_from_dc(dc, retry=True)
cpu_core, mem_max = get_static_info_from_dc(dc)
infos = get_static_info_from_dc(dc)
cpu_core = infos["core_num"]
mem_max = infos["max_ram"]
vcpu_num = infos["core_num_sum"]
rs = ResourceStat(
datacenter_name=dc,
vcpu_num=vcpu_num,
cpu_core=cpu_core,
mem_max=int(mem_max / 1048576) if mem_max else 4096,
mem_allocated=int(stats["mem_allocated"][0] / 1048576.0)
if stats["mem_allocated"]
if "mem_allocated" in stats and stats["mem_allocated"][0]
else 0,
mem_used=int(stats["mem_usage"][0] * 4096 / 100.0)
if stats["mem_usage"][0]
if "mem_usage" in stats and stats["mem_usage"][0]
else 0,
cpu_usage=float(stats["cpu_percent"][0])
if stats["cpu_percent"][0]
if "cpu_percent" in stats and stats["cpu_percent"][0]
else 0.0,
vm_count=int(stats["vmcount"][0]) if stats["vmcount"][0] else 0,
vm_count=int(stats["vmcount"][0]) if "vmcount" in stats and stats["vmcount"][0] else 0,
)
update_stats(rs)
......@@ -180,15 +184,17 @@ def update_stats(stat: ResourceStat):
cpu_usage=stat.cpu_usage,
vm_count=stat.vm_count,
cpu_core=stat.cpu_core,
vcpu_num=stat.vcpu_num,
)
dc.save()
else:
dc = dc[0]
dc.mem_max = stat.mem_max if stat.mem_max else dc.mem_max
dc.mem_used = stat.mem_used if stat.mem_used else dc.mem_used
dc.cpu_usage = stat.cpu_usage if stat.cpu_usage else dc.cpu_usage
dc.vm_count = stat.vm_count if stat.vm_count else dc.vm_count
dc.cpu_core = stat.cpu_core if stat.cpu_core else dc.cpu_core
dc.mem_max = stat.mem_max if stat.mem_max is not None else dc.mem_max
dc.mem_used = stat.mem_used if stat.mem_used is not None else dc.mem_used
dc.cpu_usage = stat.cpu_usage if stat.cpu_usage is not None else dc.cpu_usage
dc.vm_count = stat.vm_count if stat.vm_count is not None else dc.vm_count
dc.cpu_core = stat.cpu_core if stat.cpu_core is not None else dc.cpu_core
dc.vcpu_num = stat.vcpu_num if stat.vcpu_num is not None else dc.vcpu_num
dc.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment