8f595b02a00f615f255a64e597957dbb2ec91444
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."""
29 logging
.basicConfig(stream
=sys
.stdout
,
30 format
='%(asctime)s %(message)s',
31 datefmt
='%m/%d/%Y %I:%M:%S %p',
33 log
= logging
.getLogger(__name__
)
36 sys
.path
.append(os
.path
.abspath(os
.path
.join(os
.path
.realpath(__file__
), '..', '..', '..', '..')))
38 from kafka
import KafkaConsumer
40 from osm_mon
.plugins
.OpenStack
.Aodh
import alarming
41 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
43 from osm_mon
.plugins
.CloudWatch
.plugin_alarm
import plugin_alarms
44 from osm_mon
.plugins
.CloudWatch
.plugin_metric
import plugin_metrics
45 from osm_mon
.plugins
.CloudWatch
.connection
import Connection
46 from osm_mon
.plugins
.CloudWatch
.access_credentials
import AccessCredentials
48 from osm_mon
.plugins
.vRealiseOps
import plugin_receiver
50 from osm_mon
.core
.auth
import AuthManager
51 from osm_mon
.core
.database
import DatabaseManager
54 if "BROKER_URI" in os
.environ
:
55 server
= {'server': os
.getenv("BROKER_URI")}
57 server
= {'server': 'localhost:9092'}
59 # Initialize consumers for alarms and metrics
60 common_consumer
= KafkaConsumer(bootstrap_servers
=server
['server'],
61 key_deserializer
=bytes
.decode
,
62 value_deserializer
=bytes
.decode
,
63 group_id
="mon-consumer")
65 auth_manager
= AuthManager()
66 database_manager
= DatabaseManager()
67 database_manager
.create_tables()
69 # Create OpenStack alarming and metric instances
70 openstack_metrics
= metrics
.Metrics()
71 openstack_alarms
= alarming
.Alarming()
73 # Create CloudWatch alarm and metric instances
74 cloudwatch_alarms
= plugin_alarms()
75 cloudwatch_metrics
= plugin_metrics()
76 aws_connection
= Connection()
77 aws_access_credentials
= AccessCredentials()
79 # Create vROps plugin_receiver class instance
80 vrops_rcvr
= plugin_receiver
.PluginReceiver()
83 def get_vim_type(vim_uuid
):
84 """Get the vim type that is required by the message."""
86 credentials
= database_manager
.get_credentials(vim_uuid
)
87 return credentials
.type
88 except Exception as exc
:
89 log
.exception("Error getting vim_type: ")
93 # Define subscribe the consumer for the plugins
94 topics
= ['metric_request', 'alarm_request', 'access_credentials', 'vim_account']
95 # TODO: Remove access_credentials
96 common_consumer
.subscribe(topics
)
98 log
.info("Listening for alarm_request and metric_request messages")
99 for message
in common_consumer
:
100 log
.info("Message arrived: %s", message
)
103 values
= json
.loads(message
.value
)
105 values
= yaml
.safe_load(message
.value
)
106 # Check the message topic
107 if message
.topic
== "metric_request":
108 # Check the vim desired by the message
109 vim_type
= get_vim_type(values
['vim_uuid'])
111 if vim_type
== "openstack":
112 log
.info("This message is for the OpenStack plugin.")
113 openstack_metrics
.metric_calls(message
)
114 elif vim_type
== "aws":
115 log
.info("This message is for the CloudWatch plugin.")
116 aws_conn
= aws_connection
.setEnvironment()
117 cloudwatch_metrics
.metric_calls(message
, aws_conn
)
119 elif vim_type
== "vmware":
120 log
.info("This metric_request message is for the vROPs plugin.")
121 vrops_rcvr
.consume(message
)
124 log
.debug("vim_type is misconfigured or unsupported; %s",
127 elif message
.topic
== "alarm_request":
128 # Check the vim desired by the message
129 vim_type
= get_vim_type(values
['vim_uuid'])
130 if vim_type
== "openstack":
131 log
.info("This message is for the OpenStack plugin.")
132 openstack_alarms
.alarming(message
)
134 elif vim_type
== "aws":
135 log
.info("This message is for the CloudWatch plugin.")
136 aws_conn
= aws_connection
.setEnvironment()
137 cloudwatch_alarms
.alarm_calls(message
, aws_conn
)
139 elif vim_type
== "vmware":
140 log
.info("This alarm_request message is for the vROPs plugin.")
141 vrops_rcvr
.consume(message
)
144 log
.debug("vim_type is misconfigured or unsupported; %s",
147 elif message
.topic
== "vim_account":
148 if message
.key
== "create" or message
.key
== "edit":
149 auth_manager
.store_auth_credentials(values
)
150 if message
.key
== "delete":
151 auth_manager
.delete_auth_credentials(values
)
153 # TODO: Remove in the near future when all plugins support vim_uuid. Modify tests accordingly.
154 elif message
.topic
== "access_credentials":
155 # Check the vim desired by the message
156 vim_type
= get_vim_type(values
['vim_uuid'])
158 if vim_type
== "aws":
159 log
.info("This message is for the CloudWatch plugin.")
160 aws_access_credentials
.access_credential_calls(message
)
162 elif vim_type
== "vmware":
163 log
.info("This access_credentials message is for the vROPs plugin.")
164 vrops_rcvr
.consume(message
)
167 log
.debug("vim_type is misconfigured or unsupported; %s",
171 log
.info("This topic is not relevant to any of the MON plugins.")
174 except Exception as exc
:
175 log
.exception("Exception: %s")