Commit dd6450bd authored by Hardik Juneja's avatar Hardik Juneja Committed by Julien Muchembled

slapos/promise: Improve apachedex and slowquery promises

/reviewed-on !21
parent ed17ad18
...@@ -12,54 +12,53 @@ import time ...@@ -12,54 +12,53 @@ import time
import datetime import datetime
import argparse import argparse
def checkApachedexResult(apachedex_file, apachedex_report_status_file, desired_threshold): def checkApachedexResult(apachedex_path, apachedex_report_status_file, desired_threshold):
message = "No apachedex result for today or yesterday"
if not os.path.isfile(apachedex_file): today = datetime.date.today()
open(apachedex_file, 'a').close() today_or_yesterday = today, today - datetime.timedelta(1)
try:
apachedex_file = max(os.listdir(apachedex_path),
key=lambda x: os.stat(os.path.join(apachedex_path, x)).st_mtime)
except ValueError:
if datetime.date.fromtimestamp(os.stat(apachedex_path).st_mtime) in today_or_yesterday:
return 0, "Instance has been just deployed. Skipping check.."
else:
for date in today_or_yesterday:
if apachedex_file == date.strftime('ApacheDex-%Y-%m-%d.html'):
with open(apachedex_file, 'r') as content_file: with open(os.path.join(apachedex_path, apachedex_file)) as f:
content = content_file.read() content = f.read()
if content:
if len(content) == 0: # XXX: if not a lot of usage, skip
# File is empty # XXX: too fragile, use lxml.html and use xpath
# Check the creation date of the file regex = r"Overall<\/h2>.*\n<th>apdex<\/th><th>.*?\n<\/tr><tr>\n<td [^<]*>(.*?)%<\/td>"
# and if the date is greater than 30 hour throw an error m = re.findall(regex, content)
date_created = os.path.getmtime(apachedex_file) if m:
current_date = time.mktime(datetime.datetime.now().timetuple()) apx_result=int(m[0])
if current_date - date_created > 108000: if apx_result > desired_threshold:
with open(apachedex_report_status_file) as f: return 0, "Thanks for keeping it all clean, result is " + str(apx_result)
json_content = f.read() else:
return 1, "Threshold is lower than expected: Expected was " + \
str(desired_threshold) +" and current result is " + str(apx_result)
message = "No result found in the apdex file or the file is corrupted"
break
# Print the message from the monitor report with open(apachedex_report_status_file) as f:
if len(json_content) > 0: try:
message = json.loads(json_content)["message"] json_content = json.load(f)
return message + "\nFile modification date is greater than 30 hours" except ValueError, e:
return "File modification date is greater than 30 hour" json_content = ''
else: if json_content:
#TODO: this is old regex for bash, improve it message += "\n" + json_content["message"]
regex = r"Overall<\/h2>.*\n<th>apdex<\/th><th>.*?\n<\/tr><tr>\n<td [^<]*>(.*?)%<\/td>" return 1, message
m = re.findall(regex, content)
if len(m) > 0:
result=int(m[0])
if result > desired_threshold:
return "Thanks for keeping it all clean, result is " + str(result)
else:
return "Threshold is lower than expected: Expected was " + \
str(desired_threshold) +" and current result is " + str(result)
return "No result found in the apdex file or the file is corrupted"
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--apachedex_file", required=True) parser.add_argument("--apachedex_path", required=True)
parser.add_argument("--status_file", required=True) parser.add_argument("--status_file", required=True)
parser.add_argument("--threshold", required=True) parser.add_argument("--threshold", required=True)
args = parser.parse_args() args = parser.parse_args()
if args.apachedex_file: status, message = checkApachedexResult(args.apachedex_path, args.status_file, args.threshold)
args.apachedex_file = args.apachedex_file + "/ApacheDex-" + datetime.date.today().strftime('%Y-%m-%d') + ".html" print(message)
sys.exit(status)
result = checkApachedexResult(args.apachedex_file, args.status_file, args.threshold)
print result
if result != "OK":
sys.exit(1)
...@@ -12,62 +12,67 @@ import time ...@@ -12,62 +12,67 @@ import time
import datetime import datetime
import argparse import argparse
def checkMariadbDigestResult(mariadbdex_file, mariadbdex_report_status_file, def checkMariadbDigestResult(mariadbdex_path, mariadbdex_report_status_file,
max_query_threshold, slowest_query_threshold): max_query_threshold, slowest_query_threshold):
message = "No mariadbdex result for today or yesterday"
today = datetime.date.today()
today_or_yesterday = today, today - datetime.timedelta(1)
if not os.path.isfile(mariadbdex_file): try:
open(mariadbdex_file, 'a').close() mariadbdex_file = max(os.listdir(mariadbdex_path),
key=lambda x: os.stat(os.path.join(mariadbdex_path, x)).st_mtime)
with open(mariadbdex_file, 'r') as content_file: except ValueError:
content = content_file.read() if datetime.date.fromtimestamp(os.stat(mariadbdex_path).st_mtime) in today_or_yesterday:
return 0, "Instance has been just deployed. Skipping check.."
if len(content) == 0:
# File is empty
# Check the creation date of the file
# and if the date is greater than 30 hour throw an error
date_created = os.path.getmtime(mariadbdex_file)
current_date = time.mktime(datetime.datetime.now().timetuple())
if current_date - date_created > 108000:
with open(mariadbdex_report_status_file) as f:
json_content = f.read()
# Print the message from the monitor report
if len(json_content) > 0:
message = json.loads(json_content)["message"]
return message + "\nFile modification date is greater than 30 hours"
return "File modification date is greater than 30 hour"
else: else:
regex = r"Overall: (.*) total,[\S\s]*# Exec time( *([\d]+)m?s?){4}" for date in today_or_yesterday:
m = re.findall(regex, content) if mariadbdex_file == date.strftime('slowquery_digest.txt-%Y-%m-%d'):
if len(m) > 0: with open(os.path.join(mariadbdex_path, mariadbdex_file)) as f:
total_queries_exec=m[0][0].strip() content = f.read()
slowest_query_time=int(m[0][2].strip()) if content:
has_k=total_queries_exec[-1:] # XXX: if not a lot of usage, skip this
if has_k == "k": regex = r"Overall: (.*) total,[\S\s]*# Exec time( *([\d]+)m?s?){4}"
pre=total_queries_exec[:-1] m = re.findall(regex, content)
total_queries_exec=float(pre)*1000 if m:
else: total_queries_exec=m[0][0].strip()
total_queries_exec=int(total_queries_exec) slowest_query_time=int(m[0][2].strip())
has_k=total_queries_exec[-1:]
if has_k == "k":
pre=total_queries_exec[:-1]
total_queries_exec=float(pre)*1000
else:
total_queries_exec=int(total_queries_exec)
if total_queries_exec < max_query_threshold and slowest_query_time < slowest_query_threshold:
return 0, "Thanks for keeping it all clean, total queries are : " + str(total_queries_exec) + \
" slowest query time is : " + str(slowest_query_time)
else:
return 1, "Threshold is lower than expected: \nExpected total queries : " + \
str(max_query_threshold) +" and current is: " + str(total_queries_exec) + "\n"+ \
"Expected slowest query : " + str(slowest_query_threshold) + " and current is: " + \
str(slowest_query_time)
message = "No result found in the slow query digest file or the file is corrupted"
break
if total_queries_exec < max_query_threshold and slowest_query_time < slowest_query_threshold: with open(mariadbdex_report_status_file) as f:
return "Thanks for keeping it all clean, total queries are : " + str(total_queries_exec) + \ try:
" slowest query time is : " + str(slowest_query_time) json_content = json.load(f)
else: except ValueError, e:
return "Threshold is lower than expected: \nExpected total queries : " + \ json_content = ''
str(max_query_threshold) +" and current is: " + str(total_queries_exec) + "\n"+ \ if json_content:
"Expected slowest query : " + str(slowest_query_threshold) + " and current is: " + str(slowest_query_time) message += "\n" + json_content["message"]
return "No result found in the apdex file or the file is corrupted" return 1, message
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--ptdigest_file", required=True) parser.add_argument("--ptdigest_path", required=True)
parser.add_argument("--status_file", required=True) parser.add_argument("--status_file", required=True)
parser.add_argument("--max_queries_threshold", required=True) parser.add_argument("--max_queries_threshold", required=True)
parser.add_argument("--slowest_query_threshold", required=True) parser.add_argument("--slowest_query_threshold", required=True)
args = parser.parse_args() args = parser.parse_args()
result = checkMariadbDigestResult(args.ptdigest_file, args.status_file, args.max_queries_threshold, args.slowest_query_threshold) status, message = checkMariadbDigestResult(args.ptdigest_path, args.status_file,
args.max_queries_threshold, args.slowest_query_threshold
)
print result print message
if result != "OK": sys.exit(status)
sys.exit(1)
...@@ -29,37 +29,77 @@ import unittest ...@@ -29,37 +29,77 @@ import unittest
import os import os
import time import time
import tempfile import tempfile
import datetime
import shutil
from slapos.test.promise import data from slapos.test.promise import data
from slapos.promise.check_apachedex_result import checkApachedexResult from slapos.promise.check_apachedex_result import checkApachedexResult
class TestCheckApacheDigestResult(unittest.TestCase): class TestCheckApacheDigestResult(unittest.TestCase):
def _create_file(self, date, with_content):
content = ''
if with_content:
with open(self.base_path + "/apachedex.html") as f:
content = f.read()
name = date.strftime('ApacheDex-%Y-%m-%d.html')
oldtime = time.mktime(date.timetuple()) + 2000
with open( self.base_dir+name, 'a') as the_file:
the_file.write(content)
os.utime(self.base_dir+name, ( oldtime , oldtime ))
def _remove_file(self, date):
name = date.strftime('ApacheDex-%Y-%m-%d.html')
os.remove(self.base_dir+name)
def setUp(self): def setUp(self):
_, self.empty_apachedex_file = tempfile.mkstemp() self.base_path = "/".join(data.__file__.split("/")[:-1])
_, self.old_apachedex_file = tempfile.mkstemp() self.base_dir = "/tmp/ap/"
if not os.path.exists(self.base_dir):
os.makedirs(self.base_dir)
os.utime(self.base_dir, (time.time() - 202800, time.time() - 202800))
# create test files
self.today = datetime.date.today()
self._create_file(self.today, True)
self.yesterday = (self.today - datetime.timedelta(1))
self._create_file(self.yesterday, False)
_, self.status_file = tempfile.mkstemp() _, self.status_file = tempfile.mkstemp()
self.base_path = "/".join(data.__file__.split("/")[:-1]) def test_threshold_is_greater(self):
self.status = "ok" status, message = checkApachedexResult(self.base_dir, self.status_file, 60)
self.assertEquals("Thanks for keeping it all clean, result is 80", message)
self.assertEquals(0, status)
def test_pass(self): def test_no_today_file_and_empty_yesterday_file(self):
self.assertEquals("Thanks for keeping it all clean, result is 80", self._remove_file(self.today)
checkApachedexResult(self.base_path + "/apachedex.html", self.status_file, 60)) status, message = checkApachedexResult(self.base_dir, self.status_file, 60)
self.assertEquals("No result found in the apdex file or the file is corrupted", message)
self.assertEquals(1, status)
self._create_file(self.today, True)
def test_empty_file(self): def test_threshold_is_lower(self):
self.assertEquals("No result found in the apdex file or the file is corrupted", status, message = checkApachedexResult(self.base_dir, self.status_file, 90)
checkApachedexResult(self.empty_apachedex_file, self.status_file, 60)) self.assertEquals("Threshold is lower than expected: Expected was 90 and current result is 80", message)
self.assertEquals(1, status)
def test_fail(self): def test_no_today_file_but_yesterday_file(self):
self.assertEquals("Threshold is lower than expected: Expected was 90 and current result is 80", self._remove_file(self.today)
checkApachedexResult(self.base_path + "/apachedex.html", self.status_file, 90)) self._create_file(self.yesterday, True)
status, message = checkApachedexResult(self.base_dir, self.status_file, 60)
self.assertEquals("Thanks for keeping it all clean, result is 80", message)
self.assertEquals(0, status)
self._create_file(self.today, True)
self._remove_file(self.yesterday)
self._create_file(self.yesterday, False)
def test_old_file(self): def tearDown(self):
os.utime(self.old_apachedex_file, (time.time() - 202800, time.time() - 202800)) self._remove_file(self.today)
self.assertEquals("File modification date is greater than 30 hour", self._remove_file(self.yesterday)
checkApachedexResult(self.old_apachedex_file, self.status_file, 60)) shutil.rmtree('/tmp/ap')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
...@@ -29,36 +29,77 @@ import unittest ...@@ -29,36 +29,77 @@ import unittest
import os import os
import time import time
import tempfile import tempfile
import datetime
import shutil
from slapos.test.promise import data from slapos.test.promise import data
from slapos.promise.check_slow_queries_digest_result import checkMariadbDigestResult from slapos.promise.check_slow_queries_digest_result import checkMariadbDigestResult
class TestCheckSlowQueriesDigestResult(unittest.TestCase): class TestCheckSlowQueriesDigestResult(unittest.TestCase):
def setUp(self): def _create_file(self, date, with_content):
content = ''
if with_content:
with open(self.base_path + "/ptdigest.html") as f:
content = f.read()
_, self.empty_ptdigest_file = tempfile.mkstemp() name = date.strftime('slowquery_digest.txt-%Y-%m-%d')
_, self.old_ptdigest_file = tempfile.mkstemp() oldtime = time.mktime(date.timetuple()) + 2000
_, self.status_file = tempfile.mkstemp() with open( self.base_dir+name, 'a') as the_file:
the_file.write(content)
os.utime(self.base_dir+name, ( oldtime , oldtime ))
def _remove_file(self, date):
name = date.strftime('slowquery_digest.txt-%Y-%m-%d')
os.remove(self.base_dir+name)
def setUp(self):
self.base_path = "/".join(data.__file__.split("/")[:-1]) self.base_path = "/".join(data.__file__.split("/")[:-1])
self.base_dir = "/tmp/ap/"
if not os.path.exists(self.base_dir):
os.makedirs(self.base_dir)
os.utime(self.base_dir, (time.time() - 202800, time.time() - 202800))
# create test files
self.today = datetime.date.today()
self._create_file(self.today, True)
self.yesterday = (self.today - datetime.timedelta(1))
self._create_file(self.yesterday, False)
_, self.status_file = tempfile.mkstemp()
def test_pass(self): def test_threshold_is_greater(self):
self.assertEquals("Thanks for keeping it all clean, total queries are : 3420.0 slowest query time is : 34", status, message = checkMariadbDigestResult(self.base_dir, self.status_file, 5000, 100)
checkMariadbDigestResult(self.base_path + "/ptdigest.html", self.status_file, 5000, 100)) self.assertEquals("Thanks for keeping it all clean, total queries are : 3420.0 slowest query time is : 34", message)
self.assertEquals(0, status)
def test_empty_file(self): def test_no_today_file_and_empty_yesterday_file(self):
self.assertEquals("No result found in the apdex file or the file is corrupted", self._remove_file(self.today)
checkMariadbDigestResult(self.empty_ptdigest_file, self.status_file, 60, 100)) status, message = checkMariadbDigestResult(self.base_dir, self.status_file, 60, 100)
self.assertEquals("No result found in the slow query digest file or the file is corrupted", message)
self.assertEquals(1, status)
self._create_file(self.today, True)
def test_fail(self): def test_fail(self):
self.assertEquals("Threshold is lower than expected: \nExpected total queries : 90 and current is: 3420.0\nExpected slowest query : 100 and current is: 34", status, message = checkMariadbDigestResult(self.base_dir, self.status_file, 90, 100)
checkMariadbDigestResult(self.base_path + "/ptdigest.html", self.status_file, 90, 100)) self.assertEquals("Threshold is lower than expected: \nExpected total queries : 90 and current is: 3420.0\nExpected slowest query : 100 and current is: 34", message)
self.assertEquals(1, status)
def test_old_file(self): def test_no_today_file_but_yesterday_file(self):
os.utime(self.old_ptdigest_file, (time.time() - 202800, time.time() - 202800)) self._remove_file(self.today)
self.assertEquals("File modification date is greater than 30 hour", self._create_file(self.yesterday, True)
checkMariadbDigestResult(self.old_ptdigest_file, self.status_file, 60, 100)) status, message = checkMariadbDigestResult(self.base_dir, self.status_file, 5000, 100)
self.assertEquals("Thanks for keeping it all clean, total queries are : 3420.0 slowest query time is : 34", message)
self.assertEquals(0, status)
self._create_file(self.today, True)
self._remove_file(self.yesterday)
self._create_file(self.yesterday, False)
def tearDown(self):
self._remove_file(self.today)
self._remove_file(self.yesterday)
shutil.rmtree('/tmp/ap')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
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