質問編集履歴
1
ソースを変更しました。求めている内容を記載しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,18 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
※ソースを記載しました。データイメージと記述内容が異なっていますが、読み替えてくれると助かります。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
やりたいことは、愚直にfor文で分類分けしているロジックをもっと効率する方法はないか。
|
18
|
+
|
19
|
+
現状だと数万件のデータが来たらかなり遅い処理になると予想しているので改善したいのです。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
大変恐縮ですが、アドバイスいただけると助かります。
|
24
|
+
|
13
25
|
|
14
26
|
|
15
27
|
###データイメージ
|
@@ -52,7 +64,7 @@
|
|
52
64
|
|
53
65
|
|
54
66
|
|
55
|
-
###ソース
|
67
|
+
###ソース
|
56
68
|
|
57
69
|
```pyhon3
|
58
70
|
|
@@ -66,107 +78,281 @@
|
|
66
78
|
|
67
79
|
"""
|
68
80
|
|
69
|
-
#
|
81
|
+
# dos
|
70
|
-
|
82
|
+
|
71
|
-
chk = re.match(r"^
|
83
|
+
chk = re.match(r"^IPBLOCK-*", value)
|
72
|
-
|
84
|
+
|
73
|
-
if chk:
|
85
|
+
if chk:
|
74
|
-
|
86
|
+
|
75
|
-
return '
|
87
|
+
return '4'
|
88
|
+
|
89
|
+
|
90
|
+
|
76
|
-
|
91
|
+
# bot manager
|
92
|
+
|
77
|
-
chk = re.match(r"^
|
93
|
+
chk = re.match(r"^39", value)
|
78
94
|
|
79
95
|
if chk:
|
80
96
|
|
81
97
|
return '2'
|
82
98
|
|
99
|
+
chk = re.match(r"^BOT-", value)
|
100
|
+
|
83
|
-
|
101
|
+
if chk:
|
102
|
+
|
103
|
+
return '2'
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
# client reputation
|
108
|
+
|
109
|
+
chk = re.match(r"^REP_", value)
|
110
|
+
|
111
|
+
if chk:
|
112
|
+
|
113
|
+
return '3'
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# API Protection
|
118
|
+
|
119
|
+
chk = re.match(r"^API_", value)
|
120
|
+
|
121
|
+
if chk:
|
122
|
+
|
123
|
+
return '6'
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
# UA filter
|
128
|
+
|
129
|
+
chk = re.match(r"^6", value)
|
130
|
+
|
131
|
+
if chk:
|
132
|
+
|
133
|
+
return '7'
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
# no match
|
138
|
+
|
139
|
+
return '1'
|
84
140
|
|
85
141
|
|
86
142
|
|
87
143
|
def log_parser():
|
88
144
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
log
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
rul
|
116
|
-
|
117
|
-
rul
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
i
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
g
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
#
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
145
|
+
test_file = 'C:\tmp/sample_apache_log.txt'
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
reader = pd.read_csv(test_file, sep='\n', chunksize=50,header=None)
|
150
|
+
|
151
|
+
for row in reader:
|
152
|
+
|
153
|
+
regex = '([(\d.)]+) - - [(.*?)] "(\S+?)(?: +(.*?) +(\S*?))?" (\d+) (\d+) "(.*?)" "(.*?)" "(.*?)" "(.*?)"'
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
pd_result=row[0].str.extract(regex, expand=True)
|
158
|
+
|
159
|
+
#
|
160
|
+
|
161
|
+
print(pd_result.at[0,0])
|
162
|
+
|
163
|
+
print(pd_result.at[0,1])
|
164
|
+
|
165
|
+
print(pd_result.at[0,2])
|
166
|
+
|
167
|
+
print(pd_result.at[0,3])
|
168
|
+
|
169
|
+
print(pd_result.at[0,4])
|
170
|
+
|
171
|
+
print(pd_result.at[0,5])
|
172
|
+
|
173
|
+
print(pd_result.at[0,6])
|
174
|
+
|
175
|
+
print(pd_result.at[0,7])
|
176
|
+
|
177
|
+
print(pd_result.at[0,8])
|
178
|
+
|
179
|
+
print(pd_result.at[0,9])
|
180
|
+
|
181
|
+
print(pd_result.at[0,10]) #分類分けデータ
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
# policyID,ruleID,riskName
|
186
|
+
|
187
|
+
pd_result[[11,12,13]] = pd_result[10].str.split('|', expand=True)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
# 複数カラムにまたがる処理を愚直にforでやってる(todo:改善したい)
|
192
|
+
|
193
|
+
for i, row in pd_result.iterrows():
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
log_kind = 0 # init
|
198
|
+
|
199
|
+
if row[12] == [''] and row[13] == [''] :
|
200
|
+
|
201
|
+
log_kind = 5 # Access_log
|
202
|
+
|
203
|
+
print (row[10] + 'is :' +log_kind)
|
204
|
+
|
205
|
+
#pd_result.at[i, 14] = log_kind # ★★要デバッグ
|
206
|
+
|
207
|
+
continue
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
# RuleID
|
212
|
+
|
213
|
+
ruleID = row[12].split(':')
|
214
|
+
|
215
|
+
ruleID_dir = []
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
if ruleID == ['']:
|
220
|
+
|
221
|
+
pass
|
222
|
+
|
223
|
+
else :
|
224
|
+
|
225
|
+
for rID in ruleID:
|
226
|
+
|
227
|
+
log_code = logDistribution(str(rID))
|
228
|
+
|
229
|
+
ruleID_dir += [log_code]
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
# Risk Group Name
|
234
|
+
|
235
|
+
riskName = row[13].split(':')
|
236
|
+
|
237
|
+
riskName_dir = []
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
if riskName == ['']:
|
242
|
+
|
243
|
+
pass
|
244
|
+
|
245
|
+
else:
|
246
|
+
|
247
|
+
for rName in riskName:
|
248
|
+
|
249
|
+
log_code = logDistribution(str(rName))
|
250
|
+
|
251
|
+
riskName_dir += [log_code]
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
# ログの分割
|
256
|
+
|
257
|
+
Aggregation_dir = []
|
258
|
+
|
259
|
+
if not ruleID_dir:
|
260
|
+
|
261
|
+
pass
|
262
|
+
|
263
|
+
else:
|
264
|
+
|
265
|
+
for i in range(len(ruleID_dir)):
|
266
|
+
|
267
|
+
tmp_dir = [ruleID_dir[i],ruleID[i],'',row[11]]
|
268
|
+
|
269
|
+
if i == 0:
|
270
|
+
|
271
|
+
Aggregation_dir.append(tmp_dir)
|
272
|
+
|
273
|
+
else :
|
274
|
+
|
275
|
+
# すでにAggregation_dirに登録されてる分類コードなら:で連結する
|
276
|
+
|
277
|
+
merge_flg = False
|
278
|
+
|
279
|
+
for j in range(len(Aggregation_dir)):
|
280
|
+
|
281
|
+
if Aggregation_dir[j][0] == tmp_dir[0]:
|
282
|
+
|
283
|
+
if not Aggregation_dir[j][1]:
|
284
|
+
|
285
|
+
Aggregation_dir[j][1] = tmp_dir[1]
|
286
|
+
|
287
|
+
merge_flg = True
|
288
|
+
|
289
|
+
else:
|
290
|
+
|
291
|
+
Aggregation_dir[j][1] = Aggregation_dir[j][1] + ':' + tmp_dir[1]
|
292
|
+
|
293
|
+
merge_flg = True
|
294
|
+
|
295
|
+
if merge_flg == False :
|
296
|
+
|
297
|
+
Aggregation_dir.append(tmp_dir)
|
298
|
+
|
299
|
+
if not riskName_dir:
|
300
|
+
|
301
|
+
pass
|
302
|
+
|
303
|
+
else:
|
304
|
+
|
305
|
+
for i in range(len(riskName_dir)):
|
306
|
+
|
307
|
+
tmp_dir = [riskName_dir[i],'',riskName[i],row[11]]
|
308
|
+
|
309
|
+
if i == 0 and not Aggregation_dir:
|
310
|
+
|
311
|
+
Aggregation_dir.append(tmp_dir)
|
312
|
+
|
313
|
+
else :
|
314
|
+
|
315
|
+
# すでにAggregation_dirに登録されてる分類コードなら:で連結する
|
316
|
+
|
317
|
+
merge_flg = False
|
318
|
+
|
319
|
+
for j in range(len(Aggregation_dir)):
|
320
|
+
|
321
|
+
if Aggregation_dir[j][0] == tmp_dir[0]:
|
322
|
+
|
323
|
+
if not Aggregation_dir[j][2]:
|
324
|
+
|
325
|
+
Aggregation_dir[j][2] = tmp_dir[2]
|
326
|
+
|
327
|
+
merge_flg = True
|
328
|
+
|
329
|
+
else:
|
330
|
+
|
331
|
+
Aggregation_dir[j][2] = Aggregation_dir[j][2] + ':' + tmp_dir[2]
|
332
|
+
|
333
|
+
merge_flg = True
|
334
|
+
|
335
|
+
if merge_flg == False :
|
336
|
+
|
337
|
+
Aggregation_dir.append(tmp_dir)
|
338
|
+
|
339
|
+
print(Aggregation_dir)
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
# Aggregation_dirで増えたレコードをもとのpandasに追加
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
if __name__ == "__main__":
|
352
|
+
|
353
|
+
# ログをファイルから読み込み
|
354
|
+
|
355
|
+
log_parser()
|
170
356
|
|
171
357
|
|
172
358
|
|