-
Notifications
You must be signed in to change notification settings - Fork 4
/
ping.py
34 lines (28 loc) · 1.16 KB
/
ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import subprocess
import time
import re
import os
from influxdb import InfluxDBClient
hosts = os.environ.get('HOSTS_TO_PING').split(',')
ping_interval_seconds = int(os.environ.get('PING_INTERVAL_SECONDS'))
print(hosts)
influxdb = InfluxDBClient(host='influxdb', port=8086, database='internet')
def insert_ping(host, latency):
influxdb.write_points([{'measurement': "ping", 'tags': {'host': host}, 'fields': {'latency': latency}}])
while True:
for host in hosts:
p = subprocess.Popen(['ping', '-q', '-c', '1', '-W', '1', host], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
output = output.decode("utf-8")
if p.returncode == 1 and '0 received' in output:
latency = 0.0
elif p.returncode == 0 and '1 received' in output:
latency_pattern = r"rtt min\/avg\/max\/mdev = ([^\/]+)"
latency = float(re.findall(latency_pattern, output).pop())
else:
print(p.returncode)
print(output)
raise NotImplementedError('Unknown state')
print(host, latency)
insert_ping(host, latency)
time.sleep(ping_interval_seconds)