Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
balancer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3c569d1c
authored
Oct 24, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dc stats utils
parent
a9aaa26d
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
81 additions
and
16 deletions
+81
-16
balancer/stats.py
+39
-9
core/models.py
+1
-0
main.py
+10
-1
sredis/models.py
+1
-0
sredis/sredis.py
+30
-6
No files found.
balancer/stats.py
View file @
3c569d1c
...
@@ -5,11 +5,11 @@ import time
...
@@ -5,11 +5,11 @@ import time
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
PREFIX
=
"RBstat?
"
MEM_ALLOCATED_TARGET
=
"RBstat?target=circle.*.memory.allocated&format=json&time=-2min
"
MEM_
ALLOCATED_TARGET
=
"target=circle.*.memory.allocated&format=json&time=-1
min"
MEM_
USAGE_TARGET
=
"RBstat?target=circle.*.memory.usage&format=json&time=-2
min"
MEM_USAGE_TARGET
=
"target=circle.*.memory.usage&format=json&time=-1
min"
VMCOUNT_TARGET
=
"RBstat?target=circle.*.vmcount&format=json&time=-2
min"
VMCOUNT_TARGET
=
"target=circle.*.vmcount&format=json&time=-1
min"
CPU_PERCENT_TARGET
=
"RBstat?target=circle.*.cpu.percent&format=json&time=-2
min"
CPU_PERCENT_TARGET
=
"target=circle.*.cpu.percent&format=json&time=-1min
"
STATIC_INFO
=
"dashboard/acpi/nodesum/
"
STAT_TARGETS
=
{
STAT_TARGETS
=
{
"mem_allocated"
:
MEM_ALLOCATED_TARGET
,
"mem_allocated"
:
MEM_ALLOCATED_TARGET
,
...
@@ -18,11 +18,32 @@ STAT_TARGETS = {
...
@@ -18,11 +18,32 @@ STAT_TARGETS = {
"cpu_percent"
:
CPU_PERCENT_TARGET
,
"cpu_percent"
:
CPU_PERCENT_TARGET
,
}
}
def
get_static_info_from_dc
(
datacenter
):
url
:
str
=
f
"{datacenter}/{STATIC_INFO}"
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})"
)
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
except
json
.
decoder
.
JSONDecodeError
:
logger
.
warning
(
f
"JSON decoding error: Static infos"
)
return
0
,
0
def
get_stat_from_dc
(
datacenter
,
retry
=
False
):
def
get_stat_from_dc
(
datacenter
,
retry
=
False
):
stat
=
{}
stat
=
{}
for
key
,
target
in
STAT_TARGETS
.
items
():
for
key
,
target
in
STAT_TARGETS
.
items
():
url
:
str
=
f
"{datacenter}/{
PREFIX}{
target}"
url
:
str
=
f
"{datacenter}/{target}"
req
=
requests
.
request
(
req
=
requests
.
request
(
method
=
"GET"
,
method
=
"GET"
,
url
=
url
,
url
=
url
,
...
@@ -33,8 +54,12 @@ def get_stat_from_dc(datacenter, retry = False):
...
@@ -33,8 +54,12 @@ def get_stat_from_dc(datacenter, retry = False):
f
"cannot retrieve the data, from dc: {datacenter} ({req.status_code})"
f
"cannot retrieve the data, from dc: {datacenter} ({req.status_code})"
)
)
else
:
else
:
try
:
data
=
json
.
loads
(
req
.
content
)
data
=
json
.
loads
(
req
.
content
)
stat
[
key
]
=
[
i
[
"datapoints"
][
0
][
0
]
for
i
in
data
]
stat
[
key
]
=
[
max
(
i
[
"datapoints"
][
0
][
0
]
if
i
[
"datapoints"
][
0
][
0
]
else
0.0
,
i
[
"datapoints"
][
1
][
0
]
if
i
[
"datapoints"
][
1
][
0
]
else
0.0
)
for
i
in
data
]
if
retry
and
stat
[
key
][
0
]
is
None
:
if
retry
and
stat
[
key
][
0
]
is
None
:
time
.
sleep
(
1
)
time
.
sleep
(
1
)
...
@@ -44,6 +69,11 @@ def get_stat_from_dc(datacenter, retry = False):
...
@@ -44,6 +69,11 @@ def get_stat_from_dc(datacenter, retry = False):
verify
=
False
,
verify
=
False
,
)
)
data
=
json
.
loads
(
req
.
content
)
data
=
json
.
loads
(
req
.
content
)
stat
[
key
]
=
[
i
[
"datapoints"
][
0
][
0
]
for
i
in
data
]
stat
[
key
]
=
[
max
(
print
(
stat
)
i
[
"datapoints"
][
0
][
0
]
if
i
[
"datapoints"
][
0
][
0
]
else
0.0
,
i
[
"datapoints"
][
1
][
0
]
if
i
[
"datapoints"
][
1
][
0
]
else
0.0
)
for
i
in
data
]
except
json
.
decoder
.
JSONDecodeError
:
logger
.
warning
(
f
"JSON decoding error: key {key}"
)
stat
[
key
]
=
0.0
return
stat
return
stat
core/models.py
View file @
3c569d1c
...
@@ -53,4 +53,5 @@ class ResourceStat(BaseModel):
...
@@ -53,4 +53,5 @@ class ResourceStat(BaseModel):
mem_max
:
int
mem_max
:
int
mem_used
:
int
mem_used
:
int
cpu_usage
:
float
cpu_usage
:
float
cpu_core
:
int
vm_count
:
int
vm_count
:
int
main.py
View file @
3c569d1c
...
@@ -14,6 +14,7 @@ from sredis.sredis import (
...
@@ -14,6 +14,7 @@ from sredis.sredis import (
update_datacenter_stats
,
update_datacenter_stats
,
update_datacenter_status
,
update_datacenter_status
,
update_stats
,
update_stats
,
valid_ram_datacenters
,
)
)
import
logging
import
logging
import
requests
import
requests
...
@@ -141,11 +142,19 @@ def get_datacenter_stat(
...
@@ -141,11 +142,19 @@ def get_datacenter_stat(
mem_max
=
x
.
mem_max
,
mem_max
=
x
.
mem_max
,
mem_used
=
x
.
mem_used
,
mem_used
=
x
.
mem_used
,
cpu_usage
=
x
.
cpu_usage
,
cpu_usage
=
x
.
cpu_usage
,
vm_count
=
x
.
vm_count
vm_count
=
x
.
vm_count
,
cpu_core
=
x
.
cpu_core
)
for
x
in
stats
)
for
x
in
stats
]
]
return
stats
return
stats
@app.get
(
"/stats/ram/"
)
def
get_datacenter_stat
(
username
=
Depends
(
get_current_user
),
vm_ram
:
int
=
1024
):
stats
=
valid_ram_datacenters
(
vm_ram
)
return
stats
@app.get
(
"/tokens/"
)
@app.get
(
"/tokens/"
)
...
...
sredis/models.py
View file @
3c569d1c
...
@@ -29,3 +29,4 @@ class DataCenterResource(HashModel):
...
@@ -29,3 +29,4 @@ class DataCenterResource(HashModel):
mem_used
:
int
=
Field
()
mem_used
:
int
=
Field
()
cpu_usage
:
float
=
Field
()
cpu_usage
:
float
=
Field
()
vm_count
:
int
=
Field
()
vm_count
:
int
=
Field
()
cpu_core
:
int
=
Field
()
sredis/sredis.py
View file @
3c569d1c
import
redis
import
redis
import
requests
import
requests
from
balancer.stats
import
get_stat_from_dc
from
balancer.stats
import
get_stat_from_dc
,
get_static_info_from_dc
from
core.models
import
UserLoginSchema
,
ResourceStat
from
core.models
import
UserLoginSchema
,
ResourceStat
from
.models
import
PUser
,
DataCenterResource
,
ResourcePool
from
.models
import
PUser
,
DataCenterResource
,
ResourcePool
from
passlib.hash
import
pbkdf2_sha256
from
passlib.hash
import
pbkdf2_sha256
...
@@ -63,12 +63,15 @@ def update_datacenter_stats():
...
@@ -63,12 +63,15 @@ def update_datacenter_stats():
for
dc
in
all_keys
.
values
():
for
dc
in
all_keys
.
values
():
logger
.
info
(
f
"Get stats from {dc}"
)
logger
.
info
(
f
"Get stats from {dc}"
)
stats
=
get_stat_from_dc
(
dc
,
retry
=
True
)
stats
=
get_stat_from_dc
(
dc
,
retry
=
True
)
cpu_core
,
mem_max
=
get_static_info_from_dc
(
dc
)
rs
=
ResourceStat
(
rs
=
ResourceStat
(
datacenter_name
=
dc
,
datacenter_name
=
dc
,
mem_max
=
4096
,
cpu_core
=
cpu_core
,
mem_used
=
int
(
stats
[
"mem_usage"
][
0
]
*
4096
/
100.0
)
if
stats
[
"mem_usage"
][
0
]
else
None
,
mem_max
=
int
(
mem_max
/
1048576
)
if
mem_max
else
4096
,
cpu_usage
=
stats
[
"cpu_percent"
][
0
],
mem_allocated
=
int
(
stats
[
"mem_allocated"
][
0
]
/
1048576.0
)
if
stats
[
"mem_allocated"
]
else
0
,
vm_count
=
int
(
stats
[
"vmcount"
][
0
])
mem_used
=
int
(
stats
[
"mem_usage"
][
0
]
*
4096
/
100.0
)
if
stats
[
"mem_usage"
][
0
]
else
0
,
cpu_usage
=
float
(
stats
[
"cpu_percent"
][
0
])
if
stats
[
"cpu_percent"
][
0
]
else
0.0
,
vm_count
=
int
(
stats
[
"vmcount"
][
0
])
if
stats
[
"vmcount"
][
0
]
else
0
)
)
update_stats
(
rs
)
update_stats
(
rs
)
...
@@ -157,7 +160,8 @@ def update_stats(stat: ResourceStat):
...
@@ -157,7 +160,8 @@ def update_stats(stat: ResourceStat):
mem_max
=
stat
.
mem_max
,
mem_max
=
stat
.
mem_max
,
mem_used
=
stat
.
mem_used
,
mem_used
=
stat
.
mem_used
,
cpu_usage
=
stat
.
cpu_usage
,
cpu_usage
=
stat
.
cpu_usage
,
vm_count
=
stat
.
vm_count
vm_count
=
stat
.
vm_count
,
cpu_core
=
stat
.
cpu_core
)
)
dc
.
save
()
dc
.
save
()
else
:
else
:
...
@@ -166,8 +170,28 @@ def update_stats(stat: ResourceStat):
...
@@ -166,8 +170,28 @@ def update_stats(stat: ResourceStat):
dc
.
mem_used
=
stat
.
mem_used
if
stat
.
mem_used
else
dc
.
mem_used
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
.
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
.
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
.
save
()
dc
.
save
()
def
get_stats
(
datacenter_name
:
str
):
dc
=
DataCenterResource
.
find
(
DataCenterResource
.
datacenter_name
==
datacenter_name
)
.
get_item
()
return
dc
def
valid_ram_datacenter
(
datacenter_name
:
str
,
vm_mem
:
int
):
dc
=
DataCenterResource
.
find
(
DataCenterResource
.
datacenter_name
==
datacenter_name
)
.
get_item
()
if
dc
is
None
:
return
False
if
dc
.
mem_max
-
dc
.
mem_used
>
vm_mem
:
return
True
return
False
def
valid_ram_datacenters
(
vm_mem
:
int
):
dcs
=
DataCenterResource
.
find
()
.
all
()
valid_dcs
=
[]
for
dc
in
dcs
:
if
dc
.
mem_max
-
dc
.
mem_used
>
vm_mem
:
valid_dcs
.
append
(
dc
.
datacenter_name
)
return
valid_dcs
def
get_all_stats
():
def
get_all_stats
():
dcs
=
DataCenterResource
.
find
()
.
all
()
dcs
=
DataCenterResource
.
find
()
.
all
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment