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
a9aaa26d
authored
Sep 30, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
setup redis for stats
parent
db0fd796
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
8 deletions
+50
-8
balancer/stats.py
+1
-1
main.py
+21
-2
sredis/sredis.py
+28
-5
No files found.
balancer/stats.py
View file @
a9aaa26d
...
...
@@ -45,5 +45,5 @@ def get_stat_from_dc(datacenter, retry = False):
)
data
=
json
.
loads
(
req
.
content
)
stat
[
key
]
=
[
i
[
"datapoints"
][
0
][
0
]
for
i
in
data
]
print
(
stat
)
return
stat
main.py
View file @
a9aaa26d
...
...
@@ -6,6 +6,7 @@ from sredis.sredis import (
check_user
,
create_puser
,
add_datacenter
,
get_all_stats
,
set_token
,
create_superpuser
,
get_rtokens
,
...
...
@@ -129,18 +130,36 @@ def update_datacenter_stat(
return
stats
@app.get
(
"/stats/"
)
def
get_datacenter_stat
(
username
=
Depends
(
get_current_user
)
):
stats
=
get_all_stats
()
stats
=
[
ResourceStat
(
datacenter_name
=
x
.
datacenter_name
,
mem_max
=
x
.
mem_max
,
mem_used
=
x
.
mem_used
,
cpu_usage
=
x
.
cpu_usage
,
vm_count
=
x
.
vm_count
)
for
x
in
stats
]
return
stats
@app.get
(
"/tokens/"
)
def
get_tokens
(
username
=
Depends
(
get_current_user
)):
return
get_rtokens
(
username
=
username
)
@app.on_event
(
"startup"
)
@repeat_every
(
seconds
=
300
)
@repeat_every
(
seconds
=
300
,
raise_exceptions
=
True
)
def
update_datacenter_healt
():
update_datacenter_status
()
@app.on_event
(
"startup"
)
@repeat_every
(
seconds
=
200
)
@repeat_every
(
seconds
=
200
,
raise_exceptions
=
True
)
def
update_datacenter_stats_rep
():
update_datacenter_stats
()
sredis/sredis.py
View file @
a9aaa26d
...
...
@@ -62,8 +62,15 @@ def update_datacenter_stats():
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
for
dc
in
all_keys
.
values
():
logger
.
info
(
f
"Get stats from {dc}"
)
get_stat_from_dc
(
dc
,
retry
=
True
)
# update_stats(stats)
stats
=
get_stat_from_dc
(
dc
,
retry
=
True
)
rs
=
ResourceStat
(
datacenter_name
=
dc
,
mem_max
=
4096
,
mem_used
=
int
(
stats
[
"mem_usage"
][
0
]
*
4096
/
100.0
)
if
stats
[
"mem_usage"
][
0
]
else
None
,
cpu_usage
=
stats
[
"cpu_percent"
][
0
],
vm_count
=
int
(
stats
[
"vmcount"
][
0
])
)
update_stats
(
rs
)
def
add_datacenter
(
datacenter
:
str
):
...
...
@@ -144,13 +151,29 @@ def update_stats(stat: ResourceStat):
dc
=
DataCenterResource
.
find
(
DataCenterResource
.
datacenter_name
==
stat
.
datacenter_name
)
.
all
()
if
not
dc
:
dc
=
DataCenterResource
(
**
stat
)
if
not
dc
or
len
(
dc
)
==
0
:
dc
=
DataCenterResource
(
datacenter_name
=
stat
.
datacenter_name
,
mem_max
=
stat
.
mem_max
,
mem_used
=
stat
.
mem_used
,
cpu_usage
=
stat
.
cpu_usage
,
vm_count
=
stat
.
vm_count
)
dc
.
save
()
else
:
dc
.
update
(
**
stat
)
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
.
save
()
def
get_all_stats
():
dcs
=
DataCenterResource
.
find
()
.
all
()
return
dcs
def
save_rpvm
(
rpname
,
username
,
datacenter
,
id
,
token
):
rpvm
=
ResourcePool
(
rpname
=
rpname
,
owner
=
username
,
datacenter
=
datacenter
,
id
=
id
,
token
=
token
...
...
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