Updated logging for CloudWatch plugin
[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
26 __author__ = "Wajeeha Hamid"
27 __date__ = "18-September-2017"
28
29 import sys
30 import json
31 from connection import Connection
32 from metric_alarms import MetricAlarm
33 from metrics import Metrics
34 sys.path.append("../../core/message_bus")
35 from producer import KafkaProducer
36 import logging
37
38 log = logging.getLogger(__name__)
39
40 class plugin_metrics():
41 """Receives Alarm info from MetricAlarm and connects with the consumer/producer """
42 def __init__ (self):
43 self.metric = Metrics()
44 self.producer = KafkaProducer('')
45 #---------------------------------------------------------------------------------------------------------------------------
46 def create_metric_request(self,metric_info):
47 '''Comaptible API using normalized parameters'''
48 metric_resp = self.metric.createMetrics(self.cloudwatch_conn,metric_info)
49 return metric_resp
50 #---------------------------------------------------------------------------------------------------------------------------
51 def update_metric_request(self,updated_info):
52 '''Comaptible API using normalized parameters'''
53 update_resp = self.metric.updateMetrics(self.cloudwatch_conn,updated_info)
54 return update_resp
55 #---------------------------------------------------------------------------------------------------------------------------
56 def delete_metric_request(self,delete_info):
57 '''Comaptible API using normalized parameters'''
58 del_resp = self.metric.deleteMetrics(self.cloudwatch_conn,delete_info)
59 return del_resp
60 #---------------------------------------------------------------------------------------------------------------------------
61 def list_metrics_request(self,list_info):
62 '''Comaptible API using normalized parameters'''
63 list_resp = self.metric.listMetrics(self.cloudwatch_conn,list_info)
64 return list_resp
65 #---------------------------------------------------------------------------------------------------------------------------
66 def read_metrics_data(self,list_info):
67 '''Comaptible API using normalized parameters
68 Read all metric data related to a specified metric'''
69 data_resp=self.metric.metricsData(self.cloudwatch_conn,list_info)
70 return data_resp
71 #---------------------------------------------------------------------------------------------------------------------------
72
73 def metric_calls(self,message,aws_conn):
74 """Gets the message from the common consumer"""
75
76 try:
77 self.cloudwatch_conn = aws_conn['cloudwatch_connection']
78 self.ec2_conn = aws_conn['ec2_connection']
79
80 metric_info = json.loads(message.value)
81 metric_response = dict()
82
83 if metric_info['vim_type'] == 'AWS':
84 log.debug ("VIM support : AWS")
85
86 # Check the Functionlity that needs to be performed: topic = 'alarms'/'metrics'/'Access_Credentials'
87 if message.topic == "metric_request":
88 log.info("Action required against: %s" % (message.topic))
89
90 if message.key == "create_metric_request":
91 if self.check_resource(metric_info['metric_create']['resource_uuid']) == True:
92 metric_resp = self.create_metric_request(metric_info['metric_create']) #alarm_info = message.value
93 metric_response['schema_version'] = metric_info['schema_version']
94 metric_response['schema_type'] = "create_metric_response"
95 metric_response['metric_create_response'] = metric_resp
96 payload = json.dumps(metric_response)
97 file = open('../../core/models/create_metric_resp.json','wb').write((payload))
98 self.producer.create_metrics_resp(key='create_metric_response',message=payload,topic = 'metric_response')
99
100 log.info("Metric configured: %s", metric_resp)
101 return metric_response
102
103 elif message.key == "update_metric_request":
104 if self.check_resource(metric_info['metric_create']['resource_uuid']) == True:
105 update_resp = self.update_metric_request(metric_info['metric_create'])
106 metric_response['schema_version'] = metric_info['schema_version']
107 metric_response['schema_type'] = "update_metric_response"
108 metric_response['metric_update_response'] = update_resp
109 payload = json.dumps(metric_response)
110 file = open('../../core/models/update_metric_resp.json','wb').write((payload))
111 self.producer.update_metric_response(key='update_metric_response',message=payload,topic = 'metric_response')
112
113 log.info("Metric Updates: %s",metric_response)
114 return metric_response
115
116 elif message.key == "delete_metric_request":
117 if self.check_resource(metric_info['resource_uuid']) == True:
118 del_resp=self.delete_metric_request(metric_info)
119 payload = json.dumps(del_resp)
120 file = open('../../core/models/delete_metric_resp.json','wb').write((payload))
121 self.producer.delete_metric_response(key='delete_metric_response',message=payload,topic = 'metric_response')
122
123 log.info("Metric Deletion Not supported in AWS : %s",del_resp)
124 return del_resp
125
126 elif message.key == "list_metric_request":
127 if self.check_resource(metric_info['metrics_list_request']['resource_uuid']) == True:
128 list_resp = self.list_metrics_request(metric_info['metrics_list_request'])
129 metric_response['schema_version'] = metric_info['schema_version']
130 metric_response['schema_type'] = "list_metric_response"
131 metric_response['correlation_id'] = metric_info['metrics_list_request']['correlation_id']
132 metric_response['vim_type'] = metric_info['vim_type']
133 metric_response['metrics_list'] = list_resp
134 payload = json.dumps(metric_response)
135 file = open('../../core/models/list_metric_resp.json','wb').write((payload))
136 self.producer.list_metric_response(key='list_metrics_response',message=payload,topic = 'metric_response')
137
138 log.info("Metric List: %s",metric_response)
139 return metric_response
140
141 elif message.key == "read_metric_data_request":
142 if self.check_resource(metric_info['resource_uuid']) == True:
143 data_resp = self.read_metrics_data(metric_info)
144 metric_response['schema_version'] = metric_info['schema_version']
145 metric_response['schema_type'] = "read_metric_data_response"
146 metric_response['metric_name'] = metric_info['metric_name']
147 metric_response['metric_uuid'] = metric_info['metric_uuid']
148 metric_response['correlation_id'] = metric_info['correlation_uuid']
149 metric_response['resource_uuid'] = metric_info['resource_uuid']
150 metric_response['tenant_uuid'] = metric_info['tenant_uuid']
151 metric_response['metrics_data'] = data_resp
152 payload = json.dumps(metric_response)
153 file = open('../../core/models/read_metric_data_resp.json','wb').write((payload))
154 self.producer.read_metric_data_response(key='read_metric_data_response',message=payload,topic = 'metric_response')
155
156 log.info("Metric Data Response: %s",metric_response)
157 return metric_response
158
159 else:
160 log.debug("Unknown key, no action will be performed")
161 else:
162 log.info("Message topic not relevant to this plugin: %s",
163 message.topic)
164
165 except Exception as e:
166 log.error("Consumer exception: %s", str(e))
167
168 #---------------------------------------------------------------------------------------------------------------------------
169 def check_resource(self,resource_uuid):
170
171 '''Checking the resource_uuid is present in EC2 instances'''
172 try:
173 check_resp = dict()
174 instances = self.ec2_conn.get_all_instance_status()
175 status_resource = False
176
177 #resource_id
178 for instance_id in instances:
179 instance_id = str(instance_id).split(':')[1]
180 if instance_id == resource_uuid:
181 check_resp['resource_uuid'] = resource_uuid
182 status_resource = True
183 else:
184 status_resource = False
185
186 #status
187 return status_resource
188
189 except Exception as e:
190 log.error("Error in Plugin Inputs %s",str(e))
191 #---------------------------------------------------------------------------------------------------------------------------
192