From e807bb3b9a7fcf2fecbfc75f60f58ab1b12f77ae Mon Sep 17 00:00:00 2001 From: Doacola Date: Tue, 6 Oct 2020 00:05:01 +0300 Subject: [PATCH 1/3] Attempt to fix output parsing --- src/exporter.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/exporter.py b/src/exporter.py index bcbde24..9f6ad94 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -27,12 +27,23 @@ def run_speedtest(): print("Using custom server ID: "+str(server)) cmd.append("--server-id="+str(server)) output = subprocess.check_output(cmd) - if is_json(output): - data = json.loads(output) - actual_ping = int(data['ping']['latency']) - download = bytes_to_bits(data['download']['bandwidth']) - upload = bytes_to_bits(data['upload']['bandwidth']) - return (actual_ping, download, upload) + + while True: + if is_json(output): + data = json.loads(output) + if "error" in data: + # If we get here it probably means that socket timed out(Network issues?) + print('Something went wrong') + print(data['error']) + return None + if "type" in data: + if data['type'] == 'log': + print(str(data["timestamp"]) + " - " + str(data["message"])) + if data['type'] == 'result': + actual_ping = int(data['ping']['latency']) + download = bytes_to_bits(data['download']['bandwidth']) + upload = bytes_to_bits(data['upload']['bandwidth']) + return (actual_ping, download, upload) def update_results(test_done): ping.set(test_done[0]) @@ -46,8 +57,9 @@ def run(http_port, sleep_time): print("Successfully started Speedtest Exporter on http://localhost:" + str(http_port)) while True: test = run_speedtest() - update_results(test) - time.sleep(sleep_time) + if isinstance(test, tuple): + update_results(test) + time.sleep(sleep_time) if __name__ == '__main__': # Create the Metrics From 1dd004d321cf9efa637072bd372ac542c7929d9a Mon Sep 17 00:00:00 2001 From: Doacola Date: Tue, 6 Oct 2020 01:24:44 +0300 Subject: [PATCH 2/3] Try ignoring the exit code from cli on error --- src/exporter.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/exporter.py b/src/exporter.py index 9f6ad94..495f509 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -20,15 +20,19 @@ def is_json(myjson): return True def run_speedtest(): + print('Starting speedtest') cmd = ["speedtest", "--format=json-pretty", "--progress=no", "--accept-license", "--accept-gdpr"] server = os.environ.get('SPEEDTEST_SERVER') if server: if server.isnumeric(): print("Using custom server ID: "+str(server)) - cmd.append("--server-id="+str(server)) - output = subprocess.check_output(cmd) + cmd.append("--server-id=" + str(server)) while True: + try: + output = subprocess.check_output(cmd) + except subprocess.CalledProcessError as e: + output = e.output if is_json(output): data = json.loads(output) if "error" in data: From 55da636aed7462566f4513f6276f1d851cf83443 Mon Sep 17 00:00:00 2001 From: Doacola Date: Thu, 8 Oct 2020 14:52:52 +0300 Subject: [PATCH 3/3] Add env configuration and minor cleanup --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 73714d5..9949f5d 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ # Speedtest Exporter -## Description +Simple **Speedtest exporter** for **Prometheus** written in **Python** using the official CLI from **Ookla** -This is a simple **speedtest exporter** for **Prometheus** written in **Python** +## Quick Start -## Running the Container - -To run the container via **CLI**: +**Docker**: ```bash docker run -d \ @@ -29,4 +27,11 @@ services: restart: unless-stopped ``` -Then just acess the page `http://localhost:9112/` and you will have the metrics. \ No newline at end of file +Then just acess the page `http://localhost:9112/` and you will have the metrics. + +### Environment Variables + +The following environment variables configure the exporter: + +* `SPEEDTEST_SERVER` + Custom server ID from Speedtest server list like [https://telcodb.net/explore/speedtest-servers/](https://telcodb.net/explore/speedtest-servers/)