Commit 24b0d926 authored by Łukasz Nowak's avatar Łukasz Nowak

promise: Improve reporting in check_surykatka_json

Prefix with ERROR and OK messages for problems and ok checks, as they are
grouped together in the output.
parent ded03013
......@@ -27,10 +27,10 @@ class RunPromise(GenericPromise):
self.error_list = []
self.info_list = []
def appendError(self, message):
def appendErrorMessage(self, message):
self.error_list.append(message)
def appendInfo(self, message):
def appendInfoMessage(self, message):
self.info_list.append(message)
def emitLog(self):
......@@ -44,19 +44,19 @@ class RunPromise(GenericPromise):
def senseBotStatus(self):
key = 'bot_status'
def logError(msg, *args):
self.appendError(key + ': ' + msg % args)
def appendError(msg, *args):
self.appendErrorMessage(key + ': ERROR ' + msg % args)
if key not in self.surykatka_json:
logError("%r not in %r", key, self.json_file)
appendError("%r not in %r", key, self.json_file)
return
bot_status_list = self.surykatka_json[key]
if len(bot_status_list) == 0:
logError("%r empty in %r", key, self.json_file)
appendError("%r empty in %r", key, self.json_file)
return
bot_status = bot_status_list[0]
if bot_status.get('text') != 'loop':
logError("No type loop detected in %r", self.json_file)
appendError("No type loop detected in %r", self.json_file)
return
timetuple = email.utils.parsedate(bot_status['date'])
last_bot_datetime = datetime.datetime.fromtimestamp(time.mktime(timetuple))
......@@ -64,23 +64,24 @@ class RunPromise(GenericPromise):
delta = self.utcnow - last_bot_datetime
# sanity check
if delta < datetime.timedelta(minutes=0):
logError('Last bot datetime %s is in future, UTC now %s',
last_bot_datetime_string, self.utcnow_string)
appendError('Last bot datetime %s is in future, UTC now %s',
last_bot_datetime_string, self.utcnow_string)
return
if delta > datetime.timedelta(minutes=15):
logError('Last bot datetime %s is more than 15 minutes old, UTC now %s',
last_bot_datetime_string, self.utcnow_string)
appendError(
'Last bot datetime %s is more than 15 minutes old, UTC now %s',
last_bot_datetime_string, self.utcnow_string)
return
self.appendInfo(
'%s: Last bot status from %s ok, UTC now is %s' %
self.appendInfoMessage(
'%s: OK Last bot status from %s, UTC now is %s' %
(key, last_bot_datetime_string, self.utcnow_string))
def senseSslCertificate(self):
key = 'ssl_certificate'
def appendError(msg, *args):
self.appendError(key + ': ' + msg % args)
self.appendErrorMessage(key + ': ERROR ' + msg % args)
url = self.getConfig('url')
parsed_url = urlparse(url)
......@@ -128,8 +129,8 @@ class RunPromise(GenericPromise):
self.utcnow_string)
return
else:
self.appendInfo(
'%s: Certificate for %s will expire on %s, which is more than %s '
self.appendInfoMessage(
'%s: OK Certificate for %s will expire on %s, which is more than %s '
'days, UTC now is %s' %
(key, url, entry['not_after'], certificate_expiration_days,
self.utcnow_string))
......@@ -139,11 +140,11 @@ class RunPromise(GenericPromise):
key = 'http_query'
error_list = []
def logError(msg, *args):
self.appendError(key + ': ' + msg % args)
def appendError(msg, *args):
self.appendErrorMessage(key + ': ERROR ' + msg % args)
if key not in self.surykatka_json:
logError("%r not in %r", key, self.json_file)
appendError("%r not in %r", key, self.json_file)
return
url = self.getConfig('url')
......@@ -152,7 +153,7 @@ class RunPromise(GenericPromise):
entry_list = [q for q in self.surykatka_json[key] if q['url'] == url]
if len(entry_list) == 0:
logError('No data for %s', url)
appendError('No data for %s', url)
return
error_list = []
for entry in entry_list:
......@@ -175,15 +176,15 @@ class RunPromise(GenericPromise):
'expected IPs %s differes from got %s' % (
' '.join(ip_list), ' '.join(db_ip_list)))
if len(error_list):
logError('Problem with %s : ' % (url,) + ', '.join(error_list))
appendError('%s : ' % (url,) + ', '.join(error_list))
return
if len(ip_list) > 0:
self.appendInfo(
'%s: %s replied correctly with status code %s on ip list %s' %
self.appendInfoMessage(
'%s: OK %s replied correctly with status code %s on ip list %s' %
(key, url, status_code, ' '.join(ip_list)))
else:
self.appendInfo(
'%s: %s replied correctly with status code %s' %
self.appendInfoMessage(
'%s: OK %s replied correctly with status code %s' %
(key, url, status_code))
def sense(self):
......@@ -202,13 +203,14 @@ class RunPromise(GenericPromise):
self.json_file = self.getConfig('json-file', '')
if not os.path.exists(self.json_file):
self.appendError('File %r does not exists' % self.json_file)
self.appendErrorMessage('ERROR File %r does not exists' % self.json_file)
else:
with open(self.json_file) as fh:
try:
self.surykatka_json = json.load(fh)
except Exception:
self.appendError("Problem loading JSON from %r" % self.json_file)
self.appendErrorMessage(
"ERROR loading JSON from %r" % self.json_file)
else:
report = self.getConfig('report')
if report == 'bot_status':
......@@ -217,7 +219,8 @@ class RunPromise(GenericPromise):
self.senseHttpQuery()
self.senseSslCertificate()
else:
self.appendError("Report %r is not supported" % report)
self.appendErrorMessage(
"ERROR Report %r is not supported" % report)
self.emitLog()
def anomaly(self):
......
......@@ -51,7 +51,7 @@ class TestCheckSurykatkaJSON(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"File '' does not exists")
"ERROR File '' does not exists")
def test_not_existing_file(self):
self.writeSurykatkaPromise({'json-file': self.json_file})
......@@ -60,7 +60,7 @@ class TestCheckSurykatkaJSON(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"File '%s' does not exists" % (self.json_file,))
"ERROR File '%s' does not exists" % (self.json_file,))
def test_empty_file(self):
self.writeSurykatkaPromise({'json-file': self.json_file})
......@@ -70,7 +70,7 @@ class TestCheckSurykatkaJSON(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"Problem loading JSON from '%s'" % (self.json_file,))
"ERROR loading JSON from '%s'" % (self.json_file,))
class TestCheckSurykatkaJSONUnknownReport(CheckSurykatkaJSONMixin):
......@@ -89,7 +89,7 @@ class TestCheckSurykatkaJSONUnknownReport(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"Report 'NOT_EXISTING_ENTRY' is not supported")
"ERROR Report 'NOT_EXISTING_ENTRY' is not supported")
class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
......@@ -114,8 +114,8 @@ class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertPassedMessage(
self.getPromiseResult(self.promise_name),
"bot_status: Last bot status from Fri, 13 Dec 2222 08:10:11 -0000 "
"ok, UTC now is Wed, 13 Dec 2222 09:11:12 -0000"
"bot_status: OK Last bot status from Fri, 13 Dec 2222 08:10:11 -0000, "
"UTC now is Wed, 13 Dec 2222 09:11:12 -0000"
)
def test_bot_status_future(self):
......@@ -140,7 +140,7 @@ class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"bot_status: Last bot datetime Sat, 13 Dec 2223 08:10:11 -0000 is "
"bot_status: ERROR Last bot datetime Sat, 13 Dec 2223 08:10:11 -0000 is "
"in future, UTC now Wed, 13 Dec 2222 09:11:12 -0000"
)
......@@ -166,7 +166,7 @@ class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"bot_status: Last bot datetime Sat, 13 Dec 2223 08:10:11 -0000 is "
"bot_status: ERROR Last bot datetime Sat, 13 Dec 2223 08:10:11 -0000 is "
"more than 15 minutes old, UTC now Wed, 13 Dec 2223 09:26:12 -0000"
)
......@@ -185,7 +185,7 @@ class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"bot_status: 'bot_status' not in '%s'" % (self.json_file,))
"bot_status: ERROR 'bot_status' not in '%s'" % (self.json_file,))
def test_empty_bot_status(self):
self.writeSurykatkaPromise(
......@@ -203,7 +203,7 @@ class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"bot_status: 'bot_status' empty in '%s'" % (self.json_file,))
"bot_status: ERROR 'bot_status' empty in '%s'" % (self.json_file,))
class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
......@@ -259,9 +259,9 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertPassedMessage(
self.getPromiseResult(self.promise_name),
"http_query: https://www.erp5.com/ replied correctly with "
"http_query: OK https://www.erp5.com/ replied correctly with "
"status code 302 on ip list 127.0.0.1 127.0.0.2 ssl_certificate: "
"Certificate for https://www.erp5.com/ will expire on Mon, 13 Jul "
"OK Certificate for https://www.erp5.com/ will expire on Mon, 13 Jul "
"2020 12:00:00 -0000, which is more than 15 days, UTC now is "
"Fri, 27 Dec 2019 15:11:12 -0000"
)
......@@ -306,7 +306,7 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertPassedMessage(
self.getPromiseResult(self.promise_name),
"http_query: http://www.erp5.com/ replied correctly with "
"http_query: OK http://www.erp5.com/ replied correctly with "
"status code 302 on ip list 127.0.0.1 127.0.0.2"
)
......@@ -361,9 +361,9 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertPassedMessage(
self.getPromiseResult(self.promise_name),
"http_query: https://www.erp5.com/ replied correctly with status "
"code 302 ssl_certificate: Certificate for https://www.erp5.com/ will "
"expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 "
"http_query: OK https://www.erp5.com/ replied correctly with status "
"code 302 ssl_certificate: OK Certificate for https://www.erp5.com/ "
"will expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 "
"days, UTC now is Fri, 27 Dec 2019 15:11:12 -0000"
)
......@@ -402,8 +402,8 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"http_query: No data for https://www.erp5.com/ ssl_certificate: "
"Certificate for https://www.erp5.com/ will expire on Mon, 13 Jul "
"http_query: ERROR No data for https://www.erp5.com/ ssl_certificate: "
"OK Certificate for https://www.erp5.com/ will expire on Mon, 13 Jul "
"2020 12:00:00 -0000, which is more than 15 days, UTC now is "
"Fri, 27 Dec 2019 15:11:12 -0000"
)
......@@ -449,8 +449,8 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"ssl_certificate: No data for https://www.erp5.com/ http_query: "
"https://www.erp5.com/ replied correctly with "
"ssl_certificate: ERROR No data for https://www.erp5.com/ http_query: "
"OK https://www.erp5.com/ replied correctly with "
"status code 302 on ip list 127.0.0.1 127.0.0.2"
)
......@@ -493,9 +493,10 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"ssl_certificate: No data for https://www.erp5.com/ . If the error "
"persist, please update surykatka. http_query: https://www.erp5.com/ "
"replied correctly with status code 302 on ip list 127.0.0.1 127.0.0.2"
"ssl_certificate: ERROR No data for https://www.erp5.com/ . If the "
"error persist, please update surykatka. http_query: OK "
"https://www.erp5.com/ replied correctly with status code 302 on ip "
"list 127.0.0.1 127.0.0.2"
)
def test_bad_code(self):
......@@ -550,8 +551,8 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"http_query: Problem with https://www.erp5.com/ : IP 127.0.0.1 got "
"status code 302 instead of 301 ssl_certificate: Certificate for "
"http_query: ERROR https://www.erp5.com/ : IP 127.0.0.1 got "
"status code 302 instead of 301 ssl_certificate: OK Certificate for "
"https://www.erp5.com/ will expire on Mon, 13 Jul 2020 12:00:00 "
"-0000, which is more than 15 days, UTC now is Fri, 27 Dec 2019 "
"15:11:12 -0000"
......@@ -597,8 +598,8 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"http_query: Problem with https://www.erp5.com/ : IP 127.0.0.1 got "
"status code %s instead of 301 ssl_certificate: Certificate for "
"http_query: ERROR https://www.erp5.com/ : IP 127.0.0.1 got "
"status code %s instead of 301 ssl_certificate: OK Certificate for "
"https://www.erp5.com/ will expire on Mon, 13 Jul 2020 12:00:00 "
"-0000, which is more than 15 days, UTC now is Fri, 27 Dec 2019 "
"15:11:12 -0000" % explanation
......@@ -669,9 +670,9 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"http_query: Problem with https://www.erp5.com/ : expected IPs "
"http_query: ERROR https://www.erp5.com/ : expected IPs "
"127.0.0.1 127.0.0.2 differes from got 127.0.0.1 127.0.0.4 "
"ssl_certificate: Certificate for https://www.erp5.com/ will expire "
"ssl_certificate: OK Certificate for https://www.erp5.com/ will expire "
"on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days, "
"UTC now is Fri, 27 Dec 2019 15:11:12 -0000"
)
......@@ -729,9 +730,9 @@ class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
self.launcher.run()
self.assertFailedMessage(
self.getPromiseResult(self.promise_name),
"http_query: Problem with https://www.erp5.com/ : IP 127.0.0.1 got "
"http_query: ERROR https://www.erp5.com/ : IP 127.0.0.1 got "
"status code 302 instead of 301, expected IPs 127.0.0.1 127.0.0.2 "
"differes from got 127.0.0.1 127.0.0.4 ssl_certificate: Certificate "
"differes from got 127.0.0.1 127.0.0.4 ssl_certificate: OK Certificate "
"for https://www.erp5.com/ will expire on Mon, 13 Jul 2020 12:00:00 "
"-0000, which is more than 15 days, UTC now is Fri, 27 Dec 2019 "
"15:11:12 -0000"
......
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