-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchecksurrogate.py
154 lines (133 loc) · 5.44 KB
/
checksurrogate.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cgi
import os
import socket
import datetime
import urllib2
import logging
import helptool
from models import Surrogate
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
def is_dev():
return os.environ.get('SERVER_SOFTWARE', '').startswith('Development')
class CheckSurrogate(webapp.RequestHandler):
def get(self):
socket.timeout(1)
check_server_count = 0
check_period = 600
mirrordelay = 86400
t1 = datetime.datetime.now()
lm = ''
dm = ''
ttmp = datetime.datetime.now()
keika = ttmp - t1
message = ''
surrogates = Surrogate.all().order('checkpref').order('time')
for surrogate in surrogates:
if check_server_count >= 100 and not is_dev():
break
if surrogate.checkpref > 150 and not is_dev():
continue
ttmp = datetime.datetime.now()
keika = ttmp - t1
if keika > datetime.timedelta(0,20) and not is_dev():
break
if surrogate.checkpref:
surrogate.checkpref += int(surrogate.checkpref)
else:
surrogate.checkpref = 0
if surrogate.tracefile:
True
else:
tracefile = 'ftp-master.debian.org'
if surrogate.type == "CNAME":
check_server_count += 1
dm = 'go check'
tf, lmt = helptool.delegateForCname(surrogate.ip)
if tf:
message = surrogate.ip + " is alive (CNAME host)."
else:
message = surrogate.ip + " is dead (CNAME host)."
logging.info(message)
surrogate.lastModifiedTime = lmt
if surrogate.time - lmt > datetime.timedelta(0,mirrordelay):
surrogate.alive = False
surrogate.checkpref += 1
surrogate.failreason = "DELAY"
else:
surrogate.alive = True
surrogate.checkpref = 0
surrogate.failreason = ""
surrogate.put()
elif surrogate.alive == None or t1 > surrogate.time + datetime.timedelta(0,check_period) or is_dev(): #remote_addr == "127.0.0.1":
check_server_count += 1
dm = 'go check'
k = surrogate.ip
req = urllib2.Request(url="http://" + k + '/debian/project/trace/' + tracefile)
req.add_header('User-Agent',"Debian-cdn-mirror-ping/1.5")
try:
f = urllib2.urlopen(req)
lm = f.info()['Last-Modified']
message = ''
lmt = datetime.datetime.strptime(lm, "%a, %d %b %Y %H:%M:%S GMT")
surrogate.lastModifiedTime = lmt
if surrogate.time - lmt > datetime.timedelta(0,mirrordelay):
surrogate.alive = False
surrogate.checkpref += 1
surrogate.failreason = "DELAY"
else:
surrogate.alive = True
surrogate.checkpref = 0
surrogate.failreason = ""
except urllib2.HTTPError, e:
message += "%s is not working. (HTTP error)" % (k)
surrogate.alive = False
surrogate.checkpref += 1
surrogate.failreason = "E:HTTP"
logging.info(message)
except urllib2.URLError, e:
message += "%s is not working. (URL error)" % (k)
surrogate.alive = False
surrogate.checkpref += 1
surrogate.failreason = "E:URL"
logging.info(message)
except:
message += "%s is not working. " % (k)
surrogate.alive = False
surrogate.checkpref += 1
surrogate.failreason = "E:NO WORK"
logging.info(message)
surrogate.put()
else:
dm = 'no check: ' + "%s is checked less than %d sec ago (surrogate.time %s, %s)" % (surrogate.ip,check_period,surrogate.time,t1)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
t2 = datetime.datetime.now()
keika = t2 - t1
template_values = {
'surrogates': surrogates,
'url': url,
'url_linktext': url_linktext,
'message': message,
'lm':lm,
'dm':dm,
'keika':keika,
}
path = os.path.join(os.path.dirname(__file__), 'managesurrogate.html')
self.response.out.write(template.render(path, template_values))
application = webapp.WSGIApplication(
[('/checksurrogate', CheckSurrogate)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()