-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatencyTester.py
More file actions
executable file
·46 lines (37 loc) · 1.4 KB
/
latencyTester.py
File metadata and controls
executable file
·46 lines (37 loc) · 1.4 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
import os, sys, time, datetime
import socket
def online(ping_destination):
ping_destination = ping_destination.replace("http://","")
HOST_UP = True if os.system("timeout 5 ping -c 1 -W 5 " + ping_destination) is 0 else False
return HOST_UP
def averagePing(ping_destination, amount) :
# SET YOUR PING RESPONSE TIME THRESHOLD HERE, IN MILLISECONDS
threshold = 1250
interval = 0.25
ping_destination = ping_destination.replace("http://","") # removing http:// from the string since its not supported in ping
count = 0
line = 'Ping Interval: ' + str(interval) + ', Destination: ' + ping_destination + ', Threshold to Log (msec): ' + str(threshold) + '\n'
ping_command = 'ping ' + ping_destination + ' -t 1'
# child = pexpect.spawn(ping_command)
child.timeout=400
pings = []
while 1:
line = child.readline()
if not line:
return False
if line.startswith(b'ping: unknown host'):
print('Unknown host: ' + ping_destination)
return False
if line.startswith(b'Alarm clock:'):
print('Unknown host: ' + ping_destination)
return False
if count > 0:
ping_time = float(line[line.find(b'time=') + 5:line.find(b' ms')])
line = time.strftime("%m/%d/%Y %H:%M:%S") + ": " + str(ping_time)
#print(str(count) + ": " + line)
pings.append(ping_time)
if ping_time > threshold:
print('Ping over threshold, disrupting.')
if(count >= amount):
return sum(pings) / float(len(pings))
count += 1