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
Lu Xu
slapos.toolbox
Commits
5adb1a51
Commit
5adb1a51
authored
Jan 04, 2023
by
Justin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
promise/plugin: Added promise to check network errors packets
parent
2a935f51
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
162 additions
and
0 deletions
+162
-0
slapos/promise/plugin/check_network_errors_packets.py
slapos/promise/plugin/check_network_errors_packets.py
+64
-0
slapos/test/promise/plugin/test_check_network_errors_packets.py
.../test/promise/plugin/test_check_network_errors_packets.py
+98
-0
No files found.
slapos/promise/plugin/check_network_errors_packets.py
0 → 100644
View file @
5adb1a51
import
psutil
import
math
from
zope.interface
import
implementer
from
slapos.grid.promise
import
interface
from
slapos.grid.promise.generic
import
GenericPromise
@
implementer
(
interface
.
IPromise
)
class
RunPromise
(
GenericPromise
):
def
__init__
(
self
,
config
):
super
(
RunPromise
,
self
).
__init__
(
config
)
# Get reference values
self
.
setPeriodicity
(
float
(
self
.
getConfig
(
'frequency'
,
5
)))
self
.
max_lost_packets
=
int
(
self
.
getConfig
(
'max-lost-packets-per-MB'
,
100
))
self
.
max_error_messages
=
int
(
self
.
getConfig
(
'max-error-messages-per-MB'
,
100
))
def
sense
(
self
):
promise_success
=
True
# Get current Network statistics
network_data
=
psutil
.
net_io_counters
()
# Get total number of bytes recv and sent in MB (if > 1MB)
if
(
network_data
.
bytes_recv
+
network_data
.
bytes_sent
)
>
1e6
:
total_MB
=
(
network_data
.
bytes_recv
+
network_data
.
bytes_sent
)
/
1e6
else
:
total_MB
=
1
# Get sum of errors and dropped packets
total_dropped
=
network_data
.
dropin
+
network_data
.
dropout
total_errors
=
network_data
.
errin
+
network_data
.
errout
# Check for network dropped packets
if
total_dropped
/
total_MB
>=
self
.
max_lost_packets
:
self
.
logger
.
error
(
"Network packets lost reached critical threshold: %s "
\
" (threshold is %s per MB)"
%
(
math
.
ceil
(
total_dropped
/
total_MB
),
self
.
max_lost_packets
))
promise_success
=
False
# Check for network errors
if
total_errors
/
total_MB
>=
self
.
max_error_messages
:
self
.
logger
.
error
(
"Network errors reached critical threshold: %s "
\
" (threshold is %s per MB)"
%
(
math
.
ceil
(
total_errors
/
total_MB
),
self
.
max_error_messages
))
promise_success
=
False
if
promise_success
:
self
.
logger
.
info
(
"Network statistics 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_errors_packets.py
0 → 100644
View file @
5adb1a51
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2022 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
from
collections
import
namedtuple
from
slapos.grid.promise
import
PromiseError
from
slapos.promise.plugin.check_network_errors_packets
import
RunPromise
from
.
import
TestPromisePluginMixin
class
TestCheckNetwork
(
TestPromisePluginMixin
):
promise_name
=
"monitor-network.py"
def
setUp
(
self
):
super
(
TestCheckNetwork
,
self
).
setUp
()
self
.
network_data
=
namedtuple
(
'network_data'
,
[
'bytes_recv'
,
'bytes_sent'
,
'errin'
,
'errout'
,
'dropin'
,
'dropout'
])
def
writePromise
(
self
,
**
kw
):
super
(
TestCheckNetwork
,
self
).
writePromise
(
self
.
promise_name
,
"from %s import %s
\
n
extra_config_dict = %r
\
n
"
%
(
RunPromise
.
__module__
,
RunPromise
.
__name__
,
kw
))
def
runPromise
(
self
,
summary
,
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_ok
(
self
):
message
=
"Network statistics OK"
mock_stats
=
{
'bytes_recv'
:
0
,
'bytes_sent'
:
0
,
'errin'
:
0
,
'errout'
:
0
,
'dropin'
:
0
,
'dropout'
:
0
}
self
.
writePromise
(
**
{
'max-lost-packets-per-MB'
:
5
,
'max-error-messages-per-MB'
:
5
,
})
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
def
test_network_dropped_packets_nok
(
self
):
message
=
"Network packets lost reached critical threshold: 10 (threshold is 5 per MB)"
mock_stats
=
{
'bytes_recv'
:
0
,
'bytes_sent'
:
0
,
'errin'
:
0
,
'errout'
:
0
,
'dropin'
:
5
,
'dropout'
:
5
}
self
.
writePromise
(
**
{
'max-lost-packets-per-MB'
:
5
,
'max-error-messages-per-MB'
:
5
,
})
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
def
test_network_errors_nok
(
self
):
message
=
"Network errors reached critical threshold: 10 (threshold is 5 per MB)"
mock_stats
=
{
'bytes_recv'
:
0
,
'bytes_sent'
:
0
,
'errin'
:
5
,
'errout'
:
5
,
'dropin'
:
0
,
'dropout'
:
0
}
self
.
writePromise
(
**
{
'max-lost-packets-per-MB'
:
5
,
'max-error-messages-per-MB'
:
5
,
})
self
.
assertEqual
(
message
,
self
.
runPromise
(
self
.
network_data
(
**
mock_stats
)))
def
test_network_nok
(
self
):
message
=
"Network packets lost reached critical threshold: 10 (threshold is 5 per MB)"
\
"
\
n
Network errors reached critical threshold: 10 (threshold is 5 per MB)"
mock_stats
=
{
'bytes_recv'
:
0
,
'bytes_sent'
:
0
,
'errin'
:
5
,
'errout'
:
5
,
'dropin'
:
5
,
'dropout'
:
5
}
self
.
writePromise
(
**
{
'max-lost-packets-per-MB'
:
5
,
'max-error-messages-per-MB'
:
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