Kevin Roggeman
Kevin Roggeman

Reputation: 19

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 ?

Upvotes: 1

Views: 296

Answers (1)

Razorneck
Razorneck

Reputation: 65

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)
}
}
]

Upvotes: 0

Related Questions