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
7
Merge Requests
7
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
05872cb9
Commit
05872cb9
authored
Aug 29, 2018
by
Alain Takoudjou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixup on move check_free_disk_space promise to promise.plugin
parent
dc9c4d0e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
37 deletions
+47
-37
slapos/promise/plugin/check_free_disk_space.py
slapos/promise/plugin/check_free_disk_space.py
+31
-31
slapos/test/promise/plugin/test_check_free_disk_space.py
slapos/test/promise/plugin/test_check_free_disk_space.py
+16
-6
No files found.
slapos/promise/plugin/check_free_disk_space.py
View file @
05872cb9
...
...
@@ -12,33 +12,6 @@ import psutil
from
slapos.collect.db
import
Database
def
getFreeSpace
(
disk_partition
,
database
,
date
,
time
,
logger
=
None
):
database
=
Database
(
database
,
create
=
False
,
timeout
=
10
)
try
:
# fetch free disk space
database
.
connect
()
where_query
=
"time between '%s:00' and '%s:30' and partition='%s'"
%
(
time
,
time
,
disk_partition
)
query_result
=
database
.
select
(
"disk"
,
date
,
"free"
,
where
=
where_query
)
result
=
zip
(
*
query_result
)
if
not
result
or
not
result
[
0
][
0
]:
if
logger
is
not
None
:
logger
.
info
(
"No result from collector database: disk check skipped"
)
return
0
disk_free
=
result
[
0
][
0
]
except
sqlite3
.
OperationalError
,
e
:
# if database is still locked after timeout expiration (another process is using it)
# we print warning message and try the promise at next run until max warn msg
locked_message
=
"database is locked"
if
locked_message
in
str
(
e
)
and
\
not
self
.
raiseOnDatabaseLocked
(
locked_message
):
return
0
raise
finally
:
database
.
close
()
pass
return
int
(
disk_free
)
class
RunPromise
(
GenericPromise
):
zope_interface
.
implements
(
interface
.
IPromise
)
...
...
@@ -48,6 +21,34 @@ class RunPromise(GenericPromise):
# check disk space at least every 3 minutes
self
.
setPeriodicity
(
minute
=
3
)
def
getFreeSpace
(
self
,
disk_partition
,
database
,
date
,
time
):
database
=
Database
(
database
,
create
=
False
,
timeout
=
10
)
try
:
# fetch free disk space
database
.
connect
()
where_query
=
"time between '%s:00' and '%s:30' and partition='%s'"
%
(
time
,
time
,
disk_partition
)
query_result
=
database
.
select
(
"disk"
,
date
,
"free"
,
where
=
where_query
)
result
=
zip
(
*
query_result
)
if
not
result
or
not
result
[
0
][
0
]:
self
.
logger
.
info
(
"No result from collector database: disk check skipped"
)
return
0
disk_free
=
result
[
0
][
0
]
except
sqlite3
.
OperationalError
,
e
:
# if database is still locked after timeout expiration (another process is using it)
# we print warning message and try the promise at next run until max warn count
locked_message
=
"database is locked"
if
locked_message
in
str
(
e
)
and
\
not
self
.
raiseOnDatabaseLocked
(
locked_message
):
return
0
raise
finally
:
try
:
database
.
close
()
except
Exception
:
pass
return
int
(
disk_free
)
def
raiseOnDatabaseLocked
(
self
,
locked_message
):
max_warn
=
10
latest_result_list
=
self
.
getLastPromiseResultList
(
result_count
=
max_warn
)
...
...
@@ -72,10 +73,9 @@ class RunPromise(GenericPromise):
# too many warning on database locked, now fail.
return
True
self
.
logger
.
warn
(
"collector database is locked by another process
: %s %s"
%
(
warning_count
,
len
(
latest_result_list
))
)
self
.
logger
.
warn
(
"collector database is locked by another process
"
)
return
False
def
getInodeUsage
(
self
,
path
):
max_inode_usage
=
97.99
# < 98% usage
stat
=
os
.
statvfs
(
path
)
...
...
@@ -149,8 +149,8 @@ class RunPromise(GenericPromise):
if
db_path
.
endswith
(
"collector.db"
):
db_path
=
db_path
[:
-
len
(
"collector.db"
)]
free_space
=
getFreeSpace
(
disk_partition
,
db_path
,
currentdate
,
currenttime
,
self
.
logger
)
free_space
=
self
.
getFreeSpace
(
disk_partition
,
db_path
,
currentdate
,
currenttime
)
if
free_space
==
0
:
return
elif
free_space
>
min_free_size
:
...
...
slapos/test/promise/plugin/test_check_free_disk_space.py
View file @
05872cb9
...
...
@@ -30,7 +30,6 @@ from slapos.grid.promise import PromiseError
import
os
import
sqlite3
from
slapos.test.promise
import
data
from
slapos.promise.plugin.check_free_disk_space
import
getFreeSpace
from
slapos.grid.promise
import
PromiseError
class
TestCheckFreeDiskSpace
(
TestPromisePluginMixin
):
...
...
@@ -70,12 +69,23 @@ extra_config_dict = {
if
os
.
path
.
exists
(
self
.
db_file
):
os
.
remove
(
self
.
db_file
)
def
test_check_disk
(
self
):
self
.
assertEquals
(
288739385344
,
getFreeSpace
(
'/dev/sda1'
,
'/tmp'
,
'2017-10-02'
,
'09:27'
))
def
test_check_free_disk_with_unavailable_dates
(
self
):
self
.
assertEquals
(
0
,
getFreeSpace
(
'/'
,
'/tmp'
,
'18:00'
,
'2017-09-14'
))
content
=
"""from slapos.promise.plugin.check_free_disk_space import RunPromise
extra_config_dict = {
'collectordb': '%(collectordb)s',
'threshold-file': '%(th_file)s',
'test-check-date': '2017-09-14',
'test-check-time': '18:00'
}
"""
%
{
'collectordb'
:
self
.
db_file
,
'th_file'
:
self
.
th_file
}
self
.
writePromise
(
self
.
promise_name
,
content
)
self
.
configureLauncher
()
self
.
launcher
.
run
()
result
=
self
.
getPromiseResult
(
self
.
promise_name
)
self
.
assertEquals
(
result
[
'result'
][
'failed'
],
False
)
self
.
assertEquals
(
result
[
'result'
][
'message'
],
"No result from collector database: disk check skipped"
)
def
test_disk_space_ok
(
self
):
self
.
configureLauncher
()
...
...
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