1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
3 # This file is part of OSM Monitoring module
4 # All Rights Reserved to Intel Corporation
6 # Licensed under the Apache License, Version 2.0 (the "License"); you
7 # may not use this file except in compliance with the License. You may
8 # obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 # implied. See the License for the specific language governing
16 # permissions and limitations under the License.
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
20 """A common KafkaConsumer for all MON plugins."""
27 sys.path.insert(0, os.path.abspath('../'))
29 logging.basicConfig(filename='MON_plugins.log',
30 format='%(asctime)s %(message)s',
31 datefmt='%m/%d/%Y %I:%M:%S %p', filemode='a',
33 log = logging.getLogger(__name__)
35 from kafka import KafkaConsumer
36 from kafka.errors import KafkaError
38 from osm_mon.plugins.OpenStack.Aodh import alarming
39 from osm_mon.plugins.OpenStack.common import Common
40 from osm_mon.plugins.OpenStack.Gnocchi import metrics
44 server = {'server': 'localhost:9092'}
46 # Initialize consumers for alarms and metrics
47 common_consumer = KafkaConsumer(group_id='osm_mon',
48 bootstrap_servers=server['server'])
50 # Create OpenStack alarming and metric instances
52 openstack_auth = Common()
53 openstack_metrics = metrics.Metrics()
54 openstack_alarms = alarming.Alarming()
57 def get_vim_type(message):
58 """Get the vim type that is required by the message."""
60 return json.loads(message.value)["vim_type"].lower()
61 except Exception as exc:
62 log.warn("vim_type is not configured correctly; %s", exc)
65 # Define subscribe the consumer for the plugins
66 topics = ['metric_request', 'alarm_request', 'access_credentials']
67 common_consumer.subscribe(topics)
70 log.info("Listening for alarm_request and metric_request messages")
71 for message in common_consumer:
72 # Check the message topic
73 if message.topic == "metric_request":
74 # Check the vim desired by the message
75 vim_type = get_vim_type(message)
76 if vim_type == "openstack":
77 log.info("This message is for the OpenStack plugin.")
78 openstack_metrics.metric_calls(
79 message, openstack_auth, auth_token)
81 elif vim_type == "cloudwatch":
82 log.info("This message is for the CloudWatch plugin.")
84 elif vim_type == "vrops":
85 log.info("This message is for the vROPs plugin.")
88 log.debug("vim_type is misconfigured or unsupported; %s",
91 elif message.topic == "alarm_request":
92 # Check the vim desired by the message
93 vim_type = get_vim_type(message)
94 if vim_type == "openstack":
95 log.info("This message is for the OpenStack plugin.")
96 openstack_alarms.alarming(message, openstack_auth, auth_token)
98 elif vim_type == "cloudwatch":
99 log.info("This message is for the CloudWatch plugin.")
101 elif vim_type == "vrops":
102 log.info("This message is for the vROPs plugin.")
105 log.debug("vim_type is misconfigured or unsupported; %s",
108 elif message.topic == "access_credentials":
109 # Check the vim desired by the message
110 vim_type = get_vim_type(message)
111 if vim_type == "openstack":
112 log.info("This message is for the OpenStack plugin.")
113 auth_token = openstack_auth._authenticate(message=message)
115 elif vim_type == "cloudwatch":
116 log.info("This message is for the CloudWatch plugin.")
118 elif vim_type == "vrops":
119 log.info("This message is for the vROPs plugin.")
122 log.debug("vim_type is misconfigured or unsupported; %s",
126 log.info("This topic is not relevant to any of the MON plugins.")
129 except KafkaError as exc:
130 log.warn("Exception: %s", exc)