Exemplo n.º 1
0
    def _migrate_events(self, conn, publisher, status):
        converter = EventConverter(self.dmd, status)
        manager = Manager(self.dmd)
        pipes = (IdentifierPipe(manager), AddDeviceContextAndTagsPipe(manager),
                 AssignDefaultEventClassAndTagPipe(manager))
        routing_key = 'zenoss.events.summary' if status else 'zenoss.events.archive'

        taggers = {
            EventProxy.DEVICE_CLASS_DETAIL_KEY: (self.dmd.Devices, DeviceClass),
            EventProxy.DEVICE_GROUPS_DETAIL_KEY: (self.dmd.Groups, DeviceGroup),
            EventProxy.DEVICE_LOCATION_DETAIL_KEY: (self.dmd.Locations, Location),
            EventProxy.DEVICE_SYSTEMS_DETAIL_KEY: (self.dmd.Systems, System),
        }

        try:
            for event_rows in self._page_rows(conn, status):
                with AmqpTransaction(publisher.channel):
                    for mapping_event_context in imap(converter.convert, event_rows):
                        if self._shutdown:
                            raise ShutdownException()
                        occurrence = mapping_event_context.occurrence
                        zep_raw_event = self._event_to_zep_raw_event(occurrence)
                        event_ctx = EventContext(log, zep_raw_event)
                        for pipe in pipes:
                            pipe(event_ctx)

                        # Clear tags for device class, location, systems, groups from current device
                        event_ctx.eventProxy.tags.clearType(AddDeviceContextAndTagsPipe.DEVICE_TAGGERS.keys())

                        # Resolve tags from original fields in the event
                        for detail in occurrence.details:
                            if detail.name in taggers:
                                organizer_root, organizer_cls = taggers[detail.name]
                                tags = set()
                                for val in detail.value:
                                    try:
                                        obj = organizer_root.unrestrictedTraverse(str(val[1:]))
                                        if isinstance(obj, organizer_cls):
                                            tags.update(manager.getUuidsOfPath(obj))
                                    except Exception:
                                        if log.isEnabledFor(logging.DEBUG):
                                            log.debug("Unable to resolve UUID for %s", val)
                                if tags:
                                    event_tag = occurrence.tags.add()
                                    event_tag.type = detail.name
                                    event_tag.uuid.extend(tags)

                        self._merge_tags(zep_raw_event, occurrence)
                        if log.isEnabledFor(logging.DEBUG):
                            log.debug("Migrated event: %s", mapping_event_context.summary)

                        publisher.publish("$MigratedEvents", routing_key, mapping_event_context.summary,
                                          createQueues=("$ZepMigratedEventSummary","$ZepMigratedEventArchive"))
        except ShutdownException:
            pass