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
Nicolas Wavrant
slapos.toolbox
Commits
50707d9e
Commit
50707d9e
authored
Jul 20, 2018
by
Alain Takoudjou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
promise: import cpu load check to new promise format
parent
fe94ee71
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
0 deletions
+112
-0
slapos/promise/plugin/check_server_cpu_load.py
slapos/promise/plugin/check_server_cpu_load.py
+56
-0
slapos/test/promise/plugin/test_check_server_cpu_load.py
slapos/test/promise/plugin/test_check_server_cpu_load.py
+56
-0
No files found.
slapos/promise/plugin/check_server_cpu_load.py
0 → 100644
View file @
50707d9e
from
zope
import
interface
as
zope_interface
from
slapos.grid.promise
import
interface
from
slapos.grid.promise.generic
import
GenericPromise
import
subprocess
import
os
class
RunPromise
(
GenericPromise
):
zope_interface
.
implements
(
interface
.
IPromise
)
def
__init__
(
self
,
config
):
GenericPromise
.
__init__
(
self
,
config
)
self
.
setPeriodicity
(
minute
=
3
)
def
checkCPULoad
(
self
,
tolerance
=
2.2
):
# tolerance=1.5 => accept CPU load up to 1.5 =150%
uptime_result
=
subprocess
.
check_output
([
'uptime'
])
line
=
uptime_result
.
strip
().
split
(
' '
)
load
,
load5
,
long_load
=
line
[
-
3
:]
long_load
=
float
(
long_load
.
replace
(
','
,
'.'
))
core_count
=
int
(
subprocess
.
check_output
([
'nproc'
]).
strip
())
max_load
=
core_count
*
tolerance
if
long_load
>
max_load
:
# display top statistics
top_result
=
subprocess
.
check_output
([
'top'
,
'-n'
,
'1'
,
'-b'
])
message
=
"CPU load is high: %s %s %s
\
n
\
n
"
%
(
load
,
load5
,
long_load
)
i
=
0
result_list
=
top_result
.
split
(
'
\
n
'
)
# display first 5 lines
while
i
<
len
(
result_list
)
and
i
<
5
:
message
+=
"
\
n
%s"
%
result_list
[
i
]
i
+=
1
self
.
logger
.
error
(
message
)
else
:
self
.
logger
.
info
(
"CPU load is OK"
)
def
sense
(
self
):
load_threshold
=
self
.
getConfig
(
'cpu-load-threshold'
)
threshold
=
0
if
load_threshold
is
not
None
:
try
:
threshold
=
float
(
load_threshold
)
except
ValueError
,
e
:
self
.
logger
.
error
(
"CPU load threshold %r is not valid: %s"
%
(
load_threshold
,
e
))
return
self
.
checkCPULoad
(
threshold
or
2.2
)
def
test
(
self
):
return
self
.
_test
(
result_count
=
5
,
failure_amount
=
5
)
def
anomaly
(
self
):
return
self
.
_test
(
result_count
=
5
,
failure_amount
=
5
)
slapos/test/promise/plugin/test_check_server_cpu_load.py
0 → 100644
View file @
50707d9e
##############################################################################
#
# 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 adviced 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 3
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from
slapos.test.promise.plugin
import
TestPromisePluginMixin
from
slapos.grid.promise
import
PromiseError
import
os
class
TestCheckServerCPULoad
(
TestPromisePluginMixin
):
def
setUp
(
self
):
TestPromisePluginMixin
.
setUp
(
self
)
self
.
promise_name
=
"server-cpu-load-promise.py"
content
=
"""from slapos.promise.plugin.check_server_cpu_load import RunPromise
extra_config_dict = {
'cpu-load-threshold': '2.0',
}
"""
self
.
writePromise
(
self
.
promise_name
,
content
)
def
test_check_cpu_load_run
(
self
):
self
.
configureLauncher
()
self
.
launcher
.
run
()
result
=
self
.
getPromiseResult
(
self
.
promise_name
)
if
result
[
'result'
][
'failed'
]:
self
.
assertTrue
(
"CPU load is high"
in
result
[
'result'
][
'message'
])
else
:
self
.
assertEquals
(
"CPU load is OK"
,
result
[
'result'
][
'message'
].
strip
())
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