-
Notifications
You must be signed in to change notification settings - Fork 1
/
markStable.py
182 lines (144 loc) · 5.34 KB
/
markStable.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
from elasticsearch import Elasticsearch
from time import time, strftime, localtime
from datetime import datetime, timedelta
from itertools import islice
import numpy as np
import pandas as pd
import json
import multiprocessing as mp
import Utility_Modules.r_utils as ut
import Utility_Modules.elasticqueries as qrs
def consumeIter(iterator, n = None):
"Advance the iterator n-steps ahead. If n is None, consume entirely."
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
collections.deque(iterator, maxlen=0)
else:
# advance to the empty slice starting at position n
next(islice(iterator, n, n), None)
def markStablePairPaths(es, src, dest, path_dict, threshold):
"""
Marks a single pair paths stable/unstable
Args:
es: ElasticSearch object
src: Source IP
dest: dest IP
path_dict: Dictionary of the paths between the pair. [Results will be stored in this dict only]
threshold: Amount of Consequetive reading to consider a path stable
Returns:
None
"""
query = {
"_source":['hash', 'timestamp'],
"size":9999,
"query":{
"bool":{
"must":[
{"term":{"complete":{"value":1}}},
{"term":{"src":{"value":src}}},
{"term":{"dest":{"value":dest}}}
]
}
}
}
times = []
paths = []
is_page = 0
while is_page == 0:
try:
page = es.search(index = 'ps_derived_complete_traces', body = query, scroll='2m', size=1000)
is_page = 1
except:
print("Error in retreiving timestamp data for the pair, retrying !:")
sleep(0.1)
sid = page['_scroll_id']
scroll_size = page['hits']['total']['value']
while scroll_size > 0:
for res in page['hits']['hits']:
times.append(res['_source']['timestamp'])
paths.append(res['_source']['hash'])
is_page = 0
while is_page == 0:
try:
page = es.scroll(scroll_id = sid, scroll='2m')
is_page = 1
except:
print("Error in retreiving timestamp data for the pair, retrying !:")
sleep(0.1)
sid = page['_scroll_id']
scroll_size = len(page['hits']['hits'])
data_frame = pd.DataFrame({"Time":times, "Path":paths}).sort_values(by=['Time'])
data_iterator = iter(range(data_frame.shape[0]-threshold))
for indx in data_iterator:
flag = 0
if path_dict.get(data_frame.iloc[indx,1]) != 1:
for i in range(indx,indx+threshold-1):
if data_frame.iloc[i,1] != data_frame.iloc[i+1,1]:
flag = 1
break
if flag == 0:
path_dict[data_frame.iloc[indx,1]] = 1
consumeIter(data_iterator, threshold)
def markStable(args):
"""
Marks paths between pairs as stable or unstable
Args:
pairs : Pandas df contaning columns containing src and dest
threshold : Amount of readings per hour
Returns:
List of Dictionaries of type:
{
"source":<SRC>,
"destination":<DEST>,
"paths":{P1:1, P2:0 .... Pn:1}
}
"""
pair, threshold, thread_id = args[0], args[1], args[2]
print("Thread : {} , Processing: {} Pairs".format(thread_id, pair.shape[0]))
paths_stability = []
start_time = time()
for indx in range(pair.shape[0]):
temp_res = {
"source":pair.iloc[indx,0],
"destination":pair.iloc[indx,1],
"path_dict":{}
}
p_dict_t = qrs.getPathCounts(es, pair.iloc[indx,0], pair.iloc[indx,1])
p_dict= {path['key']:0 for path in p_dict_t}
markStablePairPaths(es, pair.iloc[indx,0], pair.iloc[indx,1], path_dict=p_dict, threshold=threshold)
temp_res['path_dict'] = p_dict
paths_stability.append(temp_res)
if indx % 25 == 0:
mins, secs = divmod(time()-start_time, 60)
print("Thread : {} | Processed : {} pairs | Elapsed: {}m {}s".format(thread_id, indx, mins, secs))
return paths_stability
if __name__ == "__main__":
user = None
passwd = None
if user is None and passwd is None:
with open("creds.key") as f:
user = f.readline().strip()
passwd = f.readline().strip()
credentials = (user, passwd)
es = Elasticsearch(['atlas-kibana.mwt2.org:9200'], timeout = 180, http_auth=credentials)
if es.ping() == True:
print("Connection Successful")
else:
print("Connection Unsuccessful")
#Getting the Pairs:
pairs = qrs.getSourceDestinationPairs(es, 'ps_derived_complete_traces')
pairs = pd.DataFrame(pairs)
print("pairs Retreived")
THRESHOLD = 5
n_threads = 16
pair_pieces = np.array_split(pairs, n_threads)
pool = mp.Pool(n_threads)
results = pool.map(markStable, [[pair_pieces[i], THRESHOLD, i+1] for i in range(n_threads)])
pool.join()
pool.close()
result = []
for i in results:
result += i
with open("Results.json") as f:
f.write(json.dumps({"PathStability":result}))