def handle_error(self): try: msglog.warn("%s handling error. Please check whether the peer is up and supports the %s protocol" % (self, self.protocol)) msglog.exception(prefix="handled") msglog.inform("%s closing due to error." % self) finally: self.close()
def run(self): while not self.cancelled.isSet(): try: packed = compact() except: msglog.error("Compactor failed to run compact().") msglog.exception(prefix="handled") else: msglog.debug("Compacted %d DB files." % packed) self.cancelled.wait(self.period) msglog.inform("Compactor thread exiting run-loop.")
def _terminate_events(self, events, reason): for event in events: try: event.terminate("Alarm %r" % self.name, reason) except: message = "Failed to terminate: %s." msglog.warn("Failed to terminate: %s." % event) msglog.exception(prefix="handled") else: message = "Alarm %r terminated event %s: %s." msglog.inform(message % (self.name, event, reason)) return events
def __call__(self): if not self.started: self.start() else: try: self._run_evaluation() except: msglog.exception() msglog.inform('Trigger reschedule delayed 20 seconds.') delay = 20 else: delay = self.poll_period finally: self.reschedule(delay)
def compact(): storages = [] for storagemap in MapStorage.filesysmap.values(): for storage in storagemap.values(): if isinstance(storage, MapStorage): storages.append(storage) for storage in storages: if storage.opened() and hasattr(storage.database, "reorganize"): storage.storagelock.acquire() try: packed = storage.database.reorganize() except: msglog.warn("Compact DBM file %r failed." % storage.filepath) msglog.exception(prefix="handled") else: msglog.inform("Compacted DBM file %r." % storage.filepath) finally: storage.storagelock.release() return len(storages)
def start(self): if not self._running.isSet(): _Trigger.start(self) variables = [] self._message = '' message = self.message while '$' in message: index = message.index('$') try: if message[index+1] == '{': end = string.find(message,'}',index) if end > -1: var = message[index:end+1] variable = var[2:-1] if '$' not in variable and '{' not in variable: message = string.replace(message,var,'%s') variables.append(variable) except IndexError: pass self._message += message[0:index+1] message = message[index+1:] self._message += message self._message_vars = tuple(variables) self.state = self.STATES[self.state]['start'] self._running.set() try: Calculator.start(self) except: message = "Failed to start Trigger %r. Will retry in 30 secs." msglog.error(message % self.name) if not self._start_failed: msglog.exception(prefix="handled") self._start_failed = True else: message = "Trigger %r started. Evaluation runs in 30 seconds." msglog.inform(message % self.name) self._start_failed = False finally: self.reschedule(30)
def remove_target(self, target): if not isinstance(target, str): targeturl = as_node_url(target) else: targeturl = target self.synclock.acquire() try: self.targets.remove(targeturl) except KeyError: removed = False message = "Target %s not removed from %s: does not exist." msglog.warn(message % (target, self)) else: try: self.targetmap.pop(targeturl) except KeyError: pass removed = True msglog.inform("Target %s removed from %s." % (target, self)) finally: self.synclock.release() return removed
def start(self): if not self._running.isSet(): _Trigger.start(self) variables = [] self._message = '' message = self.message while '$' in message: index = message.index('$') try: if message[index + 1] == '{': end = string.find(message, '}', index) if end > -1: var = message[index:end + 1] variable = var[2:-1] if '$' not in variable and '{' not in variable: message = string.replace(message, var, '%s') variables.append(variable) except IndexError: pass self._message += message[0:index + 1] message = message[index + 1:] self._message += message self._message_vars = tuple(variables) self.state = self.STATES[self.state]['start'] self._running.set() try: Calculator.start(self) except: message = "Failed to start Trigger %r. Will retry in 30 secs." msglog.error(message % self.name) if not self._start_failed: msglog.exception(prefix="handled") self._start_failed = True else: message = "Trigger %r started. Evaluation runs in 30 seconds." msglog.inform(message % self.name) self._start_failed = False finally: self.reschedule(30)
def run(self): pfx = 'RNA_Scan_Thread.run:' cmdfd = self.trigger_channel.fileno() enqueue = self.work_queue.queue_noresult handle_session = self.handle_session_input clear_notifications = self.clear_notifications while self.bPollRun: try: descriptors = self.descriptors.copy() if cmdfd not in descriptors: msglog.warn("Command channel FD removed!") descriptors.add(cmdfd) r,w,e = select.select(descriptors, [], descriptors, 1) for fd in e: if fd == cmdfd: message = "%s internal polling error. Must restart." msglog.error(message % pfx) raise TypeError("command channel OOB data") try: self.unregister_session(self.get_session(fd)) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") for fd in r: if fd in self.descriptors: try: if fd == cmdfd: clear_notifications() else: self.descriptors.discard(fd) enqueue(handle_session, fd) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") except: msglog.warn("%s loop error." % pfx) msglog.exception(prefix="handled") msglog.inform("%s exiting." % pfx)
def run(self): pfx = 'RNA_Scan_Thread.run:' cmdfd = self.trigger_channel.fileno() enqueue = self.work_queue.queue_noresult handle_session = self.handle_session_input clear_notifications = self.clear_notifications while self.bPollRun: try: descriptors = self.descriptors.copy() if cmdfd not in descriptors: msglog.warn("Command channel FD removed!") descriptors.add(cmdfd) r, w, e = select.select(descriptors, [], descriptors, 1) for fd in e: if fd == cmdfd: message = "%s internal polling error. Must restart." msglog.error(message % pfx) raise TypeError("command channel OOB data") try: self.unregister_session(self.get_session(fd)) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") for fd in r: if fd in self.descriptors: try: if fd == cmdfd: clear_notifications() else: self.descriptors.discard(fd) enqueue(handle_session, fd) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") except: msglog.warn("%s loop error." % pfx) msglog.exception(prefix="handled") msglog.inform("%s exiting." % pfx)
def start(self): self.message('Alarm Manager starting.') server = self.nodespace.as_node('/services/network/http_server') if not server.is_enabled() or self.use_https: server = self.nodespace.as_node('/services/network/https_server') config = { 'parent': server, 'enabled': 1, 'secured': True, 'debug': self.debug } syndic_handler_config = config.copy() config_handler_config = config.copy() cloud_handler_config = config.copy() cloud_config_handler_config = config.copy() exporter_handler_config = config.copy() trigger_handler_config = config.copy() syndic_handler_config['name'] = 'Syndication Viewer' syndic_handler_config['path'] = '/syndication' config_handler_config['name'] = 'Alarm Configurator' config_handler_config['path'] = '/alarmconfig' cloud_handler_config['name'] = 'Cloud Handler' cloud_handler_config['path'] = '/cloud' cloud_config_handler_config['name'] = 'Cloud Configurator' cloud_config_handler_config['path'] = '/cloudconfig' exporter_handler_config['name'] = 'Exporter Configurator' exporter_handler_config['path'] = '/exportconfig' trigger_handler_config['name'] = 'Trigger Configurator' trigger_handler_config['path'] = '/triggerconfig' ## # Frist create and setup Cloud Manager so events produced by # startup of configurators can be propogated properly. startlist = [] from mpx.service.cloud.manager import CloudManager cloud_manager = self.nodespace.create_node(CloudManager) cloud_manager.configure({ 'name': 'Cloud Manager', 'parent': '/services', 'debug': self.debug }) self.cloud = cloud_manager startlist.append(cloud_manager) from mpx.service.cloud import request_handler cloud_handler_config['manager'] = '/services/Cloud Manager' cloud_handler = self.nodespace.create_node( request_handler.CloudHandler) cloud_handler.configure(cloud_handler_config) del request_handler startlist.insert(0, cloud_handler) from mpx.service.cloud.xhtml.configuration import request_handler cloud_config_handler_config['manager'] = '/services/Cloud Manager' cloud_config_handler = self.nodespace.create_node( request_handler.CloudConfigurator) cloud_config_handler.configure(cloud_config_handler_config) del request_handler startlist.append(cloud_config_handler) for cloudservice in startlist: cloudservice.start() self.message( 'Alarm Manager configured and started Cloud Manager, Handler, and Configurator.' ) ## # Syndication Handler is idempotent and so can be started anytime. from mpx.service.alarms2.presentation.syndication.http import request_handler syndic_handler = request_handler.SyndicationViewer() syndic_handler.configure(syndic_handler_config) del request_handler syndic_handler.start() self.message( 'Alarm Manager configured and started Syndication Handler.') ## # Startup Alarm Manager's configurator so that pickled Alarm Events may be # recreated. from mpx.service.alarms2.presentation.xhtml.configuration import request_handler config_handler = self.nodespace.create_node( request_handler.AlarmConfigurator) config_handler.configure(config_handler_config) del request_handler config_handler.start() self.message('Alarm Manager created and started Alarm Configurator.') ## # Now that old Alarm Events have been recreated, configure and # startup Exporters. from mpx.service.alarms2.export import exporter container = self.nodespace.create_node(exporter.ExporterContainer) container.configure({ 'name': 'Alarm Exporters', 'parent': '/services', 'debug': self.debug }) export = self.nodespace.create_node(exporter.AlarmExporter) export.configure({ 'name': 'Alarm Logger', 'parent': container, 'debug': self.debug }) formatter = self.nodespace.create_node( exporter.AlarmDictionaryFormatter) formatter.configure({ 'name': 'Log Formatter', 'parent': export, 'debug': self.debug }) transporter = self.nodespace.create_node(exporter.LoggingTransporter) transporter.configure({ 'name': 'Alarm Logger', 'log': '/services/logger/Alarm Log', 'parent': export, 'debug': self.debug }) export.add_source(self, StateEvent) container.start() self.message('Created and started alarm exporters and logger.') from mpx.service.alarms2.export.xhtml.configuration import request_handler export_config_handler = self.nodespace.create_node( request_handler.ExportersConfigurator) export_config_handler.configure(exporter_handler_config) del request_handler export_config_handler.start() self.message( 'Alarm Manager created and started Exporter Configurator.') self.cloud.add_listener(self.handle_cloud_event, 'Alarm Manager') self.cloud.add_listener(self.handle_event_resend, 'EventResend') from mpx.service.cloud.manager import FormationUpdated self.cloud.dispatcher.register_for_type(self.handle_cloud_change, FormationUpdated) self.message( 'Alarm Manager added itself as listender for Cloud Events.') ## # With all supporting infrastructure started, start triggers which may # immediately generate alarms. from mpx.service.alarms2.trigger import triggermanager trigger_manager = self.nodespace.create_node( triggermanager.TriggerManager) trigger_manager.configure({ 'name': 'Trigger Manager', 'parent': '/services', 'debug': self.debug }) del triggermanager trigger_manager.start() self.message('Alarm Manager created and started Trigger Manager.') from mpx.service.alarms2.trigger.xhtml.configuration import request_handler trigger_config_handler = self.nodespace.create_node( request_handler.TriggersConfigurator) trigger_config_handler.configure(trigger_handler_config) del request_handler self.message('Alarm Manager created and started Trigger Configurator.') trigger_config_handler.start() try: store = as_node("/services/Event Store") except KeyError: msglog.inform("Alarm Manager creating Event Store.") from mpx.service.alarms2 import store estore = store.EventStore() estore.configure({"name": "Event Store", "parent": "/services"}) estore.start() msglog.inform("Alarm Manager setup and started Event Store.") else: msglog.inform("Alarm Manager found existing Event Store.") super(AlarmManager, self).start() self.message('Alarm Manager startup complete.')
def start(self): self.message('Alarm Manager starting.') server = self.nodespace.as_node('/services/network/http_server') if not server.is_enabled() or self.use_https: server = self.nodespace.as_node('/services/network/https_server') config = {'parent': server, 'enabled': 1, 'secured': True, 'debug': self.debug} syndic_handler_config = config.copy() config_handler_config = config.copy() cloud_handler_config = config.copy() cloud_config_handler_config = config.copy() exporter_handler_config = config.copy() trigger_handler_config = config.copy() syndic_handler_config['name'] = 'Syndication Viewer' syndic_handler_config['path'] = '/syndication' config_handler_config['name'] = 'Alarm Configurator' config_handler_config['path'] = '/alarmconfig' cloud_handler_config['name'] = 'Cloud Handler' cloud_handler_config['path'] = '/cloud' cloud_config_handler_config['name'] = 'Cloud Configurator' cloud_config_handler_config['path'] = '/cloudconfig' exporter_handler_config['name'] = 'Exporter Configurator' exporter_handler_config['path'] = '/exportconfig' trigger_handler_config['name'] = 'Trigger Configurator' trigger_handler_config['path'] = '/triggerconfig' ## # Frist create and setup Cloud Manager so events produced by # startup of configurators can be propogated properly. startlist = [] from mpx.service.cloud.manager import CloudManager cloud_manager = self.nodespace.create_node(CloudManager) cloud_manager.configure({'name': 'Cloud Manager', 'parent': '/services', 'debug': self.debug}) self.cloud = cloud_manager startlist.append(cloud_manager) from mpx.service.cloud import request_handler cloud_handler_config['manager'] = '/services/Cloud Manager' cloud_handler = self.nodespace.create_node(request_handler.CloudHandler) cloud_handler.configure(cloud_handler_config) del request_handler startlist.insert(0, cloud_handler) from mpx.service.cloud.xhtml.configuration import request_handler cloud_config_handler_config['manager'] = '/services/Cloud Manager' cloud_config_handler = self.nodespace.create_node(request_handler.CloudConfigurator) cloud_config_handler.configure(cloud_config_handler_config) del request_handler startlist.append(cloud_config_handler) for cloudservice in startlist: cloudservice.start() self.message('Alarm Manager configured and started Cloud Manager, Handler, and Configurator.') ## # Syndication Handler is idempotent and so can be started anytime. from mpx.service.alarms2.presentation.syndication.http import request_handler syndic_handler = request_handler.SyndicationViewer() syndic_handler.configure(syndic_handler_config) del request_handler syndic_handler.start() self.message('Alarm Manager configured and started Syndication Handler.') ## # Startup Alarm Manager's configurator so that pickled Alarm Events may be # recreated. from mpx.service.alarms2.presentation.xhtml.configuration import request_handler config_handler = self.nodespace.create_node(request_handler.AlarmConfigurator) config_handler.configure(config_handler_config) del request_handler config_handler.start() self.message('Alarm Manager created and started Alarm Configurator.') ## # Now that old Alarm Events have been recreated, configure and # startup Exporters. from mpx.service.alarms2.export import exporter container = self.nodespace.create_node(exporter.ExporterContainer) container.configure({'name': 'Alarm Exporters', 'parent': '/services', 'debug': self.debug}) export = self.nodespace.create_node(exporter.AlarmExporter) export.configure({'name': 'Alarm Logger', 'parent': container, 'debug': self.debug}) formatter = self.nodespace.create_node(exporter.AlarmDictionaryFormatter) formatter.configure({'name': 'Log Formatter', 'parent': export, 'debug': self.debug}) transporter = self.nodespace.create_node(exporter.LoggingTransporter) transporter.configure({'name': 'Alarm Logger', 'log': '/services/logger/Alarm Log', 'parent': export, 'debug': self.debug}) export.add_source(self, StateEvent) container.start() self.message('Created and started alarm exporters and logger.') from mpx.service.alarms2.export.xhtml.configuration import request_handler export_config_handler = self.nodespace.create_node(request_handler.ExportersConfigurator) export_config_handler.configure(exporter_handler_config) del request_handler export_config_handler.start() self.message('Alarm Manager created and started Exporter Configurator.') self.cloud.add_listener(self.handle_cloud_event, 'Alarm Manager') self.cloud.add_listener(self.handle_event_resend, 'EventResend') from mpx.service.cloud.manager import FormationUpdated self.cloud.dispatcher.register_for_type( self.handle_cloud_change, FormationUpdated) self.message('Alarm Manager added itself as listender for Cloud Events.') ## # With all supporting infrastructure started, start triggers which may # immediately generate alarms. from mpx.service.alarms2.trigger import triggermanager trigger_manager = self.nodespace.create_node(triggermanager.TriggerManager) trigger_manager.configure({'name': 'Trigger Manager', 'parent': '/services', 'debug': self.debug}) del triggermanager trigger_manager.start() self.message('Alarm Manager created and started Trigger Manager.') from mpx.service.alarms2.trigger.xhtml.configuration import request_handler trigger_config_handler = self.nodespace.create_node(request_handler.TriggersConfigurator) trigger_config_handler.configure(trigger_handler_config) del request_handler self.message('Alarm Manager created and started Trigger Configurator.') trigger_config_handler.start() try: store = as_node("/services/Event Store") except KeyError: msglog.inform("Alarm Manager creating Event Store.") from mpx.service.alarms2 import store estore = store.EventStore() estore.configure({"name": "Event Store", "parent": "/services"}) estore.start() msglog.inform("Alarm Manager setup and started Event Store.") else: msglog.inform("Alarm Manager found existing Event Store.") super(AlarmManager, self).start() self.message('Alarm Manager startup complete.')