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
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
9 deletions
+51
-9
balancer/stats.py
+1
-1
main.py
+21
-2
sredis/sredis.py
+29
-6
No files found.
balancer/stats.py
View file @
a9aaa26d
...
@@ -45,5 +45,5 @@ def get_stat_from_dc(datacenter, retry = False):
...
@@ -45,5 +45,5 @@ def get_stat_from_dc(datacenter, retry = False):
)
)
data
=
json
.
loads
(
req
.
content
)
data
=
json
.
loads
(
req
.
content
)
stat
[
key
]
=
[
i
[
"datapoints"
][
0
][
0
]
for
i
in
data
]
stat
[
key
]
=
[
i
[
"datapoints"
][
0
][
0
]
for
i
in
data
]
print
(
stat
)
return
stat
return
stat
main.py
View file @
a9aaa26d
...
@@ -6,6 +6,7 @@ from sredis.sredis import (
...
@@ -6,6 +6,7 @@ from sredis.sredis import (
check_user
,
check_user
,
create_puser
,
create_puser
,
add_datacenter
,
add_datacenter
,
get_all_stats
,
set_token
,
set_token
,
create_superpuser
,
create_superpuser
,
get_rtokens
,
get_rtokens
,
...
@@ -129,18 +130,36 @@ def update_datacenter_stat(
...
@@ -129,18 +130,36 @@ def update_datacenter_stat(
return
stats
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/"
)
@app.get
(
"/tokens/"
)
def
get_tokens
(
username
=
Depends
(
get_current_user
)):
def
get_tokens
(
username
=
Depends
(
get_current_user
)):
return
get_rtokens
(
username
=
username
)
return
get_rtokens
(
username
=
username
)
@app.on_event
(
"startup"
)
@app.on_event
(
"startup"
)
@repeat_every
(
seconds
=
300
)
@repeat_every
(
seconds
=
300
,
raise_exceptions
=
True
)
def
update_datacenter_healt
():
def
update_datacenter_healt
():
update_datacenter_status
()
update_datacenter_status
()
@app.on_event
(
"startup"
)
@app.on_event
(
"startup"
)
@repeat_every
(
seconds
=
200
)
@repeat_every
(
seconds
=
200
,
raise_exceptions
=
True
)
def
update_datacenter_stats_rep
():
def
update_datacenter_stats_rep
():
update_datacenter_stats
()
update_datacenter_stats
()
sredis/sredis.py
View file @
a9aaa26d
...
@@ -62,8 +62,15 @@ def update_datacenter_stats():
...
@@ -62,8 +62,15 @@ def update_datacenter_stats():
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
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}"
)
get_stat_from_dc
(
dc
,
retry
=
True
)
stats
=
get_stat_from_dc
(
dc
,
retry
=
True
)
# update_stats(stats)
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
):
def
add_datacenter
(
datacenter
:
str
):
...
@@ -144,11 +151,27 @@ def update_stats(stat: ResourceStat):
...
@@ -144,11 +151,27 @@ def update_stats(stat: ResourceStat):
dc
=
DataCenterResource
.
find
(
dc
=
DataCenterResource
.
find
(
DataCenterResource
.
datacenter_name
==
stat
.
datacenter_name
DataCenterResource
.
datacenter_name
==
stat
.
datacenter_name
)
.
all
()
)
.
all
()
if
not
dc
:
if
not
dc
or
len
(
dc
)
==
0
:
dc
=
DataCenterResource
(
**
stat
)
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
:
else
:
dc
.
update
(
**
stat
)
dc
=
dc
[
0
]
dc
.
save
()
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
):
def
save_rpvm
(
rpname
,
username
,
datacenter
,
id
,
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