Fixed uploading of metrics

This commit is contained in:
Sean Corrigan 2020-07-28 16:51:36 -04:00
parent 1ef27c096e
commit b272f717ca

52
main.py
View File

@ -6,6 +6,7 @@ from influxdb import InfluxDBClient
serverip = "192.168.0.13" serverip = "192.168.0.13"
serverport = 8086 serverport = 8086
serverdatabase = "telegraf" serverdatabase = "telegraf"
servernickname = 'PythonSpeedtest'
def speed(): # To be called on schedule def speed(): # To be called on schedule
servers = [] # If you want to test against a specific server servers = [] # If you want to test against a specific server
@ -22,24 +23,41 @@ def speed(): # To be called on schedule
resultlist = [] resultlist = []
resultupload = {} result = {}
resultupload["UploadSpeed"] = results["upload"] result["UploadSpeed"] = results["upload"]
resultdownload = {} result["DownloadSpeed"] = results["download"]
resultdownload["DownloadSpeed"] = results["download"] result["Ping"] = results["ping"]
resultlatency = {}
resultlatency["Latency"] = results["ping"]
resultlist.append(resultupload)
resultlist.append(resultdownload)
resultlist.append(resultlatency)
resultforupload = json.dumps(resultlist) print(result)
print(resultforupload) return(result)
return(resultforupload)
def influxupload(): # Still having errors in uploading the data.. def influxupload(): # Still having errors in uploading the data..
jsondata = speed() pass
client = InfluxDBClient(host=serverip, port=8086, database=serverdatabase) # client = InfluxDBClient(host=serverip, port=8086, database=serverdatabase)
client.write(str(jsondata), protocol='json') # 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() # influxupload()
main()