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):
query_result = database.select("computer", date, "memory_size", limit=1)
result = zip(*query_result)
if not result or not result[0][0]:
print "couldn't fetch total memory, collectordb is empty?"
return 0
return (None, "couldn't fetch total memory, collectordb is empty?")
mem['total'] = result[0][0]
# fetch free and used memory
......@@ -32,17 +31,15 @@ def getFreeMemory(database, time, date):
query_result = database.select("system", date, "memory_free, memory_used", where=where_query)
result = zip(*query_result)
if not result or not result[0][0]:
print "couldn't fetch free memory"
return 0
return (None, "couldn't fetch free memory")
mem['free'] = result[0][0]
if not result or not result[1][0]:
print "couldn't fetch used memory"
return 0
return (None, "couldn't fetch used memory")
mem['used'] = result[1][0]
finally:
database.close()
return mem
return (mem, "")
def main():
parser = argparse.ArgumentParser()
......@@ -58,11 +55,12 @@ def main():
db_path = args.collectordb
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
if memory is 0:
return 0
if memory['used'] <= threshold:
print "OK - memory used: {:d}% ({}B of {}B)".format(memory["used"] * 100 / memory["total"], memory["used"], memory["total"])
return 0
......
......@@ -32,6 +32,8 @@ import sqlite3
from slapos.test.promise import data
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):
def setUp(self):
......@@ -47,11 +49,14 @@ class TestComputerMemory(unittest.TestCase):
self.conn.close()
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'))
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):
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