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 ...@@ -12,7 +12,16 @@ import psutil
from slapos.collect.db import Database 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) database = Database(database, create=False, timeout=10)
try: try:
...@@ -22,32 +31,24 @@ def getFreeSpace(disk_partition, database, date, time, logger=None): ...@@ -22,32 +31,24 @@ def getFreeSpace(disk_partition, database, date, time, logger=None):
query_result = database.select("disk", date, "free", where=where_query) query_result = database.select("disk", date, "free", where=where_query)
result = zip(*query_result) result = zip(*query_result)
if not result or not result[0][0]: if not result or not result[0][0]:
if logger is not None: self.logger.info("No result from collector database: disk check skipped")
logger.info("No result from collector database: disk check skipped")
return 0 return 0
disk_free = result[0][0] disk_free = result[0][0]
except sqlite3.OperationalError, e: except sqlite3.OperationalError, e:
# if database is still locked after timeout expiration (another process is using it) # 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" locked_message = "database is locked"
if locked_message in str(e) and \ if locked_message in str(e) and \
not self.raiseOnDatabaseLocked(locked_message): not self.raiseOnDatabaseLocked(locked_message):
return 0 return 0
raise raise
finally: finally:
try:
database.close() database.close()
except Exception:
pass pass
return int(disk_free) 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): def raiseOnDatabaseLocked(self, locked_message):
max_warn = 10 max_warn = 10
latest_result_list = self.getLastPromiseResultList(result_count=max_warn) latest_result_list = self.getLastPromiseResultList(result_count=max_warn)
...@@ -72,10 +73,9 @@ class RunPromise(GenericPromise): ...@@ -72,10 +73,9 @@ class RunPromise(GenericPromise):
# too many warning on database locked, now fail. # too many warning on database locked, now fail.
return True 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 return False
def getInodeUsage(self, path): def getInodeUsage(self, path):
max_inode_usage = 97.99 # < 98% usage max_inode_usage = 97.99 # < 98% usage
stat = os.statvfs(path) stat = os.statvfs(path)
...@@ -149,8 +149,8 @@ class RunPromise(GenericPromise): ...@@ -149,8 +149,8 @@ class RunPromise(GenericPromise):
if db_path.endswith("collector.db"): if db_path.endswith("collector.db"):
db_path=db_path[:-len("collector.db")] db_path=db_path[:-len("collector.db")]
free_space = getFreeSpace(disk_partition, db_path, currentdate, free_space = self.getFreeSpace(disk_partition, db_path, currentdate,
currenttime, self.logger) currenttime)
if free_space == 0: if free_space == 0:
return return
elif free_space > min_free_size: elif free_space > min_free_size:
......
...@@ -30,7 +30,6 @@ from slapos.grid.promise import PromiseError ...@@ -30,7 +30,6 @@ from slapos.grid.promise import PromiseError
import os import os
import sqlite3 import sqlite3
from slapos.test.promise import data from slapos.test.promise import data
from slapos.promise.plugin.check_free_disk_space import getFreeSpace
from slapos.grid.promise import PromiseError from slapos.grid.promise import PromiseError
class TestCheckFreeDiskSpace(TestPromisePluginMixin): class TestCheckFreeDiskSpace(TestPromisePluginMixin):
...@@ -70,12 +69,23 @@ extra_config_dict = { ...@@ -70,12 +69,23 @@ extra_config_dict = {
if os.path.exists(self.db_file): if os.path.exists(self.db_file):
os.remove(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): 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): def test_disk_space_ok(self):
self.configureLauncher() 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