From b272f717ca5df87119e8f51960936c03d41ff57b Mon Sep 17 00:00:00 2001 From: imp4ct Date: Tue, 28 Jul 2020 16:51:36 -0400 Subject: [PATCH] Fixed uploading of metrics --- main.py | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 6e7eb61..0a3d346 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from influxdb import InfluxDBClient serverip = "192.168.0.13" serverport = 8086 serverdatabase = "telegraf" +servernickname = 'PythonSpeedtest' def speed(): # To be called on schedule servers = [] # If you want to test against a specific server @@ -22,24 +23,41 @@ def speed(): # To be called on schedule resultlist = [] - resultupload = {} - resultupload["UploadSpeed"] = results["upload"] - resultdownload = {} - resultdownload["DownloadSpeed"] = results["download"] - resultlatency = {} - resultlatency["Latency"] = results["ping"] - resultlist.append(resultupload) - resultlist.append(resultdownload) - resultlist.append(resultlatency) + result = {} + result["UploadSpeed"] = results["upload"] + result["DownloadSpeed"] = results["download"] + result["Ping"] = results["ping"] - resultforupload = json.dumps(resultlist) - print(resultforupload) - - return(resultforupload) + print(result) + return(result) def influxupload(): # Still having errors in uploading the data.. - jsondata = speed() - client = InfluxDBClient(host=serverip, port=8086, database=serverdatabase) - client.write(str(jsondata), protocol='json') + pass + # client = InfluxDBClient(host=serverip, port=8086, database=serverdatabase) + # client.write_points(jsondata, tags=None ) +def main(host='192.168.0.13', port=8086): + 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"] + } + } + ] + #"time": "2009-11-10T23:00:00Z", + client = InfluxDBClient(host, port, database = serverdatabase) + + client.write_points(json_body) -influxupload() \ No newline at end of file +# influxupload() +main() \ No newline at end of file