Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import logging
import datetime
from prometheus_client import make_wsgi_app, Gauge
from prometheus_client import make_wsgi_app, Counter, Gauge
from flask import Flask
from waitress import serve
from shutil import which
Expand All @@ -30,6 +30,10 @@
'Speedtest current Download Speed in bit/s')
upload_speed = Gauge('speedtest_upload_bits_per_second',
'Speedtest current Upload speed in bits/s')
download_usage = Counter('speedtest_total_download_mebibytes',
'Speedtest total data downloaded in MiB')
upload_usage = Counter('speedtest_total_upload_mebibytes',
'Speedtest total data uploaded in MiB')
up = Gauge('speedtest_up', 'Speedtest status whether the scrape worked')

# Cache metrics for how long (seconds)?
Expand All @@ -40,6 +44,8 @@
def bytes_to_bits(bytes_per_sec):
return bytes_per_sec * 8

def bytes_to_mebibytes(bytes):
return bytes / 1024 / 1024

def bits_to_megabits(bits_per_sec):
megabits = round(bits_per_sec * (10**-6), 2)
Expand Down Expand Up @@ -94,22 +100,26 @@ def runTest():
actual_ping = data['ping']['latency']
download = bytes_to_bits(data['download']['bandwidth'])
upload = bytes_to_bits(data['upload']['bandwidth'])
download_size = bytes_to_mebibytes(data['download']['bytes'])
upload_size = bytes_to_mebibytes(data['upload']['bytes'])
return (actual_server, actual_jitter, actual_ping, download,
upload, 1)
upload, download_size, upload_size, 1)


@app.route("/metrics")
def updateResults():
global cache_until

if datetime.datetime.now() > cache_until:
r_server, r_jitter, r_ping, r_download, r_upload, r_status = runTest()
r_server, r_jitter, r_ping, r_download, r_upload, r_download_size, r_upload_size, r_status = runTest()
server.set(r_server)
jitter.set(r_jitter)
ping.set(r_ping)
download_speed.set(r_download)
upload_speed.set(r_upload)
up.set(r_status)
download_usage.inc(r_download_size)
upload_usage.inc(r_upload_size)
logging.info("Server=" + str(r_server) + " Jitter=" + str(r_jitter) +
"ms" + " Ping=" + str(r_ping) + "ms" + " Download=" +
bits_to_megabits(r_download) + " Upload=" +
Expand Down