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
6
Merge Requests
6
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
slapos.toolbox
Commits
860c6c4b
Commit
860c6c4b
authored
Jan 04, 2023
by
Justin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
promise/plugin: Added promise to check network transit
parent
5adb1a51
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
195 additions
and
0 deletions
+195
-0
slapos/promise/plugin/check_network_transit.py
slapos/promise/plugin/check_network_transit.py
+84
-0
slapos/test/promise/plugin/test_check_network_transit.py
slapos/test/promise/plugin/test_check_network_transit.py
+111
-0
No files found.
slapos/promise/plugin/check_network_transit.py
0 → 100644
View file @
860c6c4b
import
json
import
os
import
psutil
import
time
from
psutil._common
import
bytes2human
from
.util
import
JSONPromise
from
zope.interface
import
implementer
from
slapos.grid.promise
import
interface
@
implementer
(
interface
.
IPromise
)
class
RunPromise
(
JSONPromise
):
def
__init__
(
self
,
config
):
super
(
RunPromise
,
self
).
__init__
(
config
)
# Get reference values
self
.
setPeriodicity
(
float
(
self
.
getConfig
(
'frequency'
,
1
)))
self
.
last_transit_file
=
self
.
getConfig
(
'last-transit-file'
,
'last_transit'
)
self
.
max_data_amount
=
float
(
self
.
getConfig
(
'max-data-amount'
,
10e3
))
*
1048576
# MB converted into bytes
self
.
min_data_amount
=
float
(
self
.
getConfig
(
'min-data-amount'
,
0.1
))
*
1048576
# MB converted into bytes
self
.
transit_period
=
int
(
self
.
getConfig
(
'transit-period'
,
600
))
# secondes
def
sense
(
self
):
promise_success
=
True
# Get current network statistics, see https://psutil.readthedocs.io/en/latest/#network
network_data
=
psutil
.
net_io_counters
(
nowrap
=
True
)
data_amount
=
network_data
.
bytes_recv
+
network_data
.
bytes_sent
# Log data amount
data
=
json
.
dumps
({
'network_data_amount'
:
data_amount
})
self
.
json_logger
.
info
(
"Network data amount"
,
extra
=
{
'data'
:
data
})
# Get last timestamp (i.e. last modification) of log file
try
:
t
=
os
.
path
.
getmtime
(
self
.
last_transit_file
)
except
OSError
:
t
=
0
# We recalculate every quarter of transit_period since calculate over periodicity
# can be heavy in computation
if
(
time
.
time
()
-
t
)
>
self
.
transit_period
/
4
:
open
(
self
.
last_transit_file
,
'w'
).
close
()
temp_list
=
self
.
getJsonLogDataInterval
(
self
.
transit_period
)
if
temp_list
:
# If no previous data in log
if
len
(
temp_list
)
==
1
:
pass
else
:
data_diff
=
temp_list
[
0
][
'network_data_amount'
]
-
temp_list
[
-
1
][
'network_data_amount'
]
if
data_diff
<=
self
.
min_data_amount
:
self
.
logger
.
error
(
"Network congested, data amount over the last %s seconds "
\
"reached minimum threshold: %7s (threshold is %7s)"
%
(
self
.
transit_period
,
bytes2human
(
data_diff
),
bytes2human
(
self
.
min_data_amount
)))
promise_success
=
False
if
data_diff
>=
self
.
max_data_amount
:
self
.
logger
.
error
(
"Network congested, data amount over the last %s seconds "
\
"reached maximum threshold: %7s (threshold is %7s)"
%
(
self
.
transit_period
,
bytes2human
(
data_diff
),
bytes2human
(
self
.
max_data_amount
)))
promise_success
=
False
else
:
self
.
logger
.
error
(
"Couldn't read network data from log"
)
promise_success
=
False
if
promise_success
:
self
.
logger
.
info
(
"Network transit OK"
)
def
test
(
self
):
"""
Called after sense() if the instance is still converging.
Returns success or failure based on sense results.
In this case, fail if the previous sensor result is negative.
"""
return
self
.
_test
(
result_count
=
1
,
failure_amount
=
1
)
def
anomaly
(
self
):
"""
Called after sense() if the instance has finished converging.
Returns success or failure based on sense results.
Failure signals the instance has diverged.
In this case, fail if two out of the last three results are negative.
"""
return
self
.
_anomaly
(
result_count
=
3
,
failure_amount
=
2
)
slapos/test/promise/plugin/test_check_network_transit.py
0 → 100644
View file @
860c6c4b
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2018 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
import
mock
import
os
import
time
from
collections
import
namedtuple
from
slapos.grid.promise
import
PromiseError
from
slapos.promise.plugin.check_network_transit
import
RunPromise
from
.
import
TestPromisePluginMixin
class
TestCheckNetworkTransit
(
TestPromisePluginMixin
):
promise_name
=
"monitor-network-transit.py"
def
setUp
(
self
):
super
(
TestCheckNetworkTransit
,
self
).
setUp
()
self
.
network_data
=
namedtuple
(
'network_data'
,
[
'bytes_recv'
,
'bytes_sent'
])
def
writePromise
(
self
,
**
kw
):
super
(
TestCheckNetworkTransit
,
self
).
writePromise
(
self
.
promise_name
,
"from %s import %s
\
n
extra_config_dict = %r
\
n
"
%
(
RunPromise
.
__module__
,
RunPromise
.
__name__
,
kw
))
def
runPromise
(
self
,
summary
=
None
,
failed
=
False
):
self
.
configureLauncher
(
enable_anomaly
=
True
,
force
=
True
)
with
mock
.
patch
(
'psutil.net_io_counters'
,
return_value
=
summary
):
if
failed
:
self
.
assertRaises
(
PromiseError
,
self
.
launcher
.
run
)
else
:
self
.
launcher
.
run
()
result
=
self
.
getPromiseResult
(
self
.
promise_name
)[
'result'
]
self
.
assertEqual
(
result
[
'failed'
],
failed
)
return
result
[
'message'
]
def
test_network_transit_ok
(
self
):
message
=
"Network transit OK"
mock_stats
=
{
'bytes_recv'
:
1e6
,
'bytes_sent'
:
1e6
}
self
.
writePromise
(
**
{
'transit-period'
:
1
,
})
self
.
runPromise
(
self
.
network_data
(
**
{
'bytes_recv'
:
1e3
,
'bytes_sent'
:
1e3
}))
time
.
sleep
(
0.5
)
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
def
test_network_min_nok
(
self
):
message
=
"Network congested, data amount over the last 1 seconds"
\
" reached minimum threshold: 1.4K (threshold is 102.4K)"
mock_stats
=
{
'bytes_recv'
:
1e3
,
'bytes_sent'
:
1e3
}
self
.
writePromise
(
**
{
'min-data-amount'
:
0.1
,
# MB
'transit-period'
:
1
,
})
self
.
runPromise
(
self
.
network_data
(
**
{
'bytes_recv'
:
300
,
'bytes_sent'
:
300
}))
time
.
sleep
(
0.5
)
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
def
test_network_max_nok
(
self
):
message
=
"Network congested, data amount over the last 1 seconds"
\
" reached maximum threshold: 1.1M (threshold is 1.0M)"
mock_stats
=
{
'bytes_recv'
:
0.7e6
,
'bytes_sent'
:
0.5e6
}
self
.
writePromise
(
**
{
'max-data-amount'
:
1
,
# MB
'transit-period'
:
1
,
})
self
.
runPromise
(
self
.
network_data
(
**
{
'bytes_recv'
:
300
,
'bytes_sent'
:
300
}))
time
.
sleep
(
0.5
)
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
def
test_network_transit_nok
(
self
):
message
=
"Network congested, data amount over the last 1 seconds reached minimum threshold: 0.0B (threshold is 0.0B)
\
n
"
\
"Network congested, data amount over the last 1 seconds reached maximum threshold: 0.0B (threshold is 0.0B)"
mock_stats
=
{
'bytes_recv'
:
1e6
,
'bytes_sent'
:
1e6
}
self
.
writePromise
(
**
{
'last_transit_file'
:
'last_transit_file'
,
'max-data-amount'
:
0
,
'min-data-amount'
:
0
,
'transit-period'
:
1
,
})
self
.
runPromise
(
self
.
network_data
(
**
{
'bytes_recv'
:
1e6
,
'bytes_sent'
:
1e6
}))
time
.
sleep
(
0.5
)
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
if
__name__
==
'__main__'
:
unittest
.
main
()
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