def get_snapshot(self, params): try: snapshot = NetworkSnapshot() json_snapshot = snapshot.memcache_get_json() return HttpJsonResponse(json.loads(json_snapshot)) except Exception: return HttpErrorResponse('Failed to fetch snapshot')
class ConfMonitor: def __init__(self, apps_dir): self.apps_dir = apps_dir self.snapshot = NetworkSnapshot() self.check_interval = self.snapshot.interval self.apps = {} self.logger = logging.getLogger('conf-monitor') def get_apps(self): self.apps = app.read_apps(self.apps_dir) self.apps_set = set(self.apps.keys()) def link_app(self, cmd, app): try: cmd.run('link_app', app) self.logger.info('%s added to %s' % (app, cmd.host)) except: self.logger.error('Could not add app %s to %s' % (app, cmd.host)) def remove_app(self, cmd, app): try: cmd.run('rm_app', app) self.logger.info('%s removed from %s' % (app, cmd.host)) except: self.logger.error('Could not remove app %s from %s' % (app, cmd.host)) def check(self, edge_location): edge_apps = set(edge_location.apps) if edge_apps == self.apps_set: return cmd = RemoteCommand(edge_location.address) to_link = self.apps_set - edge_apps if to_link: for app in to_link: self.link_app(cmd, app) to_remove = edge_apps - self.apps_set if to_remove: for app in to_remove: self.remove_app(cmd, app) def monitor(self): self.logger.info('Using "%s" as applications dir' %(self.apps_dir)) while True: try: self.get_apps() self.snapshot.memcache_load() for edge_location in self.snapshot.edge_locations: self.check(edge_location) time.sleep(self.check_interval) except KeyboardInterrupt: self.logger.info('Exiting...') exit() except Exception as e: self.logger.exception(e) exit()
class ConfMonitor: def __init__(self, apps_dir): self.apps_dir = apps_dir self.snapshot = NetworkSnapshot() self.check_interval = self.snapshot.interval self.apps = {} self.logger = logging.getLogger('conf-monitor') def get_apps(self): self.apps = app.read_apps(self.apps_dir) self.apps_set = set(self.apps.keys()) def link_app(self, cmd, app): try: cmd.run('link_app', app) self.logger.info('%s added to %s' % (app, cmd.host)) except: self.logger.error('Could not add app %s to %s' % (app, cmd.host)) def remove_app(self, cmd, app): try: cmd.run('rm_app', app) self.logger.info('%s removed from %s' % (app, cmd.host)) except: self.logger.error('Could not remove app %s from %s' % (app, cmd.host)) def check(self, edge_location): edge_apps = set(edge_location.apps) if edge_apps == self.apps_set: return cmd = RemoteCommand(edge_location.address) to_link = self.apps_set - edge_apps if to_link: for app in to_link: self.link_app(cmd, app) to_remove = edge_apps - self.apps_set if to_remove: for app in to_remove: self.remove_app(cmd, app) def monitor(self): self.logger.info('Using "%s" as applications dir' % (self.apps_dir)) while True: try: self.get_apps() self.snapshot.memcache_load() for edge_location in self.snapshot.edge_locations: self.check(edge_location) time.sleep(self.check_interval) except KeyboardInterrupt: self.logger.info('Exiting...') exit() except Exception as e: self.logger.exception(e) exit()
def monitor(self): prev_snapshot = NetworkSnapshot() snapshot = NetworkSnapshot() last_snapshot = time.time() exceptionCount = 0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', self.port)) sock.listen(0) self.logger.info('Listening on port %d' %(self.port)) readFds = set([sock]) while True: try: timeout = last_snapshot + snapshot.interval - time.time() rs, ws, xs = select.select(readFds, [], [], timeout) exceptionCount = 0 if not rs: # timeout, so we take a snapshot with what we got if snapshot != prev_snapshot: self.logger.info('Taking snapshot with %s' %(str(snapshot))) self.cmap.assign_edge_locations(snapshot.edge_locations) self.cmap.update_memcache() snapshot.memcache_save() last_snapshot = time.time() prev_snapshot.edge_locations = snapshot.edge_locations snapshot.clear() continue for readFd in rs: if readFd == sock: # incoming connection conn, addr = sock.accept() readFds.add(conn) else: self.read_and_close(readFd, snapshot.edge_locations) readFds.remove(readFd) except KeyboardInterrupt: self.logger.info('Exiting...') sock.close() exit() except select.error as e: self.logger.exception(e) # this might happen if we miss a deadline with other operations if timeout < 0: last_snapshot += snapshot.interval exceptionCount += 1 # if we get more than 3 exceptions in a row, we give up if exceptionCount > 3: sock.close() exit()
def __init__(self, apps_dir): self.apps_dir = apps_dir self.snapshot = NetworkSnapshot() self.check_interval = self.snapshot.interval self.apps = {} self.logger = logging.getLogger('conf-monitor')