-
Notifications
You must be signed in to change notification settings - Fork 0
/
conncheck.py
104 lines (94 loc) · 2.87 KB
/
conncheck.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import socket, thread, time, select, sys
ROUTER="192.168.1.1"
TEST_URL="/images/empty.gif"
DEBUG=False
SOCK_TIMEOUT=5.0
STAT_FILE = "/run/conncheck.dat"
LOG_FILE = "/run/conncheck.log"
def now():
return int(round(time.time()))
def log(msg):
try:
open(LOG_FILE,"a").write(time.strftime("%D-%T:",time.localtime())+msg+"\n")
except:
print "LOG FAIL",msg
def test_connect(hp, timeout=2.0):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect(hp)
except:
return 0, now()
return 1, now()
def test_http(host=ROUTER,url=TEST_URL,port=80,timeout=SOCK_TIMEOUT):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((host,port))
except:
if DEBUG: print "CONNECT FAIL"
return 0, now()
s.send("GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % (url,host))
later = now() + timeout
response = ''
while now() < later:
r,w,e = select.select([s],[],[],1)
if s in r:
response += s.recv(1000)
if response.startswith("HTTP"):
return 1, now()
if DEBUG: print "NO RESPONSE"
return 0,int(round(now()))
if __name__ == '__main__':
import sys
DEBUG = "-debug" in sys.argv
worked, t = test_http()
if DEBUG: print worked,t
try:
# status file is: FAIL_TIME FIRST_FAILTIME FAIL_CNT succeed... toggle
stat = map(int,open(STAT_FILE,'rb').read().split(" "))
except:
stat = None
if stat is None or len(stat) != 9:
if worked:
stat = [0,0,0, t,t,0, 0,0,0]
else:
stat = [t,t,0, 0,0,0, 0,0,0]
if DEBUG: print "STAT", stat
logit = False
before = list(stat)
if worked:
stat[2] = 0
if stat[5] == 0:
stat[4] = t
logit = True
stat[5] += 1
stat[3] = t
else:
stat[5] = 0
if stat[2] == 0:
stat[1] = t
logit = True
stat[2] += 1
stat[0] = t
if DEBUG: print "STAT", stat
if logit: log(repr(before)+" --> "+repr(stat))
if stat[2] > 2 and (stat[0]-stat[1]) > 45:
# more than 2 failures in a row for more than 45 seconds
tt = now()
if tt - stat[7] > 300: # only toggle every 5 minutes
print "TOGGLE ROUTER"
log("TOGGLE_ROUTER")
try:
import toggle
toggle.toggle(1, 2.0)
if stat[8] == 0:
stat[7] = tt
stat[8] += 1
stat[7] = tt
except:
print "TOGGLE FAIL"
log("TOGGLE_FAIL")
else:
print "TOGGLE SKIP too soon",tt
open(STAT_FILE,'wb').write("%d %d %d %d %d %d %d %d %d" % tuple(stat))