diff --git a/slapos/promise/plugin/check_free_disk_space.py b/slapos/promise/plugin/check_free_disk_space.py index 9f6a9ea4c4a8bb9d4db415d785d482a23ed4c18d..f4e35478a495c1a8669a25875e84e28f249545f2 100644 --- a/slapos/promise/plugin/check_free_disk_space.py +++ b/slapos/promise/plugin/check_free_disk_space.py @@ -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: diff --git a/slapos/test/promise/plugin/test_check_free_disk_space.py b/slapos/test/promise/plugin/test_check_free_disk_space.py index e5c6c787d5945b8508a205ec36fb22d39c9d4ffd..9ee041cb7ac8102b117b6f5aa06cbcf1150d8dd5 100644 --- a/slapos/test/promise/plugin/test_check_free_disk_space.py +++ b/slapos/test/promise/plugin/test_check_free_disk_space.py @@ -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()