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

slapos/promise: Improve apachedex and slowquery promises

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