Refactors code in OpenStack plugin
[osm/MON.git] / osm_mon / plugins / CloudWatch / metrics.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-Sept-2017"
28
29 import datetime
30 import logging
31
32 try:
33 import boto
34 import boto.ec2
35 import boto.vpc
36 import boto.ec2.cloudwatch
37 import boto.ec2.connection
38 except:
39 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
40
41 log = logging.getLogger(__name__)
42
43 class Metrics():
44
45 def createMetrics(self,cloudwatch_conn,metric_info):
46 try:
47
48 '''createMetrics will be returning the metric_uuid=0 and
49 status=True when the metric is supported by AWS'''
50
51 supported=self.check_metric(metric_info['metric_name'])
52 metric_resp = dict()
53 metric_resp['resource_uuid'] = metric_info['resource_uuid']
54
55 if supported['status'] == True:
56 metric_resp['status'] = True
57 metric_resp['metric_uuid'] = 0
58 log.debug("Metrics Configured Succesfully : %s" , metric_resp)
59 else:
60 metric_resp['status'] = False
61 metric_resp['metric_uuid'] = None
62 log.error("Metric name is not supported")
63
64 return metric_resp
65
66 except Exception as e:
67 log.error("Metric Configuration Failed: " + str(e))
68 #-----------------------------------------------------------------------------------------------------------------------------
69
70 def metricsData(self,cloudwatch_conn,data_info):
71
72 """Getting Metrics Stats for an Hour.The datapoints are
73 received after every one minute.
74 Time interval can be modified using Timedelta value"""
75
76 try:
77 metric_info = dict()
78 metric_info_dict = dict()
79 timestamp_arr = {}
80 value_arr = {}
81
82 supported=self.check_metric(data_info['metric_name'])
83
84 if supported['status'] == True:
85 if int(data_info['collection_period']) % 60 == 0:
86 metric_stats=cloudwatch_conn.get_metric_statistics(60, datetime.datetime.utcnow() - datetime.timedelta(seconds=int(data_info['collection_period'])),
87 datetime.datetime.utcnow(),supported['metric_name'],'AWS/EC2', 'Maximum',
88 dimensions={'InstanceId':data_info['resource_uuid']}, unit='Percent')
89 index = 0
90 for itr in range (len(metric_stats)):
91 timestamp_arr[index] = str(metric_stats[itr]['Timestamp'])
92 value_arr[index] = metric_stats[itr]['Maximum']
93 index +=1
94 metric_info_dict['time_series'] = timestamp_arr
95 metric_info_dict['metrics_series'] = value_arr
96 log.debug("Metrics Data : %s", metric_info_dict)
97 return metric_info_dict
98 else:
99 log.error("Collection Period should be a multiple of 60")
100 return False
101
102 else:
103 log.error("Metric name is not supported")
104 return False
105
106 except Exception as e:
107 log.error("Error returning Metrics Data" + str(e))
108
109 #-----------------------------------------------------------------------------------------------------------------------------
110 def updateMetrics(self,cloudwatch_conn,metric_info):
111
112 '''updateMetrics will be returning the metric_uuid=0 and
113 status=True when the metric is supported by AWS'''
114 try:
115 supported=self.check_metric(metric_info['metric_name'])
116 update_resp = dict()
117 update_resp['resource_uuid'] = metric_info['resource_uuid']
118 if supported['status'] == True:
119 update_resp['status'] = True
120 update_resp['metric_uuid'] = 0
121 log.debug("Metric Updated : %s", update_resp)
122 else:
123 update_resp['status'] = False
124 update_resp['metric_uuid'] = None
125 log.error("Metric name is not supported")
126
127 return update_resp
128
129 except Exception as e:
130 log.error("Error in Update Metrics" + str(e))
131 #-----------------------------------------------------------------------------------------------------------------------------
132 def deleteMetrics(self,cloudwatch_conn,del_info):
133
134 ''' " Not supported in AWS"
135 Returning the required parameters with status = False'''
136 try:
137 supported=self.check_metric(del_info['metric_name'])
138 metric_resp = dict()
139 del_resp = dict()
140 if supported['status'] == True:
141 del_resp['schema_version'] = del_info['schema_version']
142 del_resp['schema_type'] = "delete_metric_response"
143 del_resp['metric_name'] = del_info['metric_name']
144 del_resp['metric_uuid'] = del_info['metric_uuid']
145 del_resp['resource_uuid'] = del_info['resource_uuid']
146 # TODO : yet to finalize
147 del_resp['tenant_uuid'] = del_info['tenant_uuid']
148 del_resp['correlation_id'] = del_info['correlation_uuid']
149 del_resp['status'] = False
150 log.info("Metric Deletion Not supported in AWS : %s",del_resp)
151 return del_resp
152 else:
153 log.error("Metric name is not supported")
154 return False
155
156 except Exception as e:
157 log.error(" Metric Deletion Not supported in AWS : " + str(e))
158 #------------------------------------------------------------------------------------------------------------------------------------
159
160 def listMetrics(self,cloudwatch_conn ,list_info):
161
162 '''Returns the list of available AWS/EC2 metrics on which
163 alarms have been configured and the metrics are being monitored'''
164 try:
165 supported = self.check_metric(list_info['metric_name'])
166 if supported['status'] == True:
167 metrics_list = []
168 metrics_data = dict()
169
170 #To get the list of associated metrics with the alarms
171 alarms = cloudwatch_conn.describe_alarms()
172 itr = 0
173 if list_info['metric_name'] == "":
174 for alarm in alarms:
175 metrics_info = dict()
176 instance_id = str(alarm.dimensions['InstanceId']).split("'")[1]
177 metrics_info['metric_name'] = str(alarm.metric)
178 metrics_info['metric_uuid'] = 0
179 metrics_info['metric_unit'] = str(alarm.unit)
180 metrics_info['resource_uuid'] = instance_id
181 metrics_list.insert(itr,metrics_info)
182 itr += 1
183 log.info(metrics_list)
184 return metrics_list
185 else:
186 for alarm in alarms:
187 metrics_info = dict()
188 if alarm.metric == supported['metric_name']:
189 instance_id = str(alarm.dimensions['InstanceId']).split("'")[1]
190 metrics_info['metric_name'] = str(alarm.metric)
191 metrics_info['metric_uuid'] = 0
192 metrics_info['metric_unit'] = str(alarm.unit)
193 metrics_info['resource_uuid'] = instance_id
194 metrics_list.insert(itr,metrics_info)
195 itr += 1
196 return metrics_list
197 log.debug("Metrics List : %s",metrics_list)
198 else:
199 log.error("Metric name is not supported")
200 return False
201
202 except Exception as e:
203 log.error("Error in Getting Metric List " + str(e))
204
205 #------------------------------------------------------------------------------------------------------------------------------------
206
207 def check_metric(self,metric_name):
208
209 ''' Checking whether the metric is supported by AWS '''
210 try:
211 check_resp = dict()
212 # metric_name
213 if metric_name == 'CPU_UTILIZATION':
214 metric_name = 'CPUUtilization'
215 metric_status = True
216 elif metric_name == 'DISK_READ_OPS':
217 metric_name = 'DiskReadOps'
218 metric_status = True
219 elif metric_name == 'DISK_WRITE_OPS':
220 metric_name = 'DiskWriteOps'
221 metric_status = True
222 elif metric_name == 'DISK_READ_BYTES':
223 metric_name = 'DiskReadBytes'
224 metric_status = True
225 elif metric_name == 'DISK_WRITE_BYTES':
226 metric_name = 'DiskWriteBytes'
227 metric_status = True
228 elif metric_name == 'PACKETS_RECEIVED':
229 metric_name = 'NetworkPacketsIn'
230 metric_status = True
231 elif metric_name == 'PACKETS_SENT':
232 metric_name = 'NetworkPacketsOut'
233 metric_status = True
234 elif metric_name == "":
235 metric_name = None
236 metric_status = True
237 log.info("Metric Not Supported by AWS plugin ")
238 else:
239 metric_name = None
240 metric_status = False
241 log.info("Metric Not Supported by AWS plugin ")
242 check_resp['metric_name'] = metric_name
243 #status
244 if metric_status == True:
245 check_resp['status'] = True
246 else:
247 check_resp['status'] = False
248
249 return check_resp
250
251 except Exception as e:
252 log.error("Error in Plugin Inputs %s",str(e))
253 #--------------------------------------------------------------------------------------------------------------------------------------
254
255
256
257
258
259
260
261