Commit ed935a62 authored by Łukasz Nowak's avatar Łukasz Nowak

check_surykatka_json: Simplify the code a bit

parent 40fd7799
...@@ -47,112 +47,98 @@ class RunPromise(GenericPromise): ...@@ -47,112 +47,98 @@ class RunPromise(GenericPromise):
self.message_list.insert(0, '%s :' % (url,)) self.message_list.insert(0, '%s :' % (url,))
emit(' '.join(self.message_list)) emit(' '.join(self.message_list))
def appendError(self, message):
self.error = True
self.message_list.extend(['ERROR', message])
def appendOk(self, message):
self.message_list.extend(['OK', message])
def senseBotStatus(self): def senseBotStatus(self):
key = 'bot_status' key = 'bot_status'
self.appendMessage('%s:' % (key, ))
def appendError(msg, *args):
self.error = True
self.appendMessage(key + ': ERROR ' + msg % args)
if key not in self.surykatka_json: if key not in self.surykatka_json:
appendError("%r not in %r", key, self.json_file) self.appendError("%r not in %r" % (key, self.json_file))
return return
bot_status_list = self.surykatka_json[key] bot_status_list = self.surykatka_json[key]
if len(bot_status_list) == 0: if len(bot_status_list) == 0:
appendError("%r empty in %r", key, self.json_file) self.appendError("%r empty in %r" % (key, self.json_file))
return return
bot_status = bot_status_list[0] bot_status = bot_status_list[0]
if bot_status.get('text') != 'loop': if bot_status.get('text') != 'loop':
appendError( self.appendError(
"bot_status is %r instead of 'loop' in %r", "bot_status is %r instead of 'loop' in %r" % (str(
str(bot_status.get('text')), self.json_file) bot_status.get('text')), self.json_file))
return return
timetuple = email.utils.parsedate(bot_status['date']) timetuple = email.utils.parsedate(bot_status['date'])
last_bot_datetime = datetime.datetime.fromtimestamp(time.mktime(timetuple)) last_bot_datetime = datetime.datetime.fromtimestamp(time.mktime(timetuple))
delta = self.utcnow - last_bot_datetime delta = self.utcnow - last_bot_datetime
# sanity check # sanity check
if delta < datetime.timedelta(minutes=0): if delta < datetime.timedelta(minutes=0):
appendError('Last bot datetime is in future') self.appendError('Last bot datetime is in future')
return return
if delta > datetime.timedelta(minutes=15): if delta > datetime.timedelta(minutes=15):
appendError('Last bot datetime is more than 15 minutes old') self.appendError('Last bot datetime is more than 15 minutes old')
return return
self.appendMessage('%s: OK Last bot status' % (key,)) self.appendOk('Last bot status')
def senseSslCertificate(self): def senseSslCertificate(self):
key = 'ssl_certificate' key = 'ssl_certificate'
self.appendMessage('%s:' % (key, ))
def appendError(msg, *args):
self.error = True
self.appendMessage(key + ': ERROR ' + msg % args)
url = self.getConfig('url') url = self.getConfig('url')
parsed_url = urlparse(url) parsed_url = urlparse(url)
if parsed_url.scheme == 'https': if parsed_url.scheme == 'https':
hostname = parsed_url.netloc hostname = parsed_url.netloc
ssl_check = True
certificate_expiration_days = self.getConfig( certificate_expiration_days = self.getConfig(
'certificate-expiration-days', '15') 'certificate-expiration-days', '15')
try: try:
certificate_expiration_days = int(certificate_expiration_days) certificate_expiration_days = int(certificate_expiration_days)
except ValueError: except ValueError:
certificate_expiration_days = None self.appendError(
'certificate-expiration-days %r is incorrect' % (
self.getConfig('certificate-expiration-days')))
else: else:
ssl_check = False self.appendOk('No check needed')
certificate_expiration_days = None
if not ssl_check:
self.appendMessage('%s: OK No check needed' % (key,))
return
if certificate_expiration_days is None:
appendError(
'certificate-expiration-days %r is incorrect',
self.getConfig('certificate-expiration-days'))
return return
if not hostname: if not hostname:
appendError('url is incorrect') self.appendError('url is incorrect')
return return
if key not in self.surykatka_json: if key not in self.surykatka_json:
appendError( self.appendError(
'No key %r. If the error persist, please update surykatka.' % (key,)) 'No key %r. If the error persist, please update surykatka.' % (key,))
return return
entry_list = [ entry_list = [
q for q in self.surykatka_json[key] if q['hostname'] == hostname] q for q in self.surykatka_json[key] if q['hostname'] == hostname]
if len(entry_list) == 0: if len(entry_list) == 0:
appendError('No data') self.appendError('No data')
return return
if len(entry_list) > 0:
self.appendMessage('%s:' % (key,))
def addError(msg, *args):
self.error = True
self.appendMessage('ERROR ' + msg % args)
for entry in sorted(entry_list, key=operator.itemgetter('ip')): for entry in sorted(entry_list, key=operator.itemgetter('ip')):
timetuple = email.utils.parsedate(entry['not_after']) timetuple = email.utils.parsedate(entry['not_after'])
if timetuple is None: if timetuple is None:
addError('IP %s no information' % (entry['ip'],)) self.appendError('IP %s no information' % (entry['ip'],))
else: else:
certificate_expiration_time = datetime.datetime.fromtimestamp( certificate_expiration_time = datetime.datetime.fromtimestamp(
time.mktime(timetuple)) time.mktime(timetuple))
if certificate_expiration_time - datetime.timedelta( if certificate_expiration_time - datetime.timedelta(
days=certificate_expiration_days) < self.utcnow: days=certificate_expiration_days) < self.utcnow:
addError( self.appendError(
'IP %s will expire in < %s days', 'IP %s will expire in < %s days' % (
entry['ip'], certificate_expiration_days) entry['ip'], certificate_expiration_days))
else: else:
self.appendMessage( self.appendOk(
'OK IP %s will expire in > %s days' % ( 'IP %s will expire in > %s days' % (
entry['ip'], certificate_expiration_days)) entry['ip'], certificate_expiration_days))
def senseHttpQuery(self): def senseHttpQuery(self):
key = 'http_query' key = 'http_query'
self.appendMessage('%s:' % (key, ))
def appendError(msg, *args):
self.error = True
self.appendMessage(key + ': ERROR ' + msg % args)
if key not in self.surykatka_json: if key not in self.surykatka_json:
appendError("%r not in %r", key, self.json_file) self.appendError("%r not in %r" % (key, self.json_file))
return return
url = self.getConfig('url') url = self.getConfig('url')
...@@ -161,13 +147,9 @@ class RunPromise(GenericPromise): ...@@ -161,13 +147,9 @@ class RunPromise(GenericPromise):
entry_list = [q for q in self.surykatka_json[key] if q['url'] == url] entry_list = [q for q in self.surykatka_json[key] if q['url'] == url]
if len(entry_list) == 0: if len(entry_list) == 0:
appendError('No data') self.appendError('No data')
return return
def addError(msg, *args):
self.error = True
self.appendMessage('ERROR ' + msg % args)
self.appendMessage('%s:' % (key,))
for entry in sorted(entry_list, key=operator.itemgetter('ip')): for entry in sorted(entry_list, key=operator.itemgetter('ip')):
entry_status_code = str(entry['status_code']) entry_status_code = str(entry['status_code'])
if entry_status_code != status_code: if entry_status_code != status_code:
...@@ -178,33 +160,30 @@ class RunPromise(GenericPromise): ...@@ -178,33 +160,30 @@ class RunPromise(GenericPromise):
entry_status_code, status_code_explanation) entry_status_code, status_code_explanation)
else: else:
status_code_explanation = entry_status_code status_code_explanation = entry_status_code
addError( self.appendError(
'IP %s expected status_code %s != %s' % ( 'IP %s expected status_code %s != %s' % (
entry['ip'], status_code_explanation, status_code)) entry['ip'], status_code_explanation, status_code))
else: else:
self.appendMessage( self.appendOk(
'OK IP %s status_code %s' % (entry['ip'], status_code)) 'IP %s status_code %s' % (entry['ip'], status_code))
if http_header_dict: if http_header_dict:
if http_header_dict != entry['http_header_dict']: if http_header_dict != entry['http_header_dict']:
addError( self.appendError(
'IP %s expected HTTP Header %s != of %s' % ( 'IP %s expected HTTP Header %s != of %s' % (
entry['ip'], entry['ip'],
json.dumps(http_header_dict, sort_keys=True), json.dumps(http_header_dict, sort_keys=True),
json.dumps(entry['http_header_dict'], sort_keys=True))) json.dumps(entry['http_header_dict'], sort_keys=True)))
else: else:
self.appendMessage( self.appendOk(
'OK IP %s HTTP Header %s' % ( 'IP %s HTTP Header %s' % (
entry['ip'], json.dumps(http_header_dict, sort_keys=True))) entry['ip'], json.dumps(http_header_dict, sort_keys=True)))
def senseDnsQuery(self): def senseDnsQuery(self):
key = 'dns_query' key = 'dns_query'
self.appendMessage('%s:' % (key, ))
def appendError(msg, *args):
self.error = True
self.appendMessage(key + ': ERROR ' + msg % args)
if key not in self.surykatka_json: if key not in self.surykatka_json:
appendError("%r not in %r", key, self.json_file) self.appendError("%r not in %r" % (key, self.json_file))
return return
url = self.getConfig('url') url = self.getConfig('url')
...@@ -215,36 +194,31 @@ class RunPromise(GenericPromise): ...@@ -215,36 +194,31 @@ class RunPromise(GenericPromise):
q for q in self.surykatka_json[key] q for q in self.surykatka_json[key]
if q['domain'] == hostname and q['rdtype'] == 'A'] if q['domain'] == hostname and q['rdtype'] == 'A']
if len(entry_list) == 0: if len(entry_list) == 0:
appendError('No data') self.appendError('No data')
return return
self.appendMessage('%s:' % (key,))
if len(ip_set): if len(ip_set):
for entry in sorted(entry_list, key=operator.itemgetter('resolver_ip')): for entry in sorted(entry_list, key=operator.itemgetter('resolver_ip')):
response_ip_set = set([ response_ip_set = set([
q.strip() for q in entry['response'].split(",") if q.strip()]) q.strip() for q in entry['response'].split(",") if q.strip()])
if ip_set != response_ip_set: if ip_set != response_ip_set:
self.error = True self.appendError(
self.appendMessage( "resolver %s expected %s != %s" % (
"ERROR resolver %s expected %s != %s" % (
entry['resolver_ip'], ' '.join(sorted(ip_set)), entry['resolver_ip'], ' '.join(sorted(ip_set)),
' '.join(sorted(response_ip_set)) or "empty-reply")) ' '.join(sorted(response_ip_set)) or "empty-reply"))
else: else:
self.appendMessage( self.appendOk(
"OK resolver %s returned expected set of IPs %s" % ( "resolver %s returned expected set of IPs %s" % (
entry['resolver_ip'], ' '.join(sorted(ip_set)),)) entry['resolver_ip'], ' '.join(sorted(ip_set)),))
else: else:
self.appendMessage('OK No check configured') self.appendOk('No check configured')
def senseTcpServer(self): def senseTcpServer(self):
key = 'tcp_server' key = 'tcp_server'
self.appendMessage('%s:' % (key, ))
def appendError(msg, *args):
self.error = True
self.appendMessage(key + ': ERROR ' + msg % args)
if key not in self.surykatka_json: if key not in self.surykatka_json:
appendError("%r not in %r", key, self.json_file) self.appendError("%r not in %r" % (key, self.json_file))
return return
url = self.getConfig('url') url = self.getConfig('url')
...@@ -264,9 +238,8 @@ class RunPromise(GenericPromise): ...@@ -264,9 +238,8 @@ class RunPromise(GenericPromise):
if hostname in [ if hostname in [
r.strip() for r in q['domain'].split(',')] and q['port'] == port] r.strip() for r in q['domain'].split(',')] and q['port'] == port]
if len(entry_list) == 0: if len(entry_list) == 0:
appendError('No data') self.appendError('No data')
return return
self.appendMessage('%s:' % (key,))
if len(ip_set) > 0: if len(ip_set) > 0:
for ip in sorted(ip_set): for ip in sorted(ip_set):
ok = False ok = False
...@@ -278,27 +251,21 @@ class RunPromise(GenericPromise): ...@@ -278,27 +251,21 @@ class RunPromise(GenericPromise):
if entry['state'] == 'open': if entry['state'] == 'open':
ok = True ok = True
if ok: if ok:
self.appendMessage('OK IP %s:%s' % (ip, port)) self.appendOk('IP %s:%s' % (ip, port))
else: else:
self.error = True self.appendError('IP %s:%s' % (ip, port))
self.appendMessage('ERROR IP %s:%s' % (ip, port))
else: else:
self.appendMessage('OK No check configured') self.appendOk('No check configured')
def senseElapsedTime(self): def senseElapsedTime(self):
key = 'elapsed_time' key = 'elapsed_time'
self.appendMessage('%s:' % (key, ))
surykatka_key = 'http_query' surykatka_key = 'http_query'
def appendError(msg, *args):
self.error = True
self.appendMessage('ERROR ' + msg % args)
if surykatka_key not in self.surykatka_json: if surykatka_key not in self.surykatka_json:
self.error = True self.appendError(
self.appendMessage( 'No key %r. If the error persist, please update surykatka.' % (
'%s: ERROR No key %r. If the error persist, please update ' surykatka_key,))
'surykatka.' % (
key, surykatka_key,))
return return
url = self.getConfig('url') url = self.getConfig('url')
...@@ -307,10 +274,8 @@ class RunPromise(GenericPromise): ...@@ -307,10 +274,8 @@ class RunPromise(GenericPromise):
entry_list = [ entry_list = [
q for q in self.surykatka_json[surykatka_key] if q['url'] == url] q for q in self.surykatka_json[surykatka_key] if q['url'] == url]
if len(entry_list) == 0: if len(entry_list) == 0:
self.error = True self.appendError('No data')
self.appendMessage('%s: ERROR No data' % (key,))
return return
self.appendMessage('%s:' % (key,))
if maximum_elapsed_time: if maximum_elapsed_time:
found = False found = False
for entry in sorted(entry_list, key=operator.itemgetter('ip')): for entry in sorted(entry_list, key=operator.itemgetter('ip')):
...@@ -318,40 +283,37 @@ class RunPromise(GenericPromise): ...@@ -318,40 +283,37 @@ class RunPromise(GenericPromise):
found = True found = True
maximum_elapsed_time = float(maximum_elapsed_time) maximum_elapsed_time = float(maximum_elapsed_time)
if entry['total_seconds'] == 0.: if entry['total_seconds'] == 0.:
appendError('IP %s failed to reply' % (entry['ip'])) self.appendError('IP %s failed to reply' % (entry['ip']))
elif entry['total_seconds'] > maximum_elapsed_time: elif entry['total_seconds'] > maximum_elapsed_time:
appendError( self.appendError(
'IP %s replied > %.2fs' % 'IP %s replied > %.2fs' %
(entry['ip'], maximum_elapsed_time)) (entry['ip'], maximum_elapsed_time))
else: else:
self.appendMessage( self.appendOk('IP %s replied < %.2fs' % (
'OK IP %s replied < %.2fs' % ( entry['ip'], maximum_elapsed_time))
entry['ip'], maximum_elapsed_time))
if not found: if not found:
appendError( self.appendError(
"No entry with total_seconds found. If the error persist, please " "No entry with total_seconds found. If the error persist, please "
"update surykatka") "update surykatka")
else: else:
self.appendMessage("OK No check configured") self.appendOk("No check configured")
def sense(self): def sense(self):
""" """
Check if frontend URL is available Sense various information about the given url
""" """
self.utcnow = datetime.datetime.utcnow() self.utcnow = datetime.datetime.utcnow()
self.json_file = self.getConfig('json-file', '') self.json_file = self.getConfig('json-file', '')
if not os.path.exists(self.json_file): if not os.path.exists(self.json_file):
self.error = True self.appendError('File %r does not exists' % self.json_file)
self.appendMessage('ERROR File %r does not exists' % self.json_file)
else: else:
with open(self.json_file) as fh: with open(self.json_file) as fh:
try: try:
self.surykatka_json = json.load(fh) self.surykatka_json = json.load(fh)
except Exception: except Exception:
self.error = True self.appendError(
self.appendMessage( "loading JSON from %r" % self.json_file)
"ERROR loading JSON from %r" % self.json_file)
else: else:
report = self.getConfig('report') report = self.getConfig('report')
if report == 'bot_status': if report == 'bot_status':
...@@ -363,9 +325,8 @@ class RunPromise(GenericPromise): ...@@ -363,9 +325,8 @@ class RunPromise(GenericPromise):
self.senseSslCertificate() self.senseSslCertificate()
self.senseElapsedTime() self.senseElapsedTime()
else: else:
self.error = True self.appendError(
self.appendMessage( "Report %r is not supported" % report)
"ERROR Report %r is not supported" % report)
self.emitLog() self.emitLog()
def anomaly(self): def anomaly(self):
......
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