2019-09-07 01:59:11 +08:00
|
|
|
LOG_INIT_MESSAGE = "Analysis didn't run."
|
|
|
|
|
2019-08-29 23:18:25 +08:00
|
|
|
|
|
|
|
class CommunicationAnalyzer(object):
|
|
|
|
|
2019-09-05 20:03:30 +08:00
|
|
|
def __init__(self, island_client, machine_ips):
|
2019-08-29 23:18:25 +08:00
|
|
|
self.island_client = island_client
|
2019-09-05 20:03:30 +08:00
|
|
|
self.machine_ips = machine_ips
|
2019-09-07 01:59:11 +08:00
|
|
|
self.log = AnalyzerLog(self.__class__.__name__)
|
2019-08-29 23:18:25 +08:00
|
|
|
|
|
|
|
def analyze_test_results(self):
|
2019-09-07 01:59:11 +08:00
|
|
|
self.log.clear()
|
2019-09-05 20:03:30 +08:00
|
|
|
for machine_ip in self.machine_ips:
|
|
|
|
if not self.did_monkey_communicate_back(machine_ip):
|
2019-09-07 01:59:11 +08:00
|
|
|
self.log.add_entry("Monkey from {} didn't communicate back".format(machine_ip))
|
2019-09-05 20:03:30 +08:00
|
|
|
return False
|
2019-09-07 01:59:11 +08:00
|
|
|
self.log.add_entry("Monkey from {} communicated back".format(machine_ip))
|
2019-09-05 20:03:30 +08:00
|
|
|
return True
|
2019-08-29 23:18:25 +08:00
|
|
|
|
2019-09-05 20:03:30 +08:00
|
|
|
def did_monkey_communicate_back(self, machine_ip):
|
2019-09-13 21:12:58 +08:00
|
|
|
query = {'ip_addresses': {'$elemMatch': {'$eq': machine_ip}}}
|
|
|
|
return len(self.island_client.find_monkeys_in_db(query)) > 0
|
2019-08-29 23:18:25 +08:00
|
|
|
|
|
|
|
|
2019-09-07 01:59:11 +08:00
|
|
|
class AnalyzerLog(object):
|
|
|
|
|
|
|
|
def __init__(self, analyzer_name):
|
|
|
|
self.contents = LOG_INIT_MESSAGE
|
|
|
|
self.name = analyzer_name
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.contents = ""
|
|
|
|
|
|
|
|
def add_entry(self, message):
|
|
|
|
self.contents = "{}\n{}".format(self.contents, message)
|
|
|
|
|
|
|
|
def get_contents(self):
|
|
|
|
return "{}: {}\n".format(self.name, self.contents)
|