Commit bd09f20e authored by Tristan Cavelier's avatar Tristan Cavelier

check_computer_memory: be more consistent with getFreeMemory return value

avoid to return exit code (int) or mem (object)
better return a tuple (mem, error_message) (object, str)
parent ff80a1a4
...@@ -23,8 +23,7 @@ def getFreeMemory(database, time, date): ...@@ -23,8 +23,7 @@ def getFreeMemory(database, time, date):
query_result = database.select("computer", date, "memory_size", limit=1) query_result = database.select("computer", date, "memory_size", limit=1)
result = zip(*query_result) result = zip(*query_result)
if not result or not result[0][0]: if not result or not result[0][0]:
print "couldn't fetch total memory, collectordb is empty?" return (None, "couldn't fetch total memory, collectordb is empty?")
return 0
mem['total'] = result[0][0] mem['total'] = result[0][0]
# fetch free and used memory # fetch free and used memory
...@@ -32,17 +31,15 @@ def getFreeMemory(database, time, date): ...@@ -32,17 +31,15 @@ def getFreeMemory(database, time, date):
query_result = database.select("system", date, "memory_free, memory_used", where=where_query) query_result = database.select("system", date, "memory_free, memory_used", 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]:
print "couldn't fetch free memory" return (None, "couldn't fetch free memory")
return 0
mem['free'] = result[0][0] mem['free'] = result[0][0]
if not result or not result[1][0]: if not result or not result[1][0]:
print "couldn't fetch used memory" return (None, "couldn't fetch used memory")
return 0
mem['used'] = result[1][0] mem['used'] = result[1][0]
finally: finally:
database.close() database.close()
return mem return (mem, "")
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
...@@ -58,11 +55,12 @@ def main(): ...@@ -58,11 +55,12 @@ def main():
db_path = args.collectordb db_path = args.collectordb
if db_path.endswith("collector.db"):db_path=db_path[:-len("collector.db")] if db_path.endswith("collector.db"):db_path=db_path[:-len("collector.db")]
memory = getFreeMemory(db_path, currenttime, currentdate) memory, error = getFreeMemory(db_path, currenttime, currentdate)
if error:
print error
return 0
threshold = float(memory['total']) * 0.2 threshold = float(memory['total']) * 0.2
if memory is 0:
return 0
if memory['used'] <= threshold: if memory['used'] <= threshold:
print "OK - memory used: {:d}% ({}B of {}B)".format(memory["used"] * 100 / memory["total"], memory["used"], memory["total"]) print "OK - memory used: {:d}% ({}B of {}B)".format(memory["used"] * 100 / memory["total"], memory["used"], memory["total"])
return 0 return 0
......
...@@ -32,6 +32,8 @@ import sqlite3 ...@@ -32,6 +32,8 @@ import sqlite3
from slapos.test.promise import data from slapos.test.promise import data
from slapos.promise.check_computer_memory import getFreeMemory from slapos.promise.check_computer_memory import getFreeMemory
total_memory_fetch_failure_message = "couldn't fetch total memory, collectordb is empty?"
class TestComputerMemory(unittest.TestCase): class TestComputerMemory(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -47,11 +49,14 @@ class TestComputerMemory(unittest.TestCase): ...@@ -47,11 +49,14 @@ class TestComputerMemory(unittest.TestCase):
self.conn.close() self.conn.close()
def test_check_memory(self): def test_check_memory(self):
self.assertEquals({'total': 33705312256.0, 'used': 33139023872.0, 'free': 566288384.0}, self.assertEquals(({'total': 33705312256.0, 'used': 33139023872.0, 'free': 566288384.0}, ""),
getFreeMemory('/tmp', '00:02', '2017-09-15')) getFreeMemory('/tmp', '00:02', '2017-09-15'))
def test_check_memory_with_unavailable_dates(self): def test_check_memory_with_unavailable_dates(self):
self.assertEquals(0, getFreeMemory('/tmp', '18:00', '2017-09-14')) self.assertEquals(
(None, total_memory_fetch_failure_message),
getFreeMemory('/tmp', '18:00', '2017-09-14'),
)
def tearDown(self): def tearDown(self):
if os.path.exists(self.db_file): if os.path.exists(self.db_file):
......
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