简体   繁体   中英

Problems to write data from python script into influxdb

I have problems with writing data into my influxdb this is the code from my python script. It's a ping script for looking if my devices in the local LAN. After that I can read the python file in the influxdb I want to visual this in Grafana

*import subprocess
import platform
from influxdb import InfluxDBClient

router= "192.168.1.1"
measurement = "ping"

def ping_ip(current_ip_address):
        try:
            output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower(
            ) == "windows" else 'c', current_ip_address ), shell=True, universal_newlines=True)
            if 'unreachable' in output:
                return False
            else:
                return True
        except Exception:
                return False

if __name__ == '__main__':
    current_ip_address = [router]
    for each in current_ip_address:
        if ping_ip(each):
            print(f"{each} is available")
        else:
            print(f"{each} is not available")




ping_data = [{

  "measurement" : "ping",
  "tags" : {
       "router" :router,
},

"fields" : {
  "ping": ping_ip
}
}
]


client = InfluxDBClient('localhost', 8086, 'ping', 'ping', 'ping')

client.write_points(ping_data)*

Can somebody help me what I doing wrong with this code ?

In your json you need to convert your function result to a string.

ping_data = [{

  "measurement" : "ping",
  "tags" : {
       "router" :router,
},

"fields" : {
  "ping": str(ping_ip)
}
}
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM