Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.toolbox
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xavier Thompson
slapos.toolbox
Commits
eec12ab7
Commit
eec12ab7
authored
Dec 14, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP
parent
3e813ce2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
39 deletions
+31
-39
slapos/promise/plugin/check_cpu_temperature.py
slapos/promise/plugin/check_cpu_temperature.py
+31
-39
No files found.
slapos/promise/plugin/check_cpu_temperature.py
View file @
eec12ab7
...
...
@@ -13,68 +13,60 @@ from slapos.grid.promise import interface
class
RunPromise
(
JSONRunPromise
):
def
__init__
(
self
,
config
):
super
(
RunPromise
,
self
).
__init__
(
config
)
self
.
setPeriodicity
(
minute
=
2
)
self
.
last_avg_computation_file
=
self
.
getConfig
(
'last-avg-computation-file'
,
'last_avg'
)
def
sense
(
self
):
promise_success
=
True
max_spot_temp
=
float
(
self
.
getConfig
(
'max-spot-temp'
,
90
))
max_avg_temp
=
float
(
self
.
getConfig
(
'max-avg-temp'
,
80
))
self
.
avg_flag_file
=
self
.
getConfig
(
'last-avg-computation-file'
,
'last_avg'
)
self
.
max_spot_temp
=
float
(
self
.
getConfig
(
'max-spot-temp'
,
90
))
self
.
max_avg_temp
=
float
(
self
.
getConfig
(
'max-avg-temp'
,
80
))
avg_temp_duration_sec
=
int
(
self
.
getConfig
(
'avg-temp-duration-sec'
,
0
))
if
avg_temp_duration_sec
:
avg_temp_duration
=
avg_temp_duration_sec
self
.
avg_temp_duration
=
avg_temp_duration_sec
else
:
avg_temp_duration
=
60
*
int
(
self
.
getConfig
(
'avg-temp-duration'
,
5
))
testing
=
self
.
getConfig
(
'testing'
)
==
"True"
self
.
avg_temp_duration
=
60
*
int
(
self
.
getConfig
(
'avg-temp-duration'
,
5
))
# For theia JHGD
#testing = True # JHGD
def
sense
(
self
):
success
=
True
# Get current temperature
if
testing
:
from
random
import
randint
cpu_temp
=
randint
(
40
,
75
)
else
:
data
=
psutil
.
sensors_temperatures
()
cpu_temp
=
data
[
'coretemp'
][
0
][
1
]
if
cpu_temp
>
max_spot_temp
:
self
.
logger
.
error
(
"Temperature reached critical threshold: %s degrees "
\
"
celsius (threshold is %s degrees celsius)"
%
(
cpu_temp
,
max_spot_temp
))
promise_success
=
False
try
:
cpu_temp
=
psutil
.
sensors_temperatures
()[
'coretemp'
][
0
][
1
]
except
(
KeyError
,
IndexError
)
as
e
:
self
.
logger
.
error
(
"Could not retrieve core temperature:
\
n
%r"
,
e
,
exc_info
=
True
)
return
if
cpu_temp
>
iself
.
max_spot_temp
:
success
=
False
self
.
logger
.
error
(
"
Temperature reached critical threshold: %s °C (threshold is %s °C)"
,
cpu_temp
,
max_spot_temp
)
# Log temperature
data
=
json
.
dumps
({
'cpu_temperature'
:
cpu_temp
})
self
.
json_logger
.
info
(
"Temperature data"
,
extra
=
{
'data'
:
data
})
# TODO: promise should computer
average only with logs between interval
# TODO: promise should compute
average only with logs between interval
# Computer average temperature
avg_computation_period
=
avg_temp_duration
/
4
avg_computation_period
=
self
.
avg_temp_duration
/
4
try
:
t
=
os
.
path
.
getmtime
(
self
.
last_avg_computation
_file
)
t
=
os
.
path
.
getmtime
(
self
.
avg_flag
_file
)
except
OSError
:
t
=
0
if
(
time
.
time
()
-
t
)
>
avg_computation_period
:
open
(
self
.
last_avg_computation
_file
,
'w'
).
close
()
temp_list
=
get_data_interval_json_log
(
self
.
log_file
,
avg_temp_duration
)
open
(
self
.
avg_flag
_file
,
'w'
).
close
()
temp_list
=
get_data_interval_json_log
(
self
.
log_file
,
self
.
avg_temp_duration
)
if
temp_list
:
avg_temp
=
sum
(
map
(
lambda
x
:
x
[
'cpu_temperature'
],
temp_list
))
/
len
(
temp_list
)
if
avg_temp
>
max_avg_temp
:
self
.
logger
.
error
(
"Average temperature over the last %s seconds "
\
"reached threshold: %s degrees celsius (threshold is %s degrees "
\
"celsius)"
%
(
avg_temp_duration
,
avg_temp
,
max_avg_temp
))
promise_success
=
False
if
avg_temp
>
self
.
max_avg_temp
:
success
=
False
self
.
logger
.
error
(
"Average temperature over the last %d s reached threshold: %s °C"
" (threshold is %s °C)"
,
avg_temp_duration
,
avg_temp
,
max_avg_temp
)
else
:
success
=
False
self
.
logger
.
error
(
"Couldn't read temperature from log"
)
promise_success
=
False
if
promise_
success
:
self
.
logger
.
info
(
"Temperature OK
"
)
if
success
:
self
.
logger
.
info
(
"Temperature OK
(%s °C)"
,
cpu_temp
)
def
test
(
self
):
"""
...
...
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