def prepare(self): """Prepare for this test - does test.doPrepare() - sends prepare messages to target probes - waits for all targets to reply with ready """ # prepare everyone self.test.doPrepare() for target in self.test.getTargets(): ProbeStorage.connectToProbe(target) # prepare for the test width given id Client.send( Prepare(target, self.test.getId(), self.test.getName(), self.test.getOptions(), Identification.PROBE_ID)) # wait for everyone to be ready self.isReadyForTest.wait(self.test.prepareTimeout) if not self.isReadyForTest.is_set(): # TODO: send abort ? self.testError = TestError( "Prepare action timed out, probes did not reply in time") self.isReadyForTest.clear() if self.testError: raise self.testError testLogger.info("Prepare over, executing test")
def manageAdd(cls, action): """Add a probe to the DHT :param action: Add action containing the probe to add """ assert isinstance(action, a.Add) cls.logger.debug("Managing Add task") #add the probe to the local DHT ProbeStorage.addProbe(ProbeStorage.newProbe(action.getIdSonde(), action.getIpSonde())) if action.hello is not None: # tell the new probe about all other probe Client.send(action.hello) cls.logger.info("Added probe %s, id %s to known probes", action.getIpSonde(), action.getIdSonde())
def manageUpdateProbes(cls, action): """Update your list of probes with this set of probes :param action: UpdateProbes action instance """ assert isinstance(action, a.UpdateProbes) cls.logger.info("Joined overlay size %s", len(action.getProbeList())) if action.echo is not None: Client.send(Hello(action.echo, list(ProbeStorage.getAllOtherProbes()), Identification.PROBE_ID)) cls.logger.info("Sent echo to %s", action.echo) for probe in action.getProbeList(): # don't re-add ourselves to the local DHT if probe.getId() != Identification.PROBE_ID: ProbeStorage.addProbe(ProbeStorage.newProbe(probe.getId(), probe.getIp()))
def manageAdd(cls, action): """Add a probe to the DHT :param action: Add action containing the probe to add """ assert isinstance(action, a.Add) cls.logger.debug("Managing Add task") #add the probe to the local DHT ProbeStorage.addProbe( ProbeStorage.newProbe(action.getIdSonde(), action.getIpSonde())) if action.hello is not None: # tell the new probe about all other probe Client.send(action.hello) cls.logger.info("Added probe %s, id %s to known probes", action.getIpSonde(), action.getIdSonde())
def abort(self): """Abort a currently running test Does different actions depending of the current status of the test""" if not self.overed: self.test.doOver() self.testError = TestAborted("A probe send an abort signal") for target in self.test.getTargets(): # this test is aborted! Client.send(Abort(target, self.test.getId())) # releasing all flags self.isReadyForTest.set() self.areReportsCollected.set() testLogger.ddebug("Abort message broadcast") self.isReadyForTest.set() testLogger.info("Test cancelled")
def manageUpdateProbes(cls, action): """Update your list of probes with this set of probes :param action: UpdateProbes action instance """ assert isinstance(action, a.UpdateProbes) cls.logger.info("Joined overlay size %s", len(action.getProbeList())) if action.echo is not None: Client.send( Hello(action.echo, list(ProbeStorage.getAllOtherProbes()), Identification.PROBE_ID)) cls.logger.info("Sent echo to %s", action.echo) for probe in action.getProbeList(): # don't re-add ourselves to the local DHT if probe.getId() != Identification.PROBE_ID: ProbeStorage.addProbe( ProbeStorage.newProbe(probe.getId(), probe.getIp()))
def manageAddPrefix(cls, action): """Add a prefix to the DHT. A prefix is a set of addresses :param action: AddPrefix action """ assert isinstance(action, a.AddPrefix) try: net = ip_network(action.getPrefix(), strict = False) hosts = net.hosts() if net.num_addresses > 1 else [net.network_address] for host in hosts: try: h = str(host) if not ProbeStorage.isKnownIp(h): Client.send(AddToOverlay(Identification.PROBE_ID, h)) except Exception as e: cls.logger.warning("Error while adding probe %s : %s", h, e) except ValueError: cls.logger.warning("Wrong prefix given %s", action.getPrefix())
def manageQuit(cls, action): """Quit the overlay nicely Tells everyone about this :param action: Quit action """ assert isinstance(action, a.Quit) cls.logger.debug("Managing Quit task") cls.logger.info("Exiting the overlay") try: Client.broadcast(Bye("", Identification.PROBE_ID), toMyself = False) except ProbeConnectionException as e: cls.logger.warning("Could not sent Bye message %s", e) # Other commands to close all connections, etc Client.allMessagesSent() ProbeStorage.clearAllProbes() cls.logger.info("All probes cleared, all connections closed.") ProbeStorage.addSelfProbe() cls.logger.info("Re-added the localhost probe, ready to proceed again")
def over(self): """Finish testing - does test.doOver() - sends Over message to targets - wait for target probes to reply with Result """ self.test.doOver() for target in self.test.getTargets(): # this test is over! Client.send(Over(target, self.test.getId())) self.areReportsCollected.wait(self.test.resultTimeout) # if a timeout occurs if not self.areReportsCollected.is_set(): # TODO: send abort to the probe that did answer ? self.testError = TestError("All probes did not give results in time") self.areReportsCollected.clear() testLogger.info("Over done, processing results")
def manageQuit(cls, action): """Quit the overlay nicely Tells everyone about this :param action: Quit action """ assert isinstance(action, a.Quit) cls.logger.debug("Managing Quit task") cls.logger.info("Exiting the overlay") try: Client.broadcast(Bye("", Identification.PROBE_ID), toMyself=False) except ProbeConnectionException as e: cls.logger.warning("Could not sent Bye message %s", e) # Other commands to close all connections, etc Client.allMessagesSent() ProbeStorage.clearAllProbes() cls.logger.info("All probes cleared, all connections closed.") ProbeStorage.addSelfProbe() cls.logger.info("Re-added the localhost probe, ready to proceed again")
def over(self): """Finish testing - does test.doOver() - sends Over message to targets - wait for target probes to reply with Result """ self.test.doOver() for target in self.test.getTargets(): # this test is over! Client.send(Over(target, self.test.getId())) self.areReportsCollected.wait(self.test.resultTimeout) # if a timeout occurs if not self.areReportsCollected.is_set(): # TODO: send abort to the probe that did answer ? self.testError = TestError( "All probes did not give results in time") self.areReportsCollected.clear() testLogger.info("Over done, processing results")
def manageAddPrefix(cls, action): """Add a prefix to the DHT. A prefix is a set of addresses :param action: AddPrefix action """ assert isinstance(action, a.AddPrefix) try: net = ip_network(action.getPrefix(), strict=False) hosts = net.hosts() if net.num_addresses > 1 else [ net.network_address ] for host in hosts: try: h = str(host) if not ProbeStorage.isKnownIp(h): Client.send(AddToOverlay(Identification.PROBE_ID, h)) except Exception as e: cls.logger.warning("Error while adding probe %s : %s", h, e) except ValueError: cls.logger.warning("Wrong prefix given %s", action.getPrefix())
def manageAddToOverlay(cls, action): assert isinstance(action, a.AddToOverlay) cls.logger.ddebug("Add probe to overlay") try: probeId = Retry.retry(times=Consts.GET_REMOTE_ID_RETRY, interval=Consts.GET_REMOVE_ID_RETRY_INTERVAL, failure=ProbeConnectionException, eraise=ProbeConnectionException)( Client.getRemoteId)(action.probeIp) # probeId = Client.getRemoteId(action.probeIp) cls.logger.info("Adding probe %s at %s to overlay", probeId, action.probeIp) addMessage = Add(Identification.PROBE_ID, probeId, action.probeIp) #use action directly because of timing issues selfAddAction = a.Add( action.probeIp, probeId, Hello(probeId, list(ProbeStorage.getAllOtherProbes()), Identification.PROBE_ID, echo=Identification.PROBE_ID if action.mergeOverlays else None)) # selfAddMessage = copy.deepcopy(addMessage) # selfAddAction.hello = Hello(probeId, # list(ProbeStorage.getAllOtherProbes()), # Identification.PROBE_ID, # echo = Identification.PROBE_ID if action.mergeOverlays else None) # Do broadcast before adding the probe so that it doesn't receive unnecessary message # addMessage = m.Add(Identification.PROBE_ID, probeId, message.targetIp, hello=True) # print(ProbeStorage.getIdAllOtherProbes()) Client.broadcast(addMessage) #treat message after so that the new guy does not receive bogus add message #treat the add for this addToOverlay before any other AddToOverlay # import calls.messagetoaction as MTA cls.addTask(selfAddAction) #try to fix adding host too quickly Scheduler.addToOverlay() cls.logger.debug("Probe %s added to overlay", probeId) except ProbeConnectionException as e: cls.logger.warning("Adding probe failed %s : %s", action.probeIp, e)
def prepare(self): """Prepare for this test - does test.doPrepare() - sends prepare messages to target probes - waits for all targets to reply with ready """ # prepare everyone self.test.doPrepare() for target in self.test.getTargets(): ProbeStorage.connectToProbe(target) # prepare for the test width given id Client.send(Prepare(target, self.test.getId(), self.test.getName(), self.test.getOptions(), Identification.PROBE_ID)) # wait for everyone to be ready self.isReadyForTest.wait(self.test.prepareTimeout) if not self.isReadyForTest.is_set(): # TODO: send abort ? self.testError = TestError("Prepare action timed out, probes did not reply in time") self.isReadyForTest.clear() if self.testError: raise self.testError testLogger.info("Prepare over, executing test")
def manageAddToOverlay(cls, action): assert isinstance(action, a.AddToOverlay) cls.logger.ddebug("Add probe to overlay") try: probeId = Retry.retry(times = Consts.GET_REMOTE_ID_RETRY, interval = Consts.GET_REMOVE_ID_RETRY_INTERVAL, failure = ProbeConnectionException, eraise = ProbeConnectionException)(Client.getRemoteId)(action.probeIp) # probeId = Client.getRemoteId(action.probeIp) cls.logger.info("Adding probe %s at %s to overlay", probeId, action.probeIp) addMessage = Add(Identification.PROBE_ID, probeId, action.probeIp) #use action directly because of timing issues selfAddAction = a.Add(action.probeIp, probeId, Hello(probeId, list(ProbeStorage.getAllOtherProbes()), Identification.PROBE_ID, echo = Identification.PROBE_ID if action.mergeOverlays else None) ) # selfAddMessage = copy.deepcopy(addMessage) # selfAddAction.hello = Hello(probeId, # list(ProbeStorage.getAllOtherProbes()), # Identification.PROBE_ID, # echo = Identification.PROBE_ID if action.mergeOverlays else None) # Do broadcast before adding the probe so that it doesn't receive unnecessary message # addMessage = m.Add(Identification.PROBE_ID, probeId, message.targetIp, hello=True) # print(ProbeStorage.getIdAllOtherProbes()) Client.broadcast(addMessage) #treat message after so that the new guy does not receive bogus add message #treat the add for this addToOverlay before any other AddToOverlay # import calls.messagetoaction as MTA cls.addTask(selfAddAction) #try to fix adding host too quickly Scheduler.addToOverlay() cls.logger.debug("Probe %s added to overlay", probeId) except ProbeConnectionException as e: cls.logger.warning("Adding probe failed %s : %s", action.probeIp, e)
def finish(self): """Clean the test and send the results""" if self.report is not None: Client.send(Result(self.sourceId, self.test.getId(), self.report)) TestResponder.cleanTest(self.test.getId())
def replyPrepare(self): """Prepare for the test and send Ready when ready""" self.test.replyPrepare() Client.send( Ready(self.sourceId, self.test.getId(), Identification.PROBE_ID))
def replyPrepare(self): """Prepare for the test and send Ready when ready""" self.test.replyPrepare() Client.send(Ready(self.sourceId, self.test.getId(), Identification.PROBE_ID))
commander.start() if Params.WATCHERS: wd = os.path.join(DATA_DIR, WATCHERS_OUTPUT_DIR) if not os.path.exists(wd): os.mkdir(wd) WatcherManager.setOutputDir(wd) for watcher in args.watchers: parts = watcher.partition('=') try: WatcherManager.registerWatcher(parts[0], parts[2], wlogger) except WatcherError as e: logging.getLogger().warning("Starting watcher failed : %s", e, exc_info = 1) # ProbeStorage.addProbe( Probe("id", "10.0.0.1" ) ) client = Client() client.start() client.isUp.wait() logging.getLogger().info("Startup Done") for prefix in args.add_prefix: logging.getLogger().info("Adding probes in prefix %s", prefix) ActionMan.manageAddPrefix(actions.AddPrefix(prefix)) logging.getLogger().info("Probe(s) in prefix %s committed for addition", prefix) if Params.WATCHERS: WatcherManager.startWatchers() server.join() client.join() actionMan.join()
if Params.WATCHERS: wd = os.path.join(DATA_DIR, WATCHERS_OUTPUT_DIR) if not os.path.exists(wd): os.mkdir(wd) WatcherManager.setOutputDir(wd) for watcher in args.watchers: parts = watcher.partition('=') try: WatcherManager.registerWatcher(parts[0], parts[2], wlogger) except WatcherError as e: logging.getLogger().warning("Starting watcher failed : %s", e, exc_info=1) # ProbeStorage.addProbe( Probe("id", "10.0.0.1" ) ) client = Client() client.start() client.isUp.wait() logging.getLogger().info("Startup Done") for prefix in args.add_prefix: logging.getLogger().info("Adding probes in prefix %s", prefix) ActionMan.manageAddPrefix(actions.AddPrefix(prefix)) logging.getLogger().info( "Probe(s) in prefix %s committed for addition", prefix) if Params.WATCHERS: WatcherManager.startWatchers() server.join() client.join() actionMan.join()
def manageBroadcast(cls, action): """Decapsulate broadcast message""" assert isinstance(action, a.Broadcast) Client.broadcast(action.broadcast)