Commit 05872cb9 authored by Alain Takoudjou's avatar Alain Takoudjou

fixup on move check_free_disk_space promise to promise.plugin

parent dc9c4d0e
......@@ -12,7 +12,16 @@ import psutil
from slapos.collect.db import Database
def getFreeSpace(disk_partition, database, date, time, logger=None):
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# 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:
......@@ -22,32 +31,24 @@ def getFreeSpace(disk_partition, database, date, time, logger=None):
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")
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 msg
# 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)
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# check disk space at least every 3 minutes
self.setPeriodicity(minute=3)
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:
......
......@@ -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()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment