forked from operasoftware/tlsprober
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue_setup.py
267 lines (210 loc) · 8.29 KB
/
queue_setup.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Copyright 2010-2012 Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Create or update a preconfigured list of servers, that can later be
used for a cluster job
Default is to copy all enabled hostnames from the server list in
the database.
CSV formats
index, hostname, port, protocol
index, hostname, port # HTTPS assumed
index, hostname # Port 443, HTTPS assumed; line starts with number
hostname, port # HTTPS assumed; line starts with letter; port may be empty
Options:
--input <name> : CSV File containing a list of hosts
--file : Do not use the server name list, only the provided list
--count <num> : Only configure max <num> servers from the source
--run-id <num> : ID of a job, use the server names used by this job's queue as the source
--name <name> : Name of preconfigured list
--description <text> : Description of the list
--queue-id <num> : Database record ID for pre-configured list of hostnames to be updated
--verbose : Print more information
--testbase2 : use the debug test database for this job
--threads <num> : Use the specified number of threads when inserting the entries for the job
"""
from optparse import OptionParser
import probedb.standalone
import probedb.cluster.models as Cluster
import probedb.probedata2.models as Probe
import probedb.resultdb2.models as Results
import fileinput
from django.db import connection
import datetime
import threading
from multiprocessing import Process, JoinableQueue as Queue, Lock
from django.db import transaction
def __ProgressCounter(queue, threads, options):
i=0
while True:
queue.get()
i += 1
if i%100 == 0:
if options.verbose:
print "Queued ", i, "servers so far. Threads active ", sum([t.is_alive() for t in threads])
queue.task_done()
def SetupQueueThread(tid, target, probe_servers, progress_queue, ):
connection.close()
try:
while True:
item = probe_servers.get()
if isinstance(item, int):
try:
item = Probe.Server.objects.get(id=item)
except:
probe_servers.task_done()
continue
hostname = item.servername.strip()
if (not hostname or
any([x in hostname for x in " \t%/&#\"'\\{[]}()*,;<>$"]) or
any([ord(x)>=128 or ord(x)<=32 for x in hostname])):
item.enabled = False
item.save()
probe_servers.task_done()
continue
hostname = hostname.strip(".")
while hostname.find("..")>=0:
hostname = hostname.replace("..", ".")
if hostname != item.servername:
item.enabled = False
item.save()
item = "0," + hostname+","+str(item.port)+","+item.protocol # Convert to string to correct the list
if not isinstance(item, Probe.Server):
hostname_line = item
if not hostname_line.strip():
probe_servers.task_done()
continue
split_line = hostname_line.strip().split(",")
if len(split_line) > 3:
(index, hostname, port, protocol) = split_line[:4]
if not protocol:
protocol = Probe.Server.PROTOCOL_PORT.get(port if port else 443, Probe.Server.PROTOCOL_HTTPS)
elif len(split_line) > 2:
(index, hostname, port) = split_line[:3]
protocol = Probe.Server.PROTOCOL_PORT.get(port if port else 443, Probe.Server.PROTOCOL_HTTPS)
else:
port = ""
(var1, var2) = split_line
if var1.isdigit():
(index, hostname) = (var1, var2)
else:
(hostname, port) = (var1, var2)
protocol = Probe.Server.PROTOCOL_PORT.get(port if port else 443, Probe.Server.PROTOCOL_HTTPS)
hostname = hostname.strip()
if (not hostname or
any([x in hostname for x in " \t%/&#\"'\\{[]}()*,;<>$"]) or
any([ord(x)>=128 or ord(x)<=32 for x in hostname])):
probe_servers.task_done()
continue
hostname = hostname.strip(".")
while hostname.find("..")>=0:
hostname = hostname.replace("..", ".")
if not port:
port = 443
else:
port = int(port)
sn_t = "%s:%05d:%s" % (hostname, port,protocol)
(item, created) = Probe.Server.objects.get_or_create(
full_servername = sn_t,
defaults={'enabled':True,
"alexa_rating":0,
"servername":hostname,
"port": port,
"protocol":protocol,
}
)
if created:
item.Construct()
if item.enabled:
try:
sid = transaction.savepoint()
queue_entry = Probe.PreparedQueueItem.objects.create(part_of_queue=target,server=item)
progress_queue.put(True)
transaction.savepoint_commit(sid)
except:
transaction.savepoint_rollback(sid)
pass
probe_servers.task_done()
except:
raise
pass
def setup_queue(options):
probe_servers = Queue()
progress_queue = Queue()
if options.queue_id:
queue_list = Probe.PreparedQueueList.objects.get(id=options.queue_id)
else:
queue_name=options.queue_name.strip('"').strip()
queue_list,created = Probe.PreparedQueueList.objects.get_or_create(
list_name=queue_name,
defaults = dict(list_description=options.description.strip('"').strip()),
)
if options.run_id:
run = Probe.ProbeRun.objects.get(id=options.run_id)
cursor = connection.cursor()
cursor.execute("""INSERT INTO probedata2_preparedqueueitem (part_of_queue_id, server_id)
SELECT %s AS part_of_queue_id, server_id FROM probedata2_probequeue
WHERE part_of_run_id = %s""", [str(queue_list.id),str(run.id)]
)
transaction.commit_unless_managed()
return queue_list
connection.close()
threads = []
for i in range(options.threads):
new_thread = Process(target=SetupQueueThread, args=(i,queue_list, probe_servers, progress_queue))
new_thread.daemon = True
new_thread.start()
threads.append(new_thread)
progress_thread = threading.Thread(target=__ProgressCounter, args=(progress_queue, threads,options))
progress_thread.daemon = True
progress_thread.start()
i = 0;
if not options.file_list_only:
for host in Probe.Server.objects.filter(enabled = True).values_list("id",flat=True):
probe_servers.put(host)
i+=1
if options.count and i >= options.count:
break;
if options.input_filename and (not options.count or i < options.count):
for hostname_line in fileinput.input(options.input_filename, openhook=fileinput.hook_compressed):
probe_servers.put(hostname_line)
i+=1
if options.count and i >= options.count:
break;
probe_servers.join()
progress_queue.join()
return queue_list
def main():
options_config = OptionParser()
options_config.add_option("--input", action="store", type="string", dest="input_filename", default="testlist.csv")
options_config.add_option("--id", action="store", type="int", dest="queue_id")
options_config.add_option("--name", action="store", type="string", dest="queue_name")
options_config.add_option("--description", action="store", type="string", dest="description")
options_config.add_option("--run-id", action="store", type="int", dest="run_id")
options_config.add_option("--file", action="store_true", dest="file_list_only")
options_config.add_option("--testbase2", action="store_true", dest="use_testbase2")
options_config.add_option("--threads", action="store", type="int", dest="threads", default=30)
options_config.add_option("--verbose", action="store_true", dest="verbose")
options_config.add_option("--count", action="store", type="int", dest="count", default=0)
(options, args) = options_config.parse_args()
if not options.queue_id and not options.queue_name:
raise Exception("Need a name or an id")
started = datetime.datetime.now()
queue = setup_queue(options)
if options.verbose:
print "Queue %d for %s/%s has been initiated. %d items" %(queue.id, queue.list_name, queue.list_description, Probe.PreparedQueueItem.objects.filter(part_of_queue=queue).count())
ended = datetime.datetime.now()
if options.verbose:
print "Time to run: ", (ended-started)
if __name__ == "__main__":
main()