Beispiel #1
0
def _get_notifier():
    """Return a notifier object.

    If _notifier is None it means that a notifier object has not been set.
    If _notifier is False it means that a notifier has previously failed to
    construct.
    Otherwise it is a constructed Notifier object.
    """
    global _notifier

    if _notifier is None:
        host = CONF.default_publisher_id or socket.gethostname()
        try:
            transport = oslo_messaging.get_notification_transport(CONF)
            _notifier = oslo_messaging.Notifier(transport,
                                                "identity.%s" % host)
        except Exception:
            LOG.exception("Failed to construct notifier")
            _notifier = False

    return _notifier
Beispiel #2
0
def create_notifier(conf, log, metrics_enabled):
    """Create a new notifier."""
    if oslo_messaging:
        transport = oslo_messaging.get_notification_transport(
            conf, url=conf.audit_middleware_notifications.transport_url)
        notifier = oslo_messaging.Notifier(
            transport,
            os.path.basename(sys.argv[0]),
            driver=conf.audit_middleware_notifications.driver,
            topics=conf.audit_middleware_notifications.topics)

        mqs = conf.audit_middleware_notifications.mem_queue_size
        if mqs is None:
            mqs = 10000
        notf = _MessagingNotifier(notifier, log, mqs, metrics_enabled)
        notf.setDaemon(True)
        notf.start()
        return notf

    else:
        return _LogNotifier(log)
Beispiel #3
0
    def test_serializer(self, mock_utcnow):
        transport = _FakeTransport(self.conf)

        serializer = msg_serializer.NoOpSerializer()

        notifier = oslo_messaging.Notifier(transport,
                                           'test.localhost',
                                           driver='test',
                                           topics=['test'],
                                           serializer=serializer)

        message_id = uuid.uuid4()
        uuid.uuid4 = mock.Mock(return_value=message_id)

        mock_utcnow.return_value = datetime.datetime.utcnow()

        serializer.serialize_context = mock.Mock()
        serializer.serialize_context.return_value = dict(user='******')

        serializer.serialize_entity = mock.Mock()
        serializer.serialize_entity.return_value = 'sbar'

        notifier.info(dict(user='******'), 'test.notify', 'bar')

        message = {
            'message_id': str(message_id),
            'publisher_id': 'test.localhost',
            'event_type': 'test.notify',
            'priority': 'INFO',
            'payload': 'sbar',
            'timestamp': str(timeutils.utcnow()),
        }

        self.assertEqual([(dict(user='******'), message, 'INFO', None)],
                         _impl_test.NOTIFICATIONS)

        uuid.uuid4.assert_called_once_with()
        serializer.serialize_context.assert_called_once_with(dict(user='******'))
        serializer.serialize_entity.assert_called_once_with(
            dict(user='******'), 'bar')
Beispiel #4
0
    def test_emit_cfg_log_notifier_in_notifier_drivers(self):
        drivers = ['messaging', 'log']
        self.config(notification_driver=drivers)
        self.stub_flg = True

        transport = test_notifier._FakeTransport(self.conf)
        notifier = oslo_messaging.Notifier(transport)

        def fake_notifier(*args, **kwargs):
            self.stub_flg = False

        self.stubs.Set(notifier, 'error', fake_notifier)

        logrecord = logging.LogRecord(name='name',
                                      level='WARN',
                                      pathname='/tmp',
                                      lineno=1,
                                      msg='Message',
                                      args=None,
                                      exc_info=None)
        self.publisherrorshandler.emit(logrecord)
        self.assertTrue(self.stub_flg)
Beispiel #5
0
def setup(url=None, optional=False):
    """Initialise the oslo_messaging layer."""
    global TRANSPORT, NOTIFIER, SERIALIZER

    if not cfg.CONF.enable_notifications:
        LOG.info(_LI("Notifications disabled"))
        return
    LOG.info(_LI("Notifications enabled"))

    messaging.set_transport_defaults('sahara')

    SERIALIZER = ContextSerializer(JsonPayloadSerializer())

    try:
        TRANSPORT = messaging.get_transport(cfg.CONF, url, aliases=_ALIASES)
    except messaging.InvalidTransportURL as e:
        TRANSPORT = None
        if not optional or e.url:
            raise

    if TRANSPORT:
        NOTIFIER = messaging.Notifier(TRANSPORT, serializer=SERIALIZER)
Beispiel #6
0
    def __init__(self, conf):
        self.oslo_notifiers = {}
        try:
            notifier_plugins = conf.notifiers

            LOG.debug('notifier_plugins: %s', notifier_plugins)

            if not notifier_plugins:
                LOG.info('Evaluator Notifier is disabled')
                return

            for notifier in notifier_plugins:
                LOG.debug('Adding evaluator notifier %s', notifier)

                self.oslo_notifiers[notifier] = oslo_messaging.Notifier(
                    get_transport(conf),
                    driver='messagingv2',
                    publisher_id='vitrage.evaluator',
                    topics=[notifier])

        except Exception as e:
            LOG.info('Evaluator Notifier - missing configuration %s' % str(e))
    def test_serializer(self, mock_utcnow):
        transport = _FakeTransport(self.conf)

        serializer = msg_serializer.NoOpSerializer()

        notifier = oslo_messaging.Notifier(transport,
                                           'test.localhost',
                                           driver='test',
                                           topic='test',
                                           serializer=serializer)

        message_id = uuid.uuid4()
        self.mox.StubOutWithMock(uuid, 'uuid4')
        uuid.uuid4().AndReturn(message_id)

        mock_utcnow.return_value = datetime.datetime.utcnow()

        self.mox.StubOutWithMock(serializer, 'serialize_context')
        self.mox.StubOutWithMock(serializer, 'serialize_entity')
        serializer.serialize_context(dict(user='******')).\
            AndReturn(dict(user='******'))
        serializer.serialize_entity(dict(user='******'), 'bar').AndReturn('sbar')

        self.mox.ReplayAll()

        notifier.info(dict(user='******'), 'test.notify', 'bar')

        message = {
            'message_id': str(message_id),
            'publisher_id': 'test.localhost',
            'event_type': 'test.notify',
            'priority': 'INFO',
            'payload': 'sbar',
            'timestamp': str(timeutils.utcnow()),
        }

        self.assertEqual([(dict(user='******'), message, 'INFO', None)],
                         _impl_test.NOTIFICATIONS)
Beispiel #8
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('sendto', help='Vitrage message bus path')
    parser.add_argument('topic', help='zabbix topic')
    parser.add_argument('body', help='zabbix body')
    args = parser.parse_args()

    logging.debug('[vitrage] sendto=%s, topic=%s, body=%s', args.sendto,
                  args.topic, args.body)

    transport_url = args.sendto
    transport = messaging.get_transport(cfg.CONF, transport_url)
    driver = 'messagingv2'
    publisher = 'zabbix_%s' % socket.gethostname()
    notifier = messaging.Notifier(transport,
                                  driver=driver,
                                  publisher_id=publisher,
                                  topics=['vitrage_notifications'])

    alarm_status = args.topic.lower()
    event_type = '%s.%s' % (ZABBIX_EVENT_TYPE, alarm_status)

    payload = {
        key.lower().strip(): prop.strip()
        for key, prop in (line.split('=') for line in args.body.splitlines())
    }

    logging.debug('[vitrage] publisher: %s, event: %s, payload %s', publisher,
                  event_type, payload)

    notifier.info(ctxt={
        'message_id': uuidutils.generate_uuid(),
        'publisher_id': publisher,
        'timestamp': datetime.utcnow()
    },
                  event_type=event_type,
                  payload=payload)
Beispiel #9
0
    def test_notifier(self, mock_utcnow):
        self.config(driver=['log'],
                    group='oslo_messaging_notifications')

        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        notifier = oslo_messaging.Notifier(transport, 'test.localhost')

        message_id = uuid.uuid4()
        uuid.uuid4 = mock.Mock()
        uuid.uuid4.return_value = message_id

        mock_utcnow.return_value = datetime.datetime.utcnow()

        logger = mock.Mock()

        message = {
            'message_id': str(message_id),
            'publisher_id': 'test.localhost',
            'event_type': 'test.notify',
            'priority': 'INFO',
            'payload': 'bar',
            'timestamp': str(timeutils.utcnow()),
        }

        with mock.patch.object(logging, 'getLogger') as gl:
            gl.return_value = logger

            notifier.info({}, 'test.notify', 'bar')

            uuid.uuid4.assert_called_once_with()
            logging.getLogger.assert_called_once_with(
                'oslo.messaging.notification.test.notify')

        logger.info.assert_called_once_with(JsonMessageMatcher(message))

        self.assertTrue(notifier.is_enabled())
Beispiel #10
0
    def __init__(self, app, conf):
        self._app = app
        self.ignore_projects = [
            proj.strip()
            for proj in conf.get('ignore_projects', 'gnocchi').split(',')
        ]

        oslo_messaging.set_transport_defaults(
            conf.get('control_exchange', 'swift'))
        self._notifier = oslo_messaging.Notifier(
            oslo_messaging.get_transport(cfg.CONF, url=conf.get('url')),
            publisher_id='ceilometermiddleware',
            driver=conf.get('driver', 'messagingv2'),
            topic=conf.get('topic', 'notifications'))

        self.metadata_headers = [
            h.strip().replace('-', '_').lower()
            for h in conf.get("metadata_headers", "").split(",") if h.strip()
        ]

        self.reseller_prefix = conf.get('reseller_prefix', 'AUTH_')
        if self.reseller_prefix and self.reseller_prefix[-1] != '_':
            self.reseller_prefix += '_'
Beispiel #11
0
    def test_emit_cfg_log_notifier_in_notifier_drivers(self):
        drivers = ['messaging', 'log']
        self.config(driver=drivers, group='oslo_messaging_notifications')
        self.stub_flg = True

        transport = oslo_messaging.get_notification_transport(self.conf)
        notifier = oslo_messaging.Notifier(transport)

        def fake_notifier(*args, **kwargs):
            self.stub_flg = False

        self.useFixture(
            fixtures.MockPatchObject(notifier, 'error', fake_notifier))

        logrecord = logging.LogRecord(name='name',
                                      level='WARN',
                                      pathname='/tmp',
                                      lineno=1,
                                      msg='Message',
                                      args=None,
                                      exc_info=None)
        self.publisherrorshandler.emit(logrecord)
        self.assertTrue(self.stub_flg)
def setup(url=None, optional=False):
    """Initialise the oslo_messaging layer."""
    global TRANSPORT, NOTIFIER

    if url and url.startswith("fake://"):
        # NOTE: oslo_messaging fake driver uses time.sleep
        # for task switch, so we need to monkey_patch it
        eventlet.monkey_patch(time=True)

    if not TRANSPORT:
        oslo_messaging.set_transport_defaults('dcmanager')
        exmods = ['dcmanager.common.exception']
        try:
            TRANSPORT = oslo_messaging.get_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods, aliases=_ALIASES)
        except oslo_messaging.InvalidTransportURL as e:
            TRANSPORT = None
            if not optional or e.url:
                raise

    if not NOTIFIER and TRANSPORT:
        serializer = RequestContextSerializer(JsonPayloadSerializer())
        NOTIFIER = oslo_messaging.Notifier(TRANSPORT, serializer=serializer)
Beispiel #13
0
    def __init__(self, app, **conf):
        self._application = app
        global _LOG
        _LOG = logging.getLogger(conf.get('log_name', __name__))
        self._service_name = conf.get('service_name')
        self._ignore_req_list = [x.upper().strip() for x in
                                 conf.get('ignore_req_list', '').split(',')]
        self._cadf_audit = OpenStackAuditApi(conf.get('audit_map_file'))

        transport_aliases = self._get_aliases(cfg.CONF.project)
        if conf.get('audit_topics'):
            topics=[x.strip() for x in
                                 conf.get('audit_topics', '').split(',')]
        else:
            topics=['audit']
        if messaging:
            self._notifier = oslo_messaging.Notifier(
                oslo_messaging.get_transport(cfg.CONF,
                                             aliases=transport_aliases,
                                             url=conf.get('audit_transport_url')),
                os.path.basename(sys.argv[0]),
                driver=conf.get('audit_driver') or 'messaging',
                topics=topics)
Beispiel #14
0
    def test_batch_event_listener(self, mocked):
        msg_notifier = oslo_messaging.Notifier(
            self.transport, topics=['alarm.all'], driver='messaging',
            publisher_id='test-publisher')

        received_events = []
        mocked.side_effect = lambda msg: received_events.append(msg)
        event1 = {'event_type': 'compute.instance.update',
                  'traits': ['foo', 'bar'],
                  'message_id': '20d03d17-4aba-4900-a179-dba1281a3451',
                  'generated': '2016-04-23T06:50:21.622739'}
        event2 = {'event_type': 'compute.instance.update',
                  'traits': ['foo', 'bar'],
                  'message_id': '20d03d17-4aba-4900-a179-dba1281a3452',
                  'generated': '2016-04-23T06:50:23.622739'}
        msg_notifier.sample({}, 'event', event1)
        msg_notifier.sample({}, 'event', event2)

        svc = event.EventAlarmEvaluationService(0, self.CONF)
        self.addCleanup(svc.terminate)

        time.sleep(1)
        self.assertEqual(1, len(received_events))
        self.assertEqual(2, len(received_events[0]))
Beispiel #15
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('sendto', help='Vitrage message bus path')
    parser.add_argument('topic', help='zabbix topic')
    parser.add_argument('body', help='zabbix body')
    args = parser.parse_args()

    logging.info('SENDTO: %s', args.sendto)
    logging.info('TOPIC: %s', args.topic)
    logging.info('BODY:\n----\n%s\n', args.body)

    transport_url = args.sendto
    transport = messaging.get_notification_transport(cfg.CONF, transport_url)
    driver = 'messagingv2'
    publisher = 'zabbix_%s' % socket.gethostname()
    notifier = messaging.Notifier(transport,
                                  driver=driver,
                                  publisher_id=publisher,
                                  topics=['vitrage_notifications'])

    alarm_status = args.topic.lower()
    event_type = '%s.%s' % (ZABBIX_EVENT_TYPE, alarm_status)
    payload = create_payload(args.body)

    logging.info('PUBLISHER: %s', publisher)
    logging.info('EVENT_TYPE: %s', event_type)
    logging.info('\nPAYLOAD:\n%s', payload)
    notifier.info(ctxt={
        'message_id': uuidutils.generate_uuid(),
        'publisher_id': publisher,
        'timestamp': datetime.utcnow()
    },
                  event_type=event_type,
                  payload=payload)
    logging.info('MESSAGE SENT..')
Beispiel #16
0
def get_notifier(transport, publisher_id):
    """Return a configured oslo_messaging notifier."""
    serializer = RequestContextSerializer(JsonPayloadSerializer())
    notifier = oslo_messaging.Notifier(transport, serializer=serializer)
    return notifier.prepare(publisher_id=publisher_id)
Beispiel #17
0
 def __init__(self):
     publisher_id = CONF.default_publisher_id
     self._transport = get_transport()
     self._notifier = oslo_messaging.Notifier(self._transport,
                                              publisher_id=publisher_id)
Beispiel #18
0
def get_notifier(transport, publisher_id):
    """Return a configured oslo_messaging notifier."""
    notifier = oslo_messaging.Notifier(transport, serializer=_SERIALIZER)
    return notifier.prepare(publisher_id=publisher_id)
 def __init__(self, topic='test_topic', driver='messaging', pub_id='', url=''):
     transport =  oslo_messaging.get_notification_transport(cfg.CONF, url=url)
     self.notifier = oslo_messaging.Notifier(transport, driver=driver, publisher_id=pub_id, topic=topic)
Beispiel #20
0
 def build_notifier(self):
     serializer = rpc.RequestContextSerializer(rpc.JsonPayloadSerializer())
     return om.Notifier(self.__transport,
                        publisher_id=self.publisher_id,
                        topic=self.topic_name,
                        serializer=serializer)
Beispiel #21
0
    def test_notifier(self, mock_utcnow):
        drivers = []
        if self.v1:
            drivers.append('messaging')
        if self.v2:
            drivers.append('messagingv2')

        self.config(driver=drivers,
                    topics=self.topics,
                    group='oslo_messaging_notifications')

        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        if hasattr(self, 'ctor_pub_id'):
            notifier = oslo_messaging.Notifier(transport,
                                               publisher_id=self.ctor_pub_id)
        else:
            notifier = oslo_messaging.Notifier(transport)

        prepare_kwds = {}
        if hasattr(self, 'retry'):
            prepare_kwds['retry'] = self.retry
        if hasattr(self, 'prep_pub_id'):
            prepare_kwds['publisher_id'] = self.prep_pub_id
        if prepare_kwds:
            notifier = notifier.prepare(**prepare_kwds)

        transport._send_notification = mock.Mock()

        message_id = uuid.uuid4()
        uuid.uuid4 = mock.Mock(return_value=message_id)

        mock_utcnow.return_value = datetime.datetime.utcnow()

        message = {
            'message_id': str(message_id),
            'publisher_id': self.expected_pub_id,
            'event_type': 'test.notify',
            'priority': self.priority.upper(),
            'payload': self.payload,
            'timestamp': str(timeutils.utcnow()),
        }

        sends = []
        if self.v1:
            sends.append(dict(version=1.0))
        if self.v2:
            sends.append(dict(version=2.0))

        calls = []
        for send_kwargs in sends:
            for topic in self.topics:
                if hasattr(self, 'retry'):
                    send_kwargs['retry'] = self.retry
                else:
                    send_kwargs['retry'] = -1
                target = oslo_messaging.Target(topic='%s.%s' %
                                               (topic, self.priority))
                calls.append(
                    mock.call(target, self.ctxt, message, **send_kwargs))

        method = getattr(notifier, self.priority)
        method(self.ctxt, 'test.notify', self.payload)

        uuid.uuid4.assert_called_once_with()
        transport._send_notification.assert_has_calls(calls, any_order=True)

        self.assertTrue(notifier.is_enabled())
Beispiel #22
0
    def __init__(self, worker_id, conf, namespaces=None, pollster_list=None):
        namespaces = namespaces or ['compute', 'central']
        pollster_list = pollster_list or []
        group_prefix = conf.polling.partitioning_group_prefix

        # features of using coordination and pollster-list are exclusive, and
        # cannot be used at one moment to avoid both samples duplication and
        # samples being lost
        if pollster_list and conf.coordination.backend_url:
            raise PollsterListForbidden()

        super(AgentManager, self).__init__(worker_id)

        self.conf = conf

        def _match(pollster):
            """Find out if pollster name matches to one of the list."""
            return any(
                fnmatch.fnmatch(pollster.name, pattern)
                for pattern in pollster_list)

        if type(namespaces) is not list:
            namespaces = [namespaces]

        # we'll have default ['compute', 'central'] here if no namespaces will
        # be passed
        extensions = (self._extensions('poll', namespace, self.conf).extensions
                      for namespace in namespaces)
        # get the extensions from pollster builder
        extensions_fb = (self._extensions_from_builder('poll', namespace)
                         for namespace in namespaces)
        if pollster_list:
            extensions = (moves.filter(_match, exts) for exts in extensions)
            extensions_fb = (moves.filter(_match, exts)
                             for exts in extensions_fb)

        self.extensions = list(itertools.chain(*list(extensions))) + list(
            itertools.chain(*list(extensions_fb)))

        if self.extensions == []:
            raise EmptyPollstersList()

        discoveries = (self._extensions('discover', namespace,
                                        self.conf).extensions
                       for namespace in namespaces)
        self.discoveries = list(itertools.chain(*list(discoveries)))
        self.polling_periodics = None

        if self.conf.coordination.backend_url:
            # XXX uuid4().bytes ought to work, but it requires ascii for now
            coordination_id = str(uuid.uuid4()).encode('ascii')
            self.partition_coordinator = coordination.PartitionCoordinator(
                self.conf, coordination_id)
        else:
            self.partition_coordinator = None

        # Compose coordination group prefix.
        # We'll use namespaces as the basement for this partitioning.
        namespace_prefix = '-'.join(sorted(namespaces))
        self.group_prefix = ('%s-%s' % (namespace_prefix, group_prefix)
                             if group_prefix else namespace_prefix)

        self.notifier = oslo_messaging.Notifier(
            messaging.get_transport(self.conf),
            driver=self.conf.publisher_notifier.telemetry_driver,
            publisher_id="ceilometer.polling")

        self._keystone = None
        self._keystone_last_exception = None
 def _setup_notifier(self, transport, topic='testtopic',
                     publisher_id='testpublisher'):
     return oslo_messaging.Notifier(transport, topic=topic,
                                    driver='messaging',
                                    publisher_id=publisher_id)
Beispiel #24
0
 def __init__(self, conf):
     self.notifier = oslo_messaging.Notifier(
         messaging.get_transport(conf),
         driver='messagingv2',
         publisher_id="alarming.evaluator",
         topic=conf.notifier_topic)
    def test_notifier(self, mock_utcnow):
        drivers = []
        if self.v1:
            drivers.append('messaging')
        if self.v2:
            drivers.append('messagingv2')

        self.config(driver=drivers,
                    topics=self.topics,
                    group='oslo_messaging_notifications')

        transport = _FakeTransport(self.conf)

        if hasattr(self, 'ctor_pub_id'):
            notifier = oslo_messaging.Notifier(transport,
                                               publisher_id=self.ctor_pub_id)
        else:
            notifier = oslo_messaging.Notifier(transport)

        prepare_kwds = {}
        if hasattr(self, 'retry'):
            prepare_kwds['retry'] = self.retry
        if hasattr(self, 'prep_pub_id'):
            prepare_kwds['publisher_id'] = self.prep_pub_id
        if prepare_kwds:
            notifier = notifier.prepare(**prepare_kwds)

        self.mox.StubOutWithMock(transport, '_send_notification')

        message_id = uuid.uuid4()
        self.mox.StubOutWithMock(uuid, 'uuid4')
        uuid.uuid4().AndReturn(message_id)

        mock_utcnow.return_value = datetime.datetime.utcnow()

        message = {
            'message_id': str(message_id),
            'publisher_id': self.expected_pub_id,
            'event_type': 'test.notify',
            'priority': self.priority.upper(),
            'payload': self.payload,
            'timestamp': str(timeutils.utcnow()),
        }

        sends = []
        if self.v1:
            sends.append(dict(version=1.0))
        if self.v2:
            sends.append(dict(version=2.0))

        for send_kwargs in sends:
            for topic in self.topics:
                if hasattr(self, 'retry'):
                    send_kwargs['retry'] = self.retry
                else:
                    send_kwargs['retry'] = None
                target = oslo_messaging.Target(topic='%s.%s' % (topic,
                                                                self.priority))
                transport._send_notification(target, self.ctxt, message,
                                             **send_kwargs).InAnyOrder()

        self.mox.ReplayAll()

        method = getattr(notifier, self.priority)
        method(self.ctxt, 'test.notify', self.payload)
Beispiel #26
0
 def __init__(self, conf):
     transport = messaging.get_transport(conf)
     self.notifier = oslo_messaging.Notifier(
         transport,
         driver=conf.publisher_notifier.telemetry_driver,
         publisher_id="ceilometer.api")
Beispiel #27
0
 def __init__(self, conf, publisher_id, topic):
     transport = get_transport(conf)
     self.notifier = oslo_messaging.Notifier(transport,
                                             driver='messagingv2',
                                             publisher_id=publisher_id,
                                             topic=topic)
Beispiel #28
0
 def get_notifier(self, topics=['testtopic'], publisher_id='testpublisher'):
     return oslo_messaging.Notifier(self.transport,
                                    topics=topics,
                                    driver='messaging',
                                    publisher_id=publisher_id)
Beispiel #29
0
 def _get_notifier(self, transport, pipe):
     return oslo_messaging.Notifier(
         transport,
         driver=cfg.CONF.publisher_notifier.telemetry_driver,
         publisher_id='ceilometer.notification',
         topic='%s-%s' % (self.NOTIFICATION_IPC, pipe.name))
Beispiel #30
0
def init(conf):
    global TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = messaging.get_transport(conf, allowed_remote_exmods=exmods)
    serializer = RequestContextSerializer(JsonPayloadSerializer())
    NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer)