36b89e344e7c33375e3a5f916f986e07b9ead8f2
[osm/MON.git] / osm_mon / plugins / CloudWatch / plugin_metric.py
1 ##
2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
20 ##
21
22 '''
23 AWS-Plugin implements all the methods of MON to interact with AWS using the BOTO client
24 '''
25 from osm_mon.core.message_bus.producer import KafkaProducer
26 from osm_mon.plugins.CloudWatch.metrics import Metrics
27
28 __author__ = "Wajeeha Hamid"
29 __date__ = "18-September-2017"
30
31 import json
32 import logging
33
34 log = logging.getLogger(__name__)
35
36 class plugin_metrics():
37 """Receives Alarm info from MetricAlarm and connects with the consumer/producer """
38 def __init__ (self):
39 self.metric = Metrics()
40 self.producer = KafkaProducer('')
41 #---------------------------------------------------------------------------------------------------------------------------
42 def create_metric_request(self,metric_info):
43 '''Comaptible API using normalized parameters'''
44 metric_resp = self.metric.createMetrics(self.cloudwatch_conn,metric_info)
45 return metric_resp
46 #---------------------------------------------------------------------------------------------------------------------------
47 def update_metric_request(self,updated_info):
48 '''Comaptible API using normalized parameters'''
49 update_resp = self.metric.updateMetrics(self.cloudwatch_conn,updated_info)
50 return update_resp
51 #---------------------------------------------------------------------------------------------------------------------------
52 def delete_metric_request(self,delete_info):
53 '''Comaptible API using normalized parameters'''
54 del_resp = self.metric.deleteMetrics(self.cloudwatch_conn,delete_info)
55 return del_resp
56 #---------------------------------------------------------------------------------------------------------------------------
57 def list_metrics_request(self,list_info):
58 '''Comaptible API using normalized parameters'''
59 list_resp = self.metric.listMetrics(self.cloudwatch_conn,list_info)
60 return list_resp
61 #---------------------------------------------------------------------------------------------------------------------------
62 def read_metrics_data(self,list_info):
63 '''Comaptible API using normalized parameters
64 Read all metric data related to a specified metric'''
65 data_resp=self.metric.metricsData(self.cloudwatch_conn,list_info)
66 return data_resp
67 #---------------------------------------------------------------------------------------------------------------------------
68
69 def metric_calls(self,message,aws_conn):
70 """Gets the message from the common consumer"""
71
72 try:
73 self.cloudwatch_conn = aws_conn['cloudwatch_connection']
74 self.ec2_conn = aws_conn['ec2_connection']
75
76 metric_info = json.loads(message.value)
77 metric_response = dict()
78
79 if metric_info['vim_type'] == 'AWS':
80 log.debug ("VIM support : AWS")
81
82 # Check the Functionlity that needs to be performed: topic = 'alarms'/'metrics'/'Access_Credentials'
83 if message.topic == "metric_request":
84 log.info("Action required against: %s" % (message.topic))
85
86 if message.key == "create_metric_request":
87 if self.check_resource(metric_info['metric_create_request']['resource_uuid']) == True:
88 metric_resp = self.create_metric_request(metric_info['metric_create_request']) #alarm_info = message.value
89 metric_response['schema_version'] = metric_info['schema_version']
90 metric_response['schema_type'] = "create_metric_response"
91 metric_response['metric_create_response'] = metric_resp
92 payload = json.dumps(metric_response)
93 file = open('../../core/models/create_metric_resp.json','wb').write((payload))
94 self.producer.publish_metrics_response(key='create_metric_response', message=payload, topic ='metric_response')
95
96 log.info("Metric configured: %s", metric_resp)
97 return metric_response
98
99 elif message.key == "update_metric_request":
100 if self.check_resource(metric_info['metric_create_request']['resource_uuid']) == True:
101 update_resp = self.update_metric_request(metric_info['metric_create_request'])
102 metric_response['schema_version'] = metric_info['schema_version']
103 metric_response['schema_type'] = "update_metric_response"
104 metric_response['metric_update_response'] = update_resp
105 payload = json.dumps(metric_response)
106 file = open('../../core/models/update_metric_resp.json','wb').write((payload))
107 self.producer.update_metric_response(key='update_metric_response',message=payload,topic = 'metric_response')
108
109 log.info("Metric Updates: %s",metric_response)
110 return metric_response
111
112 elif message.key == "delete_metric_request":
113 if self.check_resource(metric_info['resource_uuid']) == True:
114 del_resp=self.delete_metric_request(metric_info)
115 payload = json.dumps(del_resp)
116 file = open('../../core/models/delete_metric_resp.json','wb').write((payload))
117 self.producer.delete_metric_response(key='delete_metric_response',message=payload,topic = 'metric_response')
118
119 log.info("Metric Deletion Not supported in AWS : %s",del_resp)
120 return del_resp
121
122 elif message.key == "list_metric_request":
123 if self.check_resource(metric_info['metrics_list_request']['resource_uuid']) == True:
124 list_resp = self.list_metrics_request(metric_info['metrics_list_request'])
125 metric_response['schema_version'] = metric_info['schema_version']
126 metric_response['schema_type'] = "list_metric_response"
127 metric_response['correlation_id'] = metric_info['metrics_list_request']['correlation_id']
128 metric_response['vim_type'] = metric_info['vim_type']
129 metric_response['metrics_list'] = list_resp
130 payload = json.dumps(metric_response)
131 file = open('../../core/models/list_metric_resp.json','wb').write((payload))
132 self.producer.list_metric_response(key='list_metrics_response',message=payload,topic = 'metric_response')
133
134 log.info("Metric List: %s",metric_response)
135 return metric_response
136
137 elif message.key == "read_metric_data_request":
138 if self.check_resource(metric_info['resource_uuid']) == True:
139 data_resp = self.read_metrics_data(metric_info)
140 metric_response['schema_version'] = metric_info['schema_version']
141 metric_response['schema_type'] = "read_metric_data_response"
142 metric_response['metric_name'] = metric_info['metric_name']
143 metric_response['metric_uuid'] = metric_info['metric_uuid']
144 metric_response['correlation_id'] = metric_info['correlation_uuid']
145 metric_response['resource_uuid'] = metric_info['resource_uuid']
146 metric_response['tenant_uuid'] = metric_info['tenant_uuid']
147 metric_response['metrics_data'] = data_resp
148 payload = json.dumps(metric_response)
149 file = open('../../core/models/read_metric_data_resp.json','wb').write((payload))
150 self.producer.read_metric_data_response(key='read_metric_data_response',message=payload,topic = 'metric_response')
151
152 log.info("Metric Data Response: %s",metric_response)
153 return metric_response
154
155 else:
156 log.debug("Unknown key, no action will be performed")
157 else:
158 log.info("Message topic not relevant to this plugin: %s",
159 message.topic)
160
161 except Exception as e:
162 log.error("Consumer exception: %s", str(e))
163
164 #---------------------------------------------------------------------------------------------------------------------------
165 def check_resource(self,resource_uuid):
166
167 '''Checking the resource_uuid is present in EC2 instances'''
168 try:
169 check_resp = dict()
170 instances = self.ec2_conn.get_all_instance_status()
171 status_resource = False
172
173 #resource_id
174 for instance_id in instances:
175 instance_id = str(instance_id).split(':')[1]
176 if instance_id == resource_uuid:
177 check_resp['resource_uuid'] = resource_uuid
178 status_resource = True
179 else:
180 status_resource = False
181
182 #status
183 return status_resource
184
185 except Exception as e:
186 log.error("Error in Plugin Inputs %s",str(e))
187 #---------------------------------------------------------------------------------------------------------------------------
188