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
Klaus Wölfel
slapos.toolbox
Commits
2fd4b63a
Commit
2fd4b63a
authored
Oct 04, 2017
by
Hardik Juneja
Committed by
Rafael Monnerat
Oct 24, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
slapos/promise: move free disk promise and use collector db for calculation of free disk
/reviewed-on
nexedi/slapos.toolbox!20
parent
38c133c1
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
202 additions
and
0 deletions
+202
-0
setup.py
setup.py
+1
-0
slapos/promise/check_free_disk/__init__.py
slapos/promise/check_free_disk/__init__.py
+122
-0
slapos/test/promise/data/disktest.sql
slapos/test/promise/data/disktest.sql
+17
-0
slapos/test/promise/test_check_free_disk.py
slapos/test/promise/test_check_free_disk.py
+62
-0
No files found.
setup.py
View file @
2fd4b63a
...
...
@@ -71,6 +71,7 @@ setup(name=name,
'agent = slapos.agent.agent:main'
,
'apache-mpm-watchdog = slapos.promise.apache_mpm_watchdog:main'
,
'check-computer-memory = slapos.promise.check_computer_memory:main'
,
'check-free-disk = slapos.promise.check_free_disk:main'
,
'check-web-page-http-cache-hit = slapos.promise.check_web_page_http_cache_hit:main'
,
'check-feed-as-promise = slapos.checkfeedaspromise:main'
,
'check-error-on-apache-log = slapos.promise.check_error_on_apache_log:main'
,
...
...
slapos/promise/check_free_disk/__init__.py
0 → 100644
View file @
2fd4b63a
#!/usr/bin/env python
"""
Check if free disk space is less than given threshold.
"""
import
sys
import
os
import
sqlite3
import
argparse
import
datetime
import
psutil
from
slapos.collect.db
import
Database
def
getFreeSpace
(
disk_partition
,
database
,
date
,
time
):
database
=
Database
(
database
)
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
]:
print
"No result from collector database: disk check skipped"
return
0
disk_free
=
result
[
0
][
0
]
finally
:
database
.
close
()
return
int
(
disk_free
)
def
getInodeUsage
(
path
):
max_inode_usage
=
97.99
# < 98% usage
stat
=
os
.
statvfs
(
path
)
usage_output
=
""
total_inode
=
stat
.
f_files
free_inode
=
stat
.
f_ffree
usage
=
round
((
float
(
total_inode
-
free_inode
)
/
total_inode
),
4
)
*
100
if
usage
>
max_inode_usage
:
return
"Disk Inodes usages is really high: %s%%"
%
usage
elif
os
.
path
.
exists
(
'/tmp'
):
# check if /tmp is mounted on another disk than path
tmp_stat
=
os
.
statvfs
(
'/tmp'
)
if
tmp_stat
.
f_blocks
!=
stat
.
f_blocks
:
tmp_usage
=
round
((
float
(
tmp_stat
.
f_files
-
tmp_stat
.
f_ffree
)
/
tmp_stat
.
f_files
),
4
)
*
100
if
tmp_usage
>
max_inode_usage
:
return
"Disk Inodes usage is high: %s%%"
%
tmp_usage
return
""
def
main
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--collectordb"
,
required
=
True
)
parser
.
add_argument
(
"--home_path"
,
required
=
True
)
parser
.
add_argument
(
"--config"
,
required
=
True
)
args
=
parser
.
parse_args
()
# find if a disk is mounted on the path
disk_partition
=
""
path
=
os
.
path
.
join
(
args
.
home_path
,
""
)
+
"extrafolder"
partitions
=
psutil
.
disk_partitions
()
while
path
is
not
'/'
:
if
not
disk_partition
:
path
=
os
.
path
.
dirname
(
path
)
else
:
break
for
p
in
partitions
:
if
p
.
mountpoint
is
path
:
disk_partition
=
p
.
device
break
if
not
disk_partition
:
print
"Couldn't find disk partition"
exit
(
2
)
min_free_size
=
1024
*
1024
*
1024
*
2
# 2G by default
if
os
.
path
.
exists
(
args
.
config
):
with
open
(
args
.
config
)
as
f
:
min_size_str
=
f
.
read
().
strip
()
if
min_size_str
==
'0'
:
# disable check
print
"Free disk space check is disabled
\
n
set a number up to 0 to enable!"
exit
(
0
)
if
min_size_str
.
isdigit
():
value
=
int
(
min_size_str
)
if
value
>=
200
:
# Minimum value is 200Mb, it's already low
min_free_size
=
int
(
min_size_str
)
*
1024
*
1024
else
:
with
open
(
args
.
config
,
'w'
)
as
f
:
f
.
write
(
str
(
min_free_size
/
(
1024
*
1024
)))
# get last minute
now
=
datetime
.
datetime
.
now
()
currentdate
=
now
.
strftime
(
'%Y-%m-%d'
)
currenttime
=
now
-
datetime
.
timedelta
(
minutes
=
1
)
currenttime
=
currenttime
.
time
().
strftime
(
'%H:%M'
)
db_path
=
args
.
collectordb
if
db_path
.
endswith
(
"collector.db"
):
db_path
=
db_path
[:
-
len
(
"collector.db"
)]
free_space
=
getFreeSpace
(
disk_partition
,
db_path
,
currentdate
,
currenttime
)
if
free_space
>
min_free_size
:
inode_usage
=
getInodeUsage
(
args
.
home_path
)
if
inode_usage
:
print
inode_usage
exit
(
2
)
print
"Disk usage: OK"
exit
(
0
)
free_space
=
round
(
free_space
/
(
1024.0
*
1024
*
1024
),
2
)
min_space
=
round
(
min_free_size
/
(
1024.0
*
1024
*
1024
),
2
)
print
'Free disk space low: remaning %s G (threshold: %s G)'
%
(
free_space
,
min_space
)
print
'Please modify minimum value in your monitor interface.'
exit
(
1
)
if
__name__
==
"__main__"
:
sys
.
exit
(
main
())
slapos/test/promise/data/disktest.sql
0 → 100644
View file @
2fd4b63a
BEGIN
TRANSACTION
;
CREATE
TABLE
disk
(
partition
text
,
used
text
,
free
text
,
mountpoint
text
,
date
text
,
time
text
,
reported
integer
NULL
DEFAULT
0
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159220666368'
,
'288948396032'
,
'/'
,
'2017-10-02'
,
'09:17:01'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159237537792'
,
'288931524608'
,
'/'
,
'2017-10-02'
,
'09:18:01'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159238090752'
,
'288930971648'
,
'/'
,
'2017-10-02'
,
'09:19:02'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159241568256'
,
'288927494144'
,
'/'
,
'2017-10-02'
,
'09:20:02'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159242719232'
,
'288926343168'
,
'/'
,
'2017-10-02'
,
'09:21:02'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159196176384'
,
'288972886016'
,
'/'
,
'2017-10-02'
,
'09:22:03'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159300747264'
,
'288868315136'
,
'/'
,
'2017-10-02'
,
'09:23:03'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159294308352'
,
'288874754048'
,
'/'
,
'2017-10-02'
,
'09:24:02'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159328468992'
,
'288840593408'
,
'/'
,
'2017-10-02'
,
'09:25:03'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159384883200'
,
'288784179200'
,
'/'
,
'2017-10-02'
,
'09:26:03'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159429677056'
,
'288739385344'
,
'/'
,
'2017-10-02'
,
'09:27:02'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159444549632'
,
'288724512768'
,
'/'
,
'2017-10-02'
,
'09:28:03'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159472902144'
,
'288696160256'
,
'/'
,
'2017-10-02'
,
'09:29:02'
,
1
);
INSERT
INTO
"disk"
VALUES
(
'/dev/sda1'
,
'159476805632'
,
'288692256768'
,
'/'
,
'2017-10-02'
,
'09:30:03'
,
1
);
COMMIT
;
slapos/test/promise/test_check_free_disk.py
0 → 100644
View file @
2fd4b63a
##############################################################################
#
# Copyright (c) 2017 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.
#
##############################################################################
import
unittest
import
os
import
sqlite3
from
slapos.test.promise
import
data
from
slapos.promise.check_free_disk
import
getFreeSpace
class
TestFreeDisk
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
base_path
=
"/"
.
join
(
data
.
__file__
.
split
(
"/"
)[:
-
1
])
self
.
status
=
"ok"
self
.
db_file
=
'/tmp/collector.db'
# populate db
self
.
conn
=
sqlite3
.
connect
(
self
.
db_file
)
f
=
open
(
self
.
base_path
+
"/disktest.sql"
)
sql
=
f
.
read
()
self
.
conn
.
executescript
(
sql
)
self
.
conn
.
close
()
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'
))
def
tearDown
(
self
):
if
os
.
path
.
exists
(
self
.
db_file
):
os
.
remove
(
self
.
db_file
)
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