From 7928c00989e59eb09d030b9f16ec95f49d297e13 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Thu, 30 Dec 2021 21:22:16 +0000 Subject: [PATCH 1/5] .gitignore: Add venv --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f2ef5e9..f3b0ad7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode -env \ No newline at end of file +env +venv \ No newline at end of file From ddea693262783694511b05edbcae6617757bb55d Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Thu, 30 Dec 2021 21:35:27 +0000 Subject: [PATCH 2/5] src: exporter.py: Change from Mb/s to Mbps --- src/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/exporter.py b/src/exporter.py index 7d75be3..fd2ae89 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -27,7 +27,7 @@ def bytes_to_bits(bytes_per_sec): def bits_to_megabits(bits_per_sec): megabits = round(bits_per_sec * (10**-6), 2) - return str(megabits) + " Mb/s" + return str(megabits) + " Mbps" def is_json(myjson): From aac152ca7489f78582d9484d11e5a8ced84b9aa4 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Thu, 30 Dec 2021 21:36:04 +0000 Subject: [PATCH 3/5] src. exporter.py: Remove space behind Mbps --- src/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/exporter.py b/src/exporter.py index fd2ae89..a179a36 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -27,7 +27,7 @@ def bytes_to_bits(bytes_per_sec): def bits_to_megabits(bits_per_sec): megabits = round(bits_per_sec * (10**-6), 2) - return str(megabits) + " Mbps" + return str(megabits) + "Mbps" def is_json(myjson): From cd4f071f58f9f20a9919cb86836b5987de57cf92 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Thu, 30 Dec 2021 21:47:49 +0000 Subject: [PATCH 4/5] src: exporter.py: Enable logging --- src/exporter.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/exporter.py b/src/exporter.py index a179a36..c55497b 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -1,13 +1,25 @@ import subprocess import json -import datetime import os +import logging +import time from prometheus_client import make_wsgi_app, Gauge from flask import Flask from waitress import serve app = Flask("Speedtest-Exporter") # Create flask app + +# Setup logging values +format_string = 'level=%(levelname)s datetime=%(asctime)s %(message)s' +logging.basicConfig(encoding='utf-8', level=logging.DEBUG, + format=format_string) + +# Disable Waitress Logs +log = logging.getLogger('waitress') +log.disabled = True + + # Create Metrics server = Gauge('speedtest_server_id', 'Speedtest server ID used to test') jitter = Gauge('speedtest_jitter_latency_milliseconds', @@ -52,13 +64,12 @@ def runTest(): output = e.output if not is_json(output): if len(output) > 0: - print('Speedtest CLI Error occurred that' + - 'was not in JSON format') - print(output) + logging.error('Speedtest CLI Error occurred that' + + 'was not in JSON format') return (0, 0, 0, 0, 0, 0) except subprocess.TimeoutExpired: - print('Speedtest CLI process took too long to complete ' + - 'and was killed.') + logging.error('Speedtest CLI process took too long to complete ' + + 'and was killed.') return (0, 0, 0, 0, 0, 0) if is_json(output): @@ -90,11 +101,13 @@ def updateResults(): download_speed.set(r_download) upload_speed.set(r_upload) up.set(r_status) - current_dt = datetime.datetime.now() - print(current_dt.strftime("%d/%m/%Y %H:%M:%S - ") + "Server: " - + str(r_server) + " | Jitter: " + str(r_jitter) + " ms | Ping: " - + str(r_ping) + " ms | Download: " + bits_to_megabits(r_download) - + " | Upload:" + bits_to_megabits(r_upload)) + logging.info( + "Server=" + str(r_server) + + " Jitter=" + str(r_jitter) + "ms" + + " Ping=" + str(r_ping) + "ms" + + " Download=" + bits_to_megabits(r_download) + + " Upload=" + bits_to_megabits(r_upload) + ) return make_wsgi_app() @@ -106,5 +119,5 @@ def mainPage(): if __name__ == '__main__': PORT = os.getenv('SPEEDTEST_PORT', 9798) - print("Starting Speedtest-Exporter on http://localhost:" + str(PORT)) + logging.info("Starting Speedtest-Exporter on http://localhost:" + str(PORT)) serve(app, host='0.0.0.0', port=PORT) From e9c41453a60d66446a59ba739bad799b6e20f300 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Thu, 30 Dec 2021 21:53:39 +0000 Subject: [PATCH 5/5] exporter.py: Remove time module --- src/exporter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/exporter.py b/src/exporter.py index c55497b..1b4cda1 100644 --- a/src/exporter.py +++ b/src/exporter.py @@ -2,7 +2,6 @@ import json import os import logging -import time from prometheus_client import make_wsgi_app, Gauge from flask import Flask from waitress import serve