From 3437bd2b72457b8cc00c25ef8b01f33d71585559 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Mon, 3 Sep 2018 22:39:41 -0300 Subject: [PATCH] Add better error messages - Add status.json with HTTP status code table - Add error fetching on PingAdapter - Give warning on no alerts set --- elstat/adapters.py | 22 +++++++++++++++++++--- elstat/worker.py | 5 +++++ status.json | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 status.json diff --git a/elstat/adapters.py b/elstat/adapters.py index dae01bd..c22afb6 100644 --- a/elstat/adapters.py +++ b/elstat/adapters.py @@ -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) diff --git a/elstat/worker.py b/elstat/worker.py index 5933b89..cdb5dfa 100644 --- a/elstat/worker.py +++ b/elstat/worker.py @@ -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] diff --git a/status.json b/status.json new file mode 100644 index 0000000..f641dca --- /dev/null +++ b/status.json @@ -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**"} \ No newline at end of file