Skip to content

Commit 8a4b18f

Browse files
authored
Merge pull request #182 from d-grossman/master
Witness!
2 parents d538a48 + b2bc39a commit 8a4b18f

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

poseidon/poseidonMain/poseidonMain.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,21 @@ def start_monitor(self, ivalue):
164164

165165
def stop_monitor(self, ivalue):
166166
''' stop monitoring an address'''
167+
168+
for my_hash, my_dict in ivalue.iteritems():
169+
if my_hash in self.monitoring:
170+
self.monitoring.pop(my_hash)
171+
172+
self.logger.debug('stop_monitor:{0},{1}'.format(ivalue, type(ivalue)))
167173
r_exchange = 'topic-poseidon-internal'
168-
r_key = 'poseidon.action.start_monitor'
174+
r_key = 'poseidon.action.stop_monitor'
169175
r_msg = json.dumps(ivalue)
170176
self.rabbit_channel_local.basic_publish(exchange=r_exchange,
171177
routing_key=r_key,
172178
body=r_msg)
173179

174180
def endpoint_shutdown(self, ivalue):
181+
self.logger.debug('endpoint_shutdown:{0}'.format(ivalue))
175182
''' shutdown an endpoint '''
176183
r_exchange = 'topic-poseidon-internal'
177184
r_key = 'poseidon.action.endpoint_shutdown'
@@ -197,13 +204,15 @@ def check_db(self, dev_hash, field):
197204
'''
198205
try:
199206
query = {'dev_id': dev_hash}
207+
query_string = str(query).replace("\'", "\"")
200208
ip = self.mod_configuration['storage_interface_ip']
201209
port = self.mod_configuration['storage_interface_port']
202210
uri = 'http://' + ip + ':' + port + \
203211
'/v1/storage/query/{database}/{collection}/{query_str}'.format(
204212
database=self.mod_configuration['database'],
205213
collection=self.mod_configuration['collection'],
206-
query_str=query)
214+
query_str=query_string)
215+
self.logger.error('check_db:{0}:{1}'.format(uri, type(uri)))
207216
resp = requests.get(uri)
208217
self.logger.debug('response from db:' + resp.text)
209218

@@ -241,46 +250,49 @@ def start_vent_collector(self, dev_hash, num_captures=1):
241250
except Exception, e:
242251
self.logger.debug('failed to start vent collector' + str(e))
243252

253+
@staticmethod
254+
def just_the_hash(ivalue):
255+
return ivalue.keys()[0]
256+
244257
def handle_item(self, itype, ivalue):
245258
self.logger.debug('handle_item:{0}:{1}'.format(itype, ivalue))
246-
ivalue = json.loads(ivalue)
259+
260+
# just get a string back from the ml stuff
261+
if 'poseidon.algos.eval_dev_class' not in itype:
262+
ivalue = json.loads(ivalue)
263+
247264
if itype == 'poseidon.action.shutdown':
248265
self.logger.debug('***** shutting down')
249266
self.shutdown = True
250267
if itype == 'poseidon.action.new_machine':
251268
self.logger.debug('***** new machine {0}'.format(ivalue))
252269
# tell monitor to monitor
253-
self.start_vent_collector(ivalue)
270+
self.start_vent_collector(self.just_the_hash(ivalue))
254271
self.start_monitor(ivalue)
255-
if itype == 'poseidon.analytics.results.traditional':
256-
# TODO make a db call
257-
# need to compare results to db
258-
if False:
259-
# if bad
260-
self.logger.debug(
261-
'***** shutting down endpoint:{0}:{1}'.format(itype, ivalue))
262-
self.endpoint_shutdown(ivalue)
263-
else:
264-
# if good
265-
self.logger.debug(
266-
'***** allowing endpoint {0}:{1}'.format(itype, ivalue))
267-
self.stop_monitor(ivalue)
268272
if 'poseidon.algos.eval_dev_class' in itype:
273+
# ivalue = classificationtype:<string>
269274
# result form eval device classifier with
270275
# dev hash attached to end of routing key
271-
dev_hash = ivalue.split('.')[-1]
276+
dev_hash = itype.split('.')[-1]
272277
prev_class = self.check_db(dev_hash, 'dev_classification')
278+
273279
monitoring_id = self.monitoring[dev_hash]
274-
self.stop_monitor(monitoring_id)
280+
temp_d = {dev_hash: monitoring_id}
281+
282+
# self.stop_monitor(monitoring_id)
283+
self.stop_monitor(temp_d)
284+
275285
self.logger.debug('stopping monitoring on:' + itype)
286+
self.logger.debug('classified as:{0}'.format(ivalue))
287+
self.logger.debug('classified previously {0}'.format(prev_class))
276288
if ivalue == prev_class:
277289
self.logger.debug(
278-
'***** allowing endpoint {0}:{1}'.format(itype, ivalue))
279-
self.endpoint_allow(monitoring_id)
290+
'***** allowing endpoint {0}:{1}'.format(itype, temp_d))
291+
self.endpoint_allow(temp_d)
280292
else:
281293
self.logger.debug(
282-
'***** shutting down endpoint:{0}:{1}'.format(itype, ivalue))
283-
self.endpoint_shutdown(monitoring_id)
294+
'***** shutting down endpoint:{0}:{1}'.format(itype, temp_d))
295+
self.endpoint_shutdown(temp_d)
284296

285297
def make_rabbit_connection(self, host, exchange, queue_name, keys): # pragma: no cover
286298
'''

poseidon/poseidonMonitor/NorthBoundControllerAbstraction/NorthBoundControllerAbstraction.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def __init__(self):
9696
self.prev_endpoints = {}
9797
self.new_endpoints = {}
9898
self.mirroring = {}
99+
self.shutdown = {}
99100
self.do_rabbit = True
100101
self.m_queue = Queue.Queue()
101102
self.rabbit_connection_local = None
@@ -171,7 +172,8 @@ def init_rabbit(self): # pragma: no cover
171172
host = 'poseidon-rabbit'
172173
exchange = 'topic-poseidon-internal'
173174
queue_name = 'poseidon_NBCA'
174-
binding_key = ['poseidon.algos.#', 'poseidon.action.#']
175+
# binding_key = ['poseidon.algos.#', 'poseidon.action.#']
176+
binding_key = ['poseidon.action.#']
175177
retval = self.make_rabbit_connection(
176178
host, exchange, queue_name, binding_key)
177179
self.rabbit_channel_local = retval[0]
@@ -244,6 +246,28 @@ def handle_item(self, item):
244246
self.bcf.mirror_ip(my_dict['ip-address'])
245247
self.mirroring[my_hash] = my_dict
246248

249+
if itype == 'poseidon.action.endpoint_shutdown':
250+
self.logger.debug(
251+
'endpoint_shutdown:{0}:{1}'.format(ivalue, type(ivalue)))
252+
for my_hash, my_dict in ivalue.iteritems():
253+
bad_ip = my_dict.get('ip-address')
254+
if bad_ip is not None:
255+
self.logger.debug(
256+
'****** shutdown {0}:{1}'.format(bad_ip, ivalue))
257+
self.bcf.shutdown_ip(bad_ip)
258+
self.shutdown[my_hash] = my_dict
259+
260+
if itype == 'poseidon.action.stop_monitor':
261+
self.logger.debug('stop_monitor:{0}:{1}'.format(itype, ivalue))
262+
for my_hash, my_dict in ivalue.iteritems():
263+
self.logger.debug('stop_monitor_dict:{0}'.format(my_dict))
264+
my_ip = my_dict.get('ip-address')
265+
if my_ip is not None:
266+
self.logger.debug('***** shutting down {0}'.format(my_ip))
267+
self.bcf.unmirror_ip(my_ip)
268+
if my_hash in self.mirroring:
269+
self.mirroring.pop(my_hash)
270+
247271
def get_rabbit_work(self):
248272
'''get work item from queue if exists'''
249273
# type , value
@@ -285,14 +309,17 @@ def find_new_machines(self, machines):
285309

286310
def print_state(self):
287311
self.logger.debug('**************PREV*****************')
288-
for my_hash, my_value in self.prev_endpoints.iteritems():
289-
self.logger.debug('P:{0}:{1}'.format(my_hash, my_value))
312+
for my_hash, my_dict in self.prev_endpoints.iteritems():
313+
self.logger.debug('P:{0}:{1}'.format(my_hash, my_dict))
290314
self.logger.debug('**************NEW******************')
291-
for my_hash, my_value in self.new_endpoints.iteritems():
292-
self.logger.debug('N:{0}:{1}'.format(my_hash, my_value))
315+
for my_hash, my_dict in self.new_endpoints.iteritems():
316+
self.logger.debug('N:{0}:{1}'.format(my_hash, my_dict))
293317
self.logger.debug('***********MIRRORING***************')
294-
for my_hash, my_value in self.mirroring.iteritems():
295-
self.logger.debug('M:{0}:{1}'.format(my_hash, my_value))
318+
for my_hash, my_dict in self.mirroring.iteritems():
319+
self.logger.debug('M:{0}:{1}'.format(my_hash, my_dict))
320+
self.logger.debug('***********SHUTDOWN****************')
321+
for my_hash, my_dict in self.shutdown.iteritems():
322+
self.logger.debug('M:{0}:{1}'.format(my_hash, my_dict))
296323

297324
def send_new_machines(self):
298325
'''send listing of new machines to main for decisions'''

poseidon/poseidonMonitor/NorthBoundControllerAbstraction/proxy/bcf/bcf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ def shutdown_ip(self, ip_addr, shutdown=True, mac_addr=None):
165165
tenant = record.get('tenant')
166166
segment = record.get('segment')
167167
mac = record.get('mac')
168-
name = '{0}{1}{2}'.format(tenant, segment, mac)
168+
name = '{0}{1}{2}'.format(tenant, segment, mac).replace(':', '')
169169
if record.get('name') is not None:
170170
name = record['name']
171171
module_logger.debug('bcf shutting down: {0}'.format(record))
172+
module_logger.debug('t:{0} s:{1} n:{2} m{3} shut:{4}'.format(
173+
tenant, segment, name, mac, shutdown))
172174
self.shutdown_endpoint(tenant, segment, name, mac, shutdown)
173175
shutdowns.append(record)
174176
return shutdowns
@@ -212,7 +214,7 @@ def get_highest(span_fabric):
212214
return (my_max + 1)
213215
else:
214216
module_logger.debug('noFilters online')
215-
return 0
217+
return 1
216218

217219
def get_seq_by_ip(self, ip):
218220
my_filter = self.get_span_fabric()[0].get('filter')

0 commit comments

Comments
 (0)