Add better error messages

- Add status.json with HTTP status code table
 - Add error fetching on PingAdapter
 - Give warning on no alerts set
This commit is contained in:
Luna Mendes 2018-09-03 22:39:41 -03:00
parent 673e5bb0fc
commit 3437bd2b72
3 changed files with 25 additions and 3 deletions

View File

@ -1,9 +1,11 @@
import asyncio
import json
import time
import re
PING_RGX = re.compile(r'(.+)( 0% packet loss)(.+)', re.I | re.M)
PING_LATENCY_RGX = re.compile('time\=(\d+(\.\d+)?) ms', re.M)
PING_ERROR_RGX = re.compile('icmp_seq\=(\d+)\ ([^time].*)$', re.M)
class Adapter:
@ -64,8 +66,11 @@ class PingAdapter(Adapter):
else:
latency = 0
worker.log.info(f'alive={alive} latency={latency}ms')
return cls._construct(alive, latency)
err = PING_ERROR_RGX.search(out)
err_msg = err.group(2) if not alive and err else 'error not found'
worker.log.info(f'alive={alive} latency={latency}ms err={err_msg!r}')
return cls._construct(alive, latency, err_msg)
class HttpAdapter(Adapter):
@ -75,6 +80,16 @@ class HttpAdapter(Adapter):
'db': ('timestamp', 'status', 'latency')
}
@classmethod
def get_phrase(cls, status: int):
with open('./status.json', 'r') as fd:
statuses = json.load(fd)
try:
return statuses[str(status)]
except KeyError:
return 'Unknown status'
@classmethod
async def query(cls, worker, adp_args: dict):
# yes, lots of attributes
@ -93,7 +108,8 @@ class HttpAdapter(Adapter):
worker.log.info(f'status={resp.status} latency={latency}ms')
if not succ:
err_str = f'HTTP Status - {resp.status}'
status_phrase = cls.get_phrase(resp.status)
err_str = f'HTTP Status - {resp.status} - {status_phrase}'
return cls._construct(succ, latency, err_str)
return cls._construct(succ, latency if succ else 0)

View File

@ -76,6 +76,7 @@ class ServiceWorker:
# extract latest and old from rows
first, last = rows
first_status, last_status = first[0], last[0]
print(first_status, last_status)
# dont do anything if theres no change
# to the statuses
@ -85,6 +86,10 @@ class ServiceWorker:
# oopsie whoopsie time to alertie owo
alerts = self.service.get('alerts', [])
if not alerts:
self.log.warning(f'no alerts set for {self.name}')
return
for alert in alerts:
try:
alert_obj = self.manager.alerts[alert]

1
status.json Normal file
View File

@ -0,0 +1 @@
{"1xx": "**Informational**", "100": "Continue", "101": "Switching Protocols", "2xx": "**Successful**", "200": "OK", "201": "Created", "202": "Accepted", "203": "Non-Authoritative Information", "204": "No Content", "205": "Reset Content", "206": "Partial Content", "3xx": "**Redirection**", "300": "Multiple Choices", "301": "Moved Permanently", "302": "Found", "303": "See Other", "304": "Not Modified", "305": "Use Proxy", "307": "Temporary Redirect", "4xx": "**Client Error**", "400": "Bad Request", "401": "Unauthorized", "402": "Payment Required", "403": "Forbidden", "404": "Not Found", "405": "Method Not Allowed", "406": "Not Acceptable", "407": "Proxy Authentication Required", "408": "Request Timeout", "409": "Conflict", "410": "Gone", "411": "Length Required", "412": "Precondition Failed", "413": "Payload Too Large", "414": "URI Too Long", "415": "Unsupported Media Type", "416": "Range Not Satisfiable", "417": "Expectation Failed", "418": "I'm a teapot", "426": "Upgrade Required", "5xx": "**Server Error**", "500": "Internal Server Error", "501": "Not Implemented", "502": "Bad Gateway", "503": "Service Unavailable", "504": "Gateway Time-out", "505": "HTTP Version Not Supported", "102": "Processing", "207": "Multi-Status", "226": "IM Used", "308": "Permanent Redirect", "422": "Unprocessable Entity", "423": "Locked", "424": "Failed Dependency", "428": "Precondition Required", "429": "Too Many Requests", "431": "Request Header Fields Too Large", "451": "Unavailable For Legal Reasons", "506": "Variant Also Negotiates", "507": "Insufficient Storage", "511": "Network Authentication Required", "7xx": "**Developer Error**"}