38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
#!/usr/bin/python3
|
|
import os
|
|
import subprocess as sp
|
|
import requests
|
|
import time
|
|
def addinfluxrecord(ip,pingtime):
|
|
data = f"PingTime,IP={ip} value={pingtime}"
|
|
print(data)
|
|
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
|
response = requests.post(
|
|
influxuri,
|
|
data=data,
|
|
headers=headers,
|
|
auth=(infdbuser,infdbpass),
|
|
)
|
|
return response
|
|
infdbuser = os.environ["INFLUX_USER"]
|
|
infdbpass = os.environ["INFLUX_PASSWORD"]
|
|
influxuri = os.environ["INFLUX_URI"]
|
|
pingaddresses = os.environ["PING_HOSTS"].split(",")
|
|
while(True):
|
|
if len(pingaddresses) > 0:
|
|
print(pingaddresses)
|
|
for ip in pingaddresses:
|
|
print(ip)
|
|
response = sp.getstatusoutput(f"ping -c 1 {ip}")
|
|
print(response)
|
|
if(response[0]!=0):
|
|
time=2000
|
|
else:
|
|
myresp = response[1].split("\n")[1].split(' ')[6].split('=')[1]
|
|
|
|
result = addinfluxrecord(ip,myresp)
|
|
print(result)
|
|
time.sleep(5)
|
|
else:
|
|
print("No Hosts Defined")
|
|
exit |