-
Notifications
You must be signed in to change notification settings - Fork 7
/
cluster_setup_add.py
221 lines (168 loc) · 6.19 KB
/
cluster_setup_add.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
# 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.
"""
Add new servername entries to an existing run
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
--count <num> : Only configure max <num> servers from the source
--run-id <num> : add the new entries to this job
--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, transaction
import datetime
import threading
from multiprocessing import Process, JoinableQueue as Queue, Lock
def __ProgressCounter(run, 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, run, 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) # 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) > 2:
(index, hostname, port) = split_line[:3]
else:
port = ""
(var1, var2) = split_line
if var1.isdigit():
(index, hostname) = (var1, var2)
else:
(hostname, port) = (var1, var2)
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" % (hostname, port)
(item, created) = Probe.Server.objects.get_or_create(
full_servername = sn_t,
defaults={'enabled':True,
"alexa_rating":0,
"servername":hostname,
"port": port,
}
)
if created:
item.Construct()
if item.enabled:
try:
sid = transaction.savepoint()
run_entry = Probe.ProbeQueue.objects.create(part_of_run=run,server=item,state=Probe.ProbeQueue.PROBEQ_IDLE)
transaction.savepoint_commit(sid)
progress_queue.put(True)
except:
transaction.savepoint_rollback(sid)
pass
probe_servers.task_done()
except:
pass
def setup_queue(options):
probe_servers = Queue()
progress_queue = Queue()
run = Probe.ProbeRun.objects.get(id = options.run_id)
summary_top = Results.ResultSummaryList.objects.get(part_of_run=run)
summary_top.setup()
connection.close()
threads = []
for i in range(options.threads):
new_thread = Process(target=SetupQueueThread, args=(i,run, probe_servers, progress_queue))
new_thread.daemon = True
new_thread.start()
threads.append(new_thread)
progress_thread = threading.Thread(target=__ProgressCounter, args=(run, progress_queue, threads,options))
progress_thread.daemon = True
progress_thread.start()
i = 0;
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 run
def main():
options_config = OptionParser()
options_config.add_option("--input", action="store", type="string", dest="input_filename", default="testlist.csv")
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_config.add_option("--run-id", action="store", type="int", dest="run_id", default=0)
(options, args) = options_config.parse_args()
started = datetime.datetime.now()
run = setup_queue(options)
if options.verbose:
print "Run %d for %s/%s has been updated with more items. Total %d items" %(run.id, run.source_name, run.description, Probe.ProbeQueue.objects.filter(part_of_run=run).count())
ended = datetime.datetime.now()
if options.verbose:
print "Time to run: ", (ended-started)
if __name__ == "__main__":
main()