commit 94be2715a2f1506a4048799ed7c2cc9cb7c62361 Author: Sean Corrigan Date: Sat Nov 7 16:46:13 2020 -0500 I am speed diff --git a/__pycache__/influxlite.cpython-38.pyc b/__pycache__/influxlite.cpython-38.pyc new file mode 100644 index 0000000..a8144fa Binary files /dev/null and b/__pycache__/influxlite.cpython-38.pyc differ diff --git a/influxlite.py b/influxlite.py new file mode 100644 index 0000000..d730488 --- /dev/null +++ b/influxlite.py @@ -0,0 +1,35 @@ +## Sean Corrigan 2020 +# Easy lite API to use in my other scripts +# to send data to my influx server. + +from influxdb import InfluxDBClient + +def createFields(datadict): + data = [] + for i,v in datadict.items(): + data.append("\"{}\": {}\n".format(i,v)) + return ''.join(data) + + +def uploadData(host, database, measurementname, datadict, nickname = 'pythontestscript', port=8086): ## Push data to Influx server + fields = createFields(datadict) + bind_params = {'host': servernickname} + + json_body = [ + { + "measurement": measurementname, + "tags": { + "host": servernickname, + }, + + "fields": { + createFields(datadict), + } + } + ] + + client = InfluxDBClient(host, port, database) # Init connection to Influx Server + client.write_points(json_body) # Write Speedtest results + +if if __name__ == "__main__": + uploadData(host='192.168.0.13', database='telegraf', ) \ No newline at end of file diff --git a/speedbeta.py b/speedbeta.py new file mode 100644 index 0000000..3c90e63 --- /dev/null +++ b/speedbeta.py @@ -0,0 +1,58 @@ +# Sean Corrigan 2020 +# Script sends data to influx in bits/sec + +import speedtest +import json +from influxdb import InfluxDBClient + +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 + +uploadInfluxdata()