Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Srikanth Vavilapalli | 21d1475 | 2016-09-02 01:37:34 +0000 | [diff] [blame] | 17 | # (C) Copyright Broadcom Corporation 2016 |
| 18 | # |
| 19 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | # you may not use this file except in compliance with the License. |
| 21 | # |
| 22 | # You may obtain a copy of the License at |
| 23 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | # |
| 25 | # Unless required by applicable law or agreed to in writing, software |
| 26 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | # See the License for the specific language governing permissions and |
| 29 | # limitations under the License. |
| 30 | |
| 31 | from broadviewpublisherbase import BroadViewPublisherBase |
| 32 | from kombu.connection import BrokerConnection |
| 33 | from kombu.messaging import Exchange, Queue, Consumer, Producer |
| 34 | import six |
| 35 | import uuid |
| 36 | import datetime |
| 37 | from broadview_collector.serializers.bst_to_ceilometer import BSTToCeilometer |
Srikanth Vavilapalli | f6eda8f | 2016-09-09 13:43:51 -0400 | [diff] [blame] | 38 | from broadview_collector.serializers.pt_to_ceilometer import PTToCeilometer |
Srikanth Vavilapalli | 21d1475 | 2016-09-02 01:37:34 +0000 | [diff] [blame] | 39 | import json |
| 40 | import ConfigParser |
| 41 | import sys |
| 42 | |
| 43 | try: |
| 44 | from oslo_log import log |
| 45 | except: |
| 46 | import logging as log |
| 47 | |
| 48 | LOG = log.getLogger(__name__) |
| 49 | |
| 50 | class BroadViewPublisher(BroadViewPublisherBase): |
| 51 | def readConfig(self): |
| 52 | try: |
| 53 | bvcfg = ConfigParser.ConfigParser() |
| 54 | bvcfg.read("/etc/broadviewcollector.conf") |
| 55 | self.rabbit_user = bvcfg.get("ceilometer", "rabbit_user") |
| 56 | self.rabbit_password = bvcfg.get("ceilometer", "rabbit_password") |
| 57 | self.rabbit_host = bvcfg.get("ceilometer", "rabbit_host") |
| 58 | self.rabbit_exchange = bvcfg.get("ceilometer", "rabbit_exchange") |
| 59 | except: |
| 60 | LOG.info("BroadViewPublisher: unable to read configuration") |
| 61 | |
| 62 | def errback(self, exc, interval): |
| 63 | LOG.error('Error: %r', exc, exc_info=1) |
| 64 | LOG.info('Retry in %s seconds.', interval) |
| 65 | |
| 66 | def setup_rabbit_mq_channel(self): |
| 67 | ceilometer_exchange = Exchange(self.rabbit_exchange, "topic", durable=False) |
| 68 | # connections/channels |
| 69 | connection = BrokerConnection(self.rabbit_host, self.rabbit_user, self.rabbit_password) |
| 70 | LOG.info("BroadViewPublisher: Connection to RabbitMQ server successful") |
| 71 | channel = connection.channel() |
| 72 | # produce |
| 73 | self._producer = Producer(channel, exchange=ceilometer_exchange, routing_key='notifications.info') |
| 74 | self._publish = connection.ensure(self._producer, self._producer.publish, errback=self.errback, max_retries=3) |
| 75 | |
| 76 | def __init__(self): |
| 77 | self.rabbit_user = 'openstack' |
| 78 | self.rabbit_password = 'password' |
| 79 | self.rabbit_host = '1.2.3.4' |
| 80 | self.rabbit_exchange = 'broadview_service' |
| 81 | self._producer = None |
| 82 | self._publish = None |
| 83 | self.readConfig() |
| 84 | LOG.info("BroadViewPublisher: Ceilometer Publisher Initialized") |
| 85 | |
| 86 | def publish(self, host, data): |
| 87 | code = 500 |
| 88 | # get a producer if needed |
| 89 | if not self._producer: |
| 90 | self.setup_rabbit_mq_channel() |
| 91 | if self._producer: |
| 92 | code = 200 |
| 93 | if self.isBST(data): |
| 94 | success, sdata = BSTToCeilometer().serialize(host, data) |
Srikanth Vavilapalli | f6eda8f | 2016-09-09 13:43:51 -0400 | [diff] [blame] | 95 | elif self.isPT(data): |
| 96 | self._topic = "broadview-pt" |
| 97 | success, sdata = PTToCeilometer().serialize(host, data) |
Srikanth Vavilapalli | 21d1475 | 2016-09-02 01:37:34 +0000 | [diff] [blame] | 98 | else: |
| 99 | success = False |
| 100 | if success: |
| 101 | sdata = json.loads(sdata) |
| 102 | if success: |
| 103 | for x in sdata: |
| 104 | try: |
| 105 | #self._producer.publish(x) |
| 106 | self._publish(x) |
| 107 | LOG.debug("BroadViewPublisher: Sent data to ceilometer exchange") |
| 108 | except Exception as e: |
| 109 | LOG.info('exception: {}'.format(e)) |
| 110 | LOG.info('unable to send to ceilometer exchange {}: {}'.format(self.rabbit_exchange, sys.exc_info()[0])) |
| 111 | else: |
| 112 | code = 500 |
| 113 | return code |
| 114 | |
| 115 | def __repr__(self): |
| 116 | return "BroadView Ceilometer Publisher {} {}".format(self.rabbit_host, self.rabbit_exchange) |
| 117 | |