From 7201cc5371cc4c7e1ada82534eccb74186dde1d6 Mon Sep 17 00:00:00 2001 From: imp4ct Date: Mon, 5 Oct 2020 21:20:49 -0400 Subject: [PATCH] Added dockerfile and basic looping main.py to static IP --- Dockerfile | 8 +++++++ main.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Dockerfile create mode 100644 main.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..41428b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3 + +ADD main.py / + +run pip install speedtest-cli +run pip install influxdb + +CMD [ "python", "./main.py" ] diff --git a/main.py b/main.py new file mode 100644 index 0000000..4f8124a --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +# Sean Corrigan 2020 +# Script sends data to influx in bits/sec + +import speedtest +import json +from influxdb import InfluxDBClient +from time import time, sleep + +serverip = "192.168.0.13" +serverport = 8086 +serverdatabase = "telegraf" +servernickname = 'Yoo01pn.ddns.net' + +def speed(): # Actual Speedtest using speedtest-cli + servers = ['19249'] # If you want to test against a specific server eg. ['13030'] or [] for closest server + threads = 4 + # Choose the amount of threads to use for the test + test = speedtest.Speedtest() + test.get_servers(servers) + test.get_best_server() + test.download(threads=threads) + test.upload(threads=threads) + test.results.share() + + results = test.results.dict() + + + result = {} + result["UploadSpeed"] = results["upload"] + result["DownloadSpeed"] = results["download"] + result["Ping"] = results["ping"] + + print(results["share"]) # Show share link + return(result) + +def uploadInfluxdata(host='192.168.0.13', port=8086): # Main upload section + query = 'select Float_value from cpu_load_short;' + query_where = 'select Int_value from cpu_load_short where host=$host;' + bind_params = {'host': servernickname} + testdata = speed() + json_body = [ + { + "measurement": "PythonSpeedTest", + "tags": { + "host": servernickname, + }, + + "fields": { + "Upload": testdata["UploadSpeed"], + "Download": testdata["DownloadSpeed"], + "Ping": testdata["Ping"] + } + } + ] + + client = InfluxDBClient(host, port, database = serverdatabase) # Init connection to Influx Server + client.write_points(json_body) # Write Speedtest results + +while True: + print("Running Test") + uploadInfluxdata() + sleep(600) + +