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
Joanne Hugé
slapos.toolbox
Commits
5f477994
Commit
5f477994
authored
1 year ago
by
Joanne Hugé
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
promise/plugin: rearrange and rename methods
parent
38f7e51b
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
41 deletions
+39
-41
slapos/promise/plugin/check_cpu_temperature.py
slapos/promise/plugin/check_cpu_temperature.py
+3
-3
slapos/promise/plugin/check_network_transit.py
slapos/promise/plugin/check_network_transit.py
+1
-1
slapos/promise/plugin/check_ram_usage.py
slapos/promise/plugin/check_ram_usage.py
+1
-1
slapos/promise/plugin/util.py
slapos/promise/plugin/util.py
+34
-34
slapos/test/promise/plugin/test_check_cpu_temperature.py
slapos/test/promise/plugin/test_check_cpu_temperature.py
+0
-2
No files found.
slapos/promise/plugin/check_cpu_temperature.py
View file @
5f477994
...
...
@@ -10,10 +10,11 @@ from slapos.grid.promise import interface
@
implementer
(
interface
.
IPromise
)
class
RunPromise
(
JSONPromise
):
def
__init__
(
self
,
config
):
super
(
RunPromise
,
self
).
__init__
(
config
)
self
.
setPeriodicity
(
float
(
self
.
getConfig
(
'frequency'
,
2
)))
self
.
avg_flag_file
=
self
.
getConfig
(
'
last-avg-computation
-file'
,
'last_avg'
)
self
.
avg_flag_file
=
self
.
getConfig
(
'
avg-flag
-file'
,
'last_avg'
)
self
.
max_spot_temp
=
float
(
self
.
getConfig
(
'max-spot-temp'
,
90
))
# °C
self
.
max_avg_temp
=
float
(
self
.
getConfig
(
'max-avg-temp'
,
80
))
# °C
self
.
avg_temp_duration
=
int
(
self
.
getConfig
(
'avg-temp-duration'
,
600
))
# secondes
...
...
@@ -50,7 +51,7 @@ class RunPromise(JSONPromise):
t
=
0
if
(
time
.
time
()
-
t
)
>
avg_computation_period
:
open
(
self
.
avg_flag_file
,
'w'
).
close
()
temp_list
=
self
.
get
JsonLogDataI
nterval
(
self
.
avg_temp_duration
)
temp_list
=
self
.
get
_json_log_data_i
nterval
(
self
.
avg_temp_duration
)
if
temp_list
:
avg_temp
=
sum
(
x
[
'cpu_temperature'
]
for
x
in
temp_list
)
/
len
(
temp_list
)
if
avg_temp
>
self
.
max_avg_temp
:
...
...
@@ -75,7 +76,6 @@ class RunPromise(JSONPromise):
"""
return
self
.
_test
(
result_count
=
1
,
failure_amount
=
1
)
def
anomaly
(
self
):
"""
Called after sense() if the instance has finished converging.
...
...
This diff is collapsed.
Click to expand it.
slapos/promise/plugin/check_network_transit.py
View file @
5f477994
...
...
@@ -39,7 +39,7 @@ class RunPromise(JSONPromise):
# can be heavy in computation
if
(
time
.
time
()
-
t
)
>
self
.
transit_period
/
4
:
open
(
self
.
last_transit_file
,
'w'
).
close
()
temp_list
=
self
.
get
JsonLogDataI
nterval
(
self
.
transit_period
)
temp_list
=
self
.
get
_json_log_data_i
nterval
(
self
.
transit_period
)
if
temp_list
:
# If no previous data in log
if
len
(
temp_list
)
==
1
:
...
...
This diff is collapsed.
Click to expand it.
slapos/promise/plugin/check_ram_usage.py
View file @
5f477994
...
...
@@ -43,7 +43,7 @@ class RunPromise(JSONPromise):
# Get last available RAM from log file since avg_ram_period / 4
if
(
time
.
time
()
-
t
)
>
self
.
avg_ram_period
/
4
:
open
(
self
.
last_avg_ram_file
,
'w'
).
close
()
temp_list
=
self
.
get
JsonLogDataI
nterval
(
self
.
avg_ram_period
)
temp_list
=
self
.
get
_json_log_data_i
nterval
(
self
.
avg_ram_period
)
if
temp_list
:
avg_ram
=
sum
(
map
(
lambda
x
:
x
[
'available_ram'
],
temp_list
))
/
len
(
temp_list
)
if
avg_ram
<
self
.
min_avg_ram
:
...
...
This diff is collapsed.
Click to expand it.
slapos/promise/plugin/util.py
View file @
5f477994
...
...
@@ -38,37 +38,14 @@ def iter_logrotate_file_handle(path, mode='r'):
except
OSError
:
break
class
JSONPromise
(
GenericPromise
):
def
__init__
(
self
,
config
):
self
.
__name
=
config
.
get
(
'name'
,
None
)
self
.
__log_folder
=
config
.
get
(
'log-folder'
,
None
)
super
(
JSONPromise
,
self
).
__init__
(
config
)
json_log_name
=
os
.
path
.
splitext
(
self
.
__name
)[
0
]
+
'.json.log'
self
.
__json_log_file
=
os
.
path
.
join
(
self
.
__log_folder
,
json_log_name
)
self
.
json_logger
=
self
.
__makeJsonLogger
(
self
.
__json_log_file
)
def
__makeJsonLogger
(
self
,
json_log_file
):
logger
=
logging
.
getLogger
(
'json-logger'
)
logger
.
setLevel
(
logging
.
INFO
)
handler
=
logging
.
FileHandler
(
json_log_file
)
formatter
=
logging
.
Formatter
(
'{"time": "%(asctime)s", "log_level": "%(levelname)s"'
', "message": "%(message)s", "data": %(data)s}'
)
handler
.
setFormatter
(
formatter
)
logger
.
addHandler
(
handler
)
return
logger
def
getJsonLogDataInterval
(
self
,
interval
):
def
get_json_log_data_interval
(
json_log_file
,
interval
):
"""
Get all data in the last "interval" seconds from JSON log
Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...)
"""
current_time
=
datetime
.
now
()
data_list
=
[]
for
f
in
iter_logrotate_file_handle
(
self
.
__
json_log_file
,
'rb'
):
for
f
in
iter_logrotate_file_handle
(
json_log_file
,
'rb'
):
for
line
in
iter_reverse_lines
(
f
):
l
=
json
.
loads
(
line
.
decode
().
replace
(
"'"
,
'"'
))
timestamp
=
dateparser
.
parse
(
l
[
'time'
])
...
...
@@ -77,26 +54,49 @@ class JSONPromise(GenericPromise):
data_list
.
append
(
l
[
'data'
])
return
data_list
def
getJsonLogLatestTimestamp
(
log
):
def
get_json_log_latest_timestamp
(
json_log_file
):
"""
Get latest timestamp from JSON log
Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...)
"""
for
f
in
iter_logrotate_file_handle
(
self
.
__
json_log_file
,
'rb'
):
for
f
in
iter_logrotate_file_handle
(
json_log_file
,
'rb'
):
for
line
in
iter_reverse_lines
(
f
):
l
=
json
.
loads
(
line
.
decode
().
replace
(
"'"
,
'"'
))
return
dateparser
.
parse
(
l
[
'time'
]
)
return
dateparser
.
parse
(
l
[
'time'
]).
timestamp
(
)
return
0
from
dateutil
import
parser
from
slapos.grid.promise.generic
import
GenericPromise
class
JSONPromise
(
GenericPromise
):
def
__init__
(
self
,
config
):
self
.
__name
=
config
.
get
(
'name'
,
None
)
self
.
__log_folder
=
config
.
get
(
'log-folder'
,
None
)
super
(
JSONPromise
,
self
).
__init__
(
config
)
json_log_name
=
os
.
path
.
splitext
(
self
.
__name
)[
0
]
+
'.json.log'
self
.
__json_log_file
=
os
.
path
.
join
(
self
.
__log_folder
,
json_log_name
)
self
.
json_logger
=
self
.
__make_json_logger
(
self
.
__json_log_file
)
def
__make_json_logger
(
self
,
json_log_file
):
logger
=
logging
.
getLogger
(
'json-logger'
)
logger
.
setLevel
(
logging
.
INFO
)
handler
=
logging
.
FileHandler
(
json_log_file
)
formatter
=
logging
.
Formatter
(
'{"time": "%(asctime)s", "log_level": "%(levelname)s"'
', "message": "%(message)s", "data": %(data)s}'
)
handler
.
setFormatter
(
formatter
)
logger
.
addHandler
(
handler
)
return
logger
def
get_json_log_data_interval
(
self
,
interval
):
return
get_json_log_data_interval
(
self
.
__json_log_file
,
interval
)
def
tail_file
(
file_path
,
line_count
=
10
):
"""
Returns the last lines of file.
"""
line_list
=
[]
with
open
(
file_path
)
as
f
:
with
open
(
file_path
,
'rb'
)
as
f
:
BUFSIZ
=
1024
f
.
seek
(
0
,
2
)
bytes
=
f
.
tell
()
...
...
@@ -106,11 +106,11 @@ def tail_file(file_path, line_count=10):
if
bytes
-
BUFSIZ
>
0
:
# Seek back one whole BUFSIZ
f
.
seek
(
block
*
BUFSIZ
,
2
)
line_list
.
insert
(
0
,
f
.
read
(
BUFSIZ
))
line_list
.
insert
(
0
,
f
.
read
(
BUFSIZ
)
.
decode
()
)
else
:
f
.
seek
(
0
,
0
)
# only read what was not read
line_list
.
insert
(
0
,
f
.
read
(
bytes
))
line_list
.
insert
(
0
,
f
.
read
(
bytes
)
.
decode
()
)
line_len
=
line_list
[
0
].
count
(
'
\
n
'
)
size
-=
line_len
bytes
-=
BUFSIZ
...
...
This diff is collapsed.
Click to expand it.
slapos/test/promise/plugin/test_check_cpu_temperature.py
View file @
5f477994
...
...
@@ -26,7 +26,6 @@
##############################################################################
import
mock
import
os
import
time
from
slapos.grid.promise
import
PromiseError
from
slapos.promise.plugin.check_cpu_temperature
import
RunPromise
...
...
@@ -74,7 +73,6 @@ class TestCheckCpuTemperature(TestPromisePluginMixin):
})
self
.
assertEqual
(
message
,
self
.
runPromise
({
'coretemp'
:
[[
0
,
90
]]}))
def
test_avg_critical
(
self
):
message
=
"Average temperature over the last 1s reached threshold: 45.0 °C (threshold is 40.0 °C)"
self
.
writePromise
(
**
{
...
...
This diff is collapsed.
Click to expand it.
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