Skip to content

Instantly share code, notes, and snippets.

@timku
Last active July 16, 2024 15:41
Show Gist options
  • Save timku/b298f252c6bfa2d0d956805f9a2ab854 to your computer and use it in GitHub Desktop.
Save timku/b298f252c6bfa2d0d956805f9a2ab854 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import datetime
from dateutil import parser
import subprocess
import json
from influxdb import InfluxDBClient
# influx configuration - edit these
ifuser = "telegraf"
ifpass = "_password_here_"
ifdb = "telegraf"
ifhost = "127.0.0.1"
ifport = 8086
measurement_name = "speedtest"
data = subprocess.check_output(
"speedtest -f json",
stderr=subprocess.STDOUT,
shell=True)
jsondata = json.loads(data)
res = {}
res["timestamp"] = jsondata["timestamp"]
res["download"] = jsondata["download"]["bandwidth"]/125000
res["upload"] = jsondata["upload"]["bandwidth"]/125000
res["ping"] = jsondata["ping"]["latency"]
res["client"] = {}
res["client"]["isp"] = jsondata["isp"]
res["client"]["ip"] = jsondata["interface"]["externalIp"]
res["share"] = jsondata["result"]["url"]
res["server"] = {}
res["server"]["url"] = jsondata["server"]["host"]
res["server"]["sponsor"] = jsondata["server"]["name"]
body = [
{
"measurement": measurement_name,
"time": res["timestamp"],
"fields": {
"download": res["download"],
"upload": res["upload"],
"ping": res["ping"]
}
}
]
# connect to influx
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb)
# write the measurement
ifclient.write_points(body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment