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.append("/root/MON")
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
42 from osm_mon.plugins.CloudWatch.plugin_alarm import plugin_alarms
43 from osm_mon.plugins.CloudWatch.plugin_metric import plugin_metrics
46 server = {'server': 'localhost:9092'}
48 # Initialize consumers for alarms and metrics
49 common_consumer = KafkaConsumer(bootstrap_servers=server['server'])
51 # Create OpenStack alarming and metric instances
53 openstack_auth = Common()
54 openstack_metrics = metrics.Metrics()
55 openstack_alarms = alarming.Alarming()
57 # Create CloudWatch alarm and metric instances
58 cloudwatch_alarms = plugin_alarms()
59 cloudwatch_metrics = plugin_metrics()
61 def get_vim_type(message):
62 """Get the vim type that is required by the message."""
64 return json.loads(message.value)["vim_type"].lower()
65 except Exception as exc:
66 log.warn("vim_type is not configured correctly; %s", exc)
69 # Define subscribe the consumer for the plugins
70 topics = ['metric_request', 'alarm_request', 'access_credentials']
71 common_consumer.subscribe(topics)
74 log.info("Listening for alarm_request and metric_request messages")
75 for message in common_consumer:
76 # Check the message topic
77 if message.topic == "metric_request":
78 # Check the vim desired by the message
79 vim_type = get_vim_type(message)
81 if vim_type == "openstack":
82 log.info("This message is for the OpenStack plugin.")
83 openstack_metrics.metric_calls(
84 message, openstack_auth, auth_token)
86 elif vim_type == "aws":
87 cloudwatch_metrics.metric_calls(message)
88 log.info("This message is for the CloudWatch plugin.")
90 elif vim_type == "vrops":
91 log.info("This message is for the vROPs plugin.")
94 log.debug("vim_type is misconfigured or unsupported; %s",
97 elif message.topic == "alarm_request":
98 # Check the vim desired by the message
99 vim_type = get_vim_type(message)
100 if vim_type == "openstack":
101 log.info("This message is for the OpenStack plugin.")
102 openstack_alarms.alarming(message, openstack_auth, auth_token)
104 elif vim_type == "aws":
105 cloudwatch_alarms.alarm_calls(message)
106 log.info("This message is for the CloudWatch plugin.")
108 elif vim_type == "vrops":
109 log.info("This message is for the vROPs plugin.")
112 log.debug("vim_type is misconfigured or unsupported; %s",
115 elif message.topic == "access_credentials":
116 # Check the vim desired by the message
117 vim_type = get_vim_type(message)
118 if vim_type == "openstack":
119 log.info("This message is for the OpenStack plugin.")
120 auth_token = openstack_auth._authenticate(message=message)
122 elif vim_type == "aws":
123 #TODO Access credentials later
124 log.info("This message is for the CloudWatch plugin.")
126 elif vim_type == "vrops":
127 log.info("This message is for the vROPs plugin.")
130 log.debug("vim_type is misconfigured or unsupported; %s",
134 log.info("This topic is not relevant to any of the MON plugins.")
137 except KafkaError as exc:
138 log.warn("Exception: %s", exc)