-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClustering_analysis_Aug_20.py
More file actions
376 lines (207 loc) · 6.53 KB
/
Copy pathClustering_analysis_Aug_20.py
File metadata and controls
376 lines (207 loc) · 6.53 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import sys
from AccessLog import *
import json
data = toJson("log-jtmelton.txt")
#data = toJson("july-log.txt")
#print data
#with open('data-Jul.json', 'w') as outfile:
# json.dump(data, outfile)
json_to_python = json.loads(data)
per_user = dict()
per_time = dict()
per_size = dict()
per_url = dict()
per_verb = dict()
per_zone = dict()
hostlist = []
hostcounter = dict()
counter = 0
for i in json_to_python:
y = json_to_python[i]
if y['HOST'] in per_user:
per_user[y['HOST']].append(y['STATUS'])
time = y['TIME']
hr = time.split(":")
per_time[y['HOST']].append(hr[1])
per_size[y['HOST']].append(int(y['SIZE']))
zn = time.split("-")
per_zone[y['HOST']].append(zn[1])
reque = y['REQUEST']
req = reque.split()
per_verb[y['HOST']].append(req[0])
per_url[y['HOST']].append(req[1])
else:
per_user[y['HOST']] = [y['STATUS']]
time = y['TIME']
hr = time.split(":")
per_time[y['HOST']] = [hr[1]]
per_size[y['HOST']] = [int(y['SIZE'])]
zn = time.split("-")
per_zone[y['HOST']] = [zn[1]]
reque = y['REQUEST']
req = reque.split()
per_verb[y['HOST']] = [req[0]]
per_url[y['HOST']] = [req[1]]
hostlist.append(y['HOST'])
##Cluster here:
##Analysis 1: IP address and Response Status Cluster
import numpy as np
X = np.array([[0,'0']])
for x in hostlist:
word_counter = {}
for word in per_user[x]:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
max_status = popular_words[0]
#print x + ": " + max_status
y = x.split(".")
ip = ""
for z in range(4):
l = len(y[z])
l = 3 - l
if(l>0):
zero = ""
for t in range(3 - len(y[z])):
zero = zero + "0"
y[z] = zero + y[z]
ip = ip + y[z]
#print str(float(float(ip)/1000)) + ": " + max_status
le = [float(float(ip)/1000),max_status]
X = np.vstack([X,le])
print "Printing IP: Status code"
print X
##For k-proto analysis:
from kmodes import kmodes
from kmodes import kprototypes
#Adjust number of clusters here
kproto = kprototypes.KPrototypes(n_clusters=4, init='Cao', verbose=2)
result = kproto.fit_predict(X, categorical= 1)
print "Printing result for 4 clusters by host-ip and status:"
print result
#cluster by status
##Analysis 2: Cluster by most frequent hour of the day
from sklearn.cluster import KMeans
import numpy as np
from sklearn.cluster import MiniBatchKMeans, KMeans
from sklearn.metrics.pairwise import pairwise_distances_argmin
from sklearn.preprocessing import StandardScaler
###Extract most frequently used hour of the day
X = np.array([[0,'0']])
print "*****PRINTING MOST FREQ TIME PER USER:****** "
for x in hostlist:
word_counter = {}
for word in per_time[x]:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
max_time = popular_words[0]
#print x + ": " + max_time
y = x.split(".")
ip = ""
for z in range(4):
l = len(y[z])
l = 3 - l
if(l>0):
zero = ""
for t in range(3 - len(y[z])):
zero = zero + "0"
y[z] = zero + y[z]
ip = ip + y[z]
#print str(float(float(ip)/1000)) + ": " + max_time
le = [float(float(ip)/1000),max_time]
X = np.vstack([X,le])
print "Printing IP:hour"
print X
print "Printing k-means for host IP and most frequent hour, clusters 24"
kmeans = KMeans(n_clusters=24, random_state=0).fit(X)
print kmeans.labels_
##############
#####*****SIZE******####
##Analysis 3: IP address and size of the response received
X = np.array([[0,'0']])
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
for x in hostlist:
avg_size = mean(per_size[x])
#print x + ": " + str(avg_size)
y = x.split(".")
ip = ""
for z in range(4):
l = len(y[z])
l = 3 - l
if(l>0):
zero = ""
for t in range(3 - len(y[z])):
zero = zero + "0"
y[z] = zero + y[z]
ip = ip + y[z]
#print str(float(float(ip)/1000)) + ": " + str(avg_size)
le = [float(float(ip)/1000),avg_size]
X = np.vstack([X,le])
print "Print IP addresss: Response Size"
print X
#Print result for size
kmeans = KMeans(n_clusters=10, random_state=0).fit(X)
print kmeans.labels_
###Analysis 4: Most frequently used http verb
###Analysis 5: Most frequently requested resource url
Y = np.array([[0,'0']])
Z = np.array([[0,'0']])
#Try to print a particular index of this hostlist
print "*****PRINTING VERB:*******"
for x in hostlist:
word_counter = {}
for word in per_verb[x]:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
popular_verb = popular_words[0]
#print x + ": " + popular_verb
word_counter = {}
for word in per_url[x]:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
popular_url = popular_words[0]
#print x + ": " + popular_url
y = x.split(".")
ip = ""
for z in range(4):
l = len(y[z])
l = 3 - l
if(l>0):
zero = ""
for t in range(3 - len(y[z])):
zero = zero + "0"
y[z] = zero + y[z]
ip = ip + y[z]
#print str(float(float(ip)/1000)) + ": " + popular_verb
le = [float(float(ip)/1000), popular_verb]
X = np.vstack([X,le])
#print str(float(float(ip)/1000)) + ": " + popular_url
le = [float(float(ip)/1000), popular_url]
Y = np.vstack([Y,le])
print "Printing IP:VERB"
print X
print "PRINTING IP:URL"
print Y
##For k-proto analysis:
#from kmodes import kmodes
#from kmodes import kprototypes
kproto = kprototypes.KPrototypes(n_clusters=4, init='Cao', verbose=2)
result = kproto.fit_predict(X, categorical= 1)
print "Printing result for verb:"
print result
kproto = kprototypes.KPrototypes(n_clusters=10, init='Cao', verbose=2)
result = kproto.fit_predict(Y, categorical= 1)
print "Printing result for url:"
print result