-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-connection.py
executable file
·87 lines (56 loc) · 1.82 KB
/
check-connection.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
import requests, re
from subprocess import Popen, PIPE
from time import sleep
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
### record: (host, max_latancy, raindow color (from 0 to 5))
hosts = [("provider-gateway", 3, 5), ("server-ip", 80, 4), ("foreign-public-server", 4, 1), ("public-server", 40, 2) ]
url="http://192.168.1.5/"
raindow = [ "8f00ff", "0000ff", "00ffff", "ffff00", "ff4a00", "ff0000"]
timeDelay = 15 # seconds between requests
############################################################
color = 0
prevColor = 0
def send(data, period):
try:
k = requests.get(url + 'set', params=data)
except:
pass
sleep(period)
def checkHost(host):
global color
ret = Popen("""ping -c 10 -i 0.2 -q -W 4 {0}| tail -n 2""".format(host[0]), shell=True, stdout=PIPE, stderr=PIPE)
# Get packets lost
try:
lost = re.search(r'(\d+)% packet loss', ret.stdout.readline().rstrip()).group(1)
if int(lost) > 20:
if color < host[2]:
color = host[2]
print host[0] + ' ' + lost
except:
pass
# Get latency
try:
latency = ret.stdout.readline().rstrip().split('/')[4]
if float(latency) > (2.0 * host[1]):
if color < host[2]:
color = host[2]
print host[0] + ' lat: ' + latency
except:
pass
while True:
pool = ThreadPool(10)
pool.map(checkHost, hosts)
pool.close()
pool.join()
print color
if color != prevColor:
payloadOff = {'rgb': '000000'}
send(payloadOff, 1.5)
payload = {'rgb': raindow[color]}
send(payload, 3)
prevColor = color;
color = 0
print
sleep(timeDelay)