5faa659853f5bc4719829f873f5674c6682926b3
[osm/MON.git] / 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 sys
30 import datetime
31 import json
32 import logging as log
33
34 try:
35 import boto
36 import boto.ec2
37 import boto.vpc
38 import boto.ec2.cloudwatch
39 import boto.ec2.connection
40 except:
41 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
42
43
44
45 class Metrics():
46
47 def createMetrics(self,cloudwatch_conn,metric_info):
48 try:
49
50 '''createMetrics will be returning the metric_uuid=0 and
51 status=True when the metric is supported by AWS'''
52
53 supported=self.check_metric(metric_info['metric_name'])
54 metric_resp = dict()
55 if supported['status'] == True:
56 metric_resp['status'] = True
57 metric_resp['metric_uuid'] = 0
58 else:
59 metric_resp['status'] = False
60 metric_resp['metric_uuid'] = None
61
62 metric_resp['resource_uuid'] = metric_info['resource_uuid']
63 log.debug("Metrics Configured Succesfully : %s" , metric_resp)
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 return False
104
105 except Exception as e:
106 log.error("Error returning Metrics Data" + str(e))
107
108 #-----------------------------------------------------------------------------------------------------------------------------
109 def updateMetrics(self,cloudwatch_conn,metric_info):
110
111 '''updateMetrics will be returning the metric_uuid=0 and
112 status=True when the metric is supported by AWS'''
113 try:
114 supported=self.check_metric(metric_info['metric_name'])
115 update_resp = dict()
116 if supported['status'] == True:
117 update_resp['status'] = True
118 update_resp['metric_uuid'] = 0
119 else:
120 update_resp['status'] = False
121 update_resp['metric_uuid'] = None
122
123 update_resp['resource_uuid'] = metric_info['resource_uuid']
124 log.debug("Metric Updated : %s", update_resp)
125 return update_resp
126
127 except Exception as e:
128 log.error("Error in Update Metrics" + str(e))
129 #-----------------------------------------------------------------------------------------------------------------------------
130 def deleteMetrics(self,cloudwatch_conn,del_info):
131
132 ''' " Not supported in AWS"
133 Returning the required parameters with status = False'''
134 try:
135 supported=self.check_metric(del_info['metric_name'])
136 metric_resp = dict()
137 del_resp = dict()
138 if supported['status'] == True:
139 del_resp['schema_version'] = del_info['schema_version']
140 del_resp['schema_type'] = "delete_metric_response"
141 del_resp['metric_name'] = del_info['metric_name']
142 del_resp['metric_uuid'] = del_info['metric_uuid']
143 del_resp['resource_uuid'] = del_info['resource_uuid']
144 # TODO : yet to finalize
145 del_resp['tenant_uuid'] = del_info['tenant_uuid']
146 del_resp['correlation_id'] = del_info['correlation_uuid']
147 del_resp['status'] = False
148 log.info("Metric Deletion Not supported in AWS : %s",del_resp)
149 return del_resp
150 else:
151 return False
152
153 except Exception as e:
154 log.error(" Metric Deletion Not supported in AWS : " + str(e))
155 #------------------------------------------------------------------------------------------------------------------------------------
156
157 def listMetrics(self,cloudwatch_conn ,list_info):
158
159 '''Returns the list of available AWS/EC2 metrics on which
160 alarms have been configured and the metrics are being monitored'''
161 try:
162 supported = self.check_metric(list_info['metric_name'])
163 if supported['status'] == True:
164 metrics_list = []
165 metrics_data = dict()
166
167 #To get the list of associated metrics with the alarms
168 alarms = cloudwatch_conn.describe_alarms()
169 itr = 0
170 if list_info['metric_name'] == "":
171 for alarm in alarms:
172 metrics_info = dict()
173 instance_id = str(alarm.dimensions['InstanceId']).split("'")[1]
174 metrics_info['metric_name'] = str(alarm.metric)
175 metrics_info['metric_uuid'] = 0
176 metrics_info['metric_unit'] = str(alarm.unit)
177 metrics_info['resource_uuid'] = instance_id
178 metrics_list.insert(itr,metrics_info)
179 itr += 1
180 print metrics_list
181 return metrics_list
182 else:
183 for alarm in alarms:
184 metrics_info = dict()
185 if alarm.metric == supported['metric_name']:
186 instance_id = str(alarm.dimensions['InstanceId']).split("'")[1]
187 metrics_info['metric_name'] = str(alarm.metric)
188 metrics_info['metric_uuid'] = 0
189 metrics_info['metric_unit'] = str(alarm.unit)
190 metrics_info['resource_uuid'] = instance_id
191 metrics_list.insert(itr,metrics_info)
192 itr += 1
193 return metrics_list
194 log.debug("Metrics List : %s",metrics_list)
195 else:
196 return False
197
198 except Exception as e:
199 log.error("Error in Getting Metric List " + str(e))
200
201 #------------------------------------------------------------------------------------------------------------------------------------
202
203 def check_metric(self,metric_name):
204
205 ''' Checking whether the metric is supported by AWS '''
206 try:
207 check_resp = dict()
208 # metric_name
209 if metric_name == 'CPU_UTILIZATION':
210 metric_name = 'CPUUtilization'
211 metric_status = True
212 elif metric_name == 'DISK_READ_OPS':
213 metric_name = 'DiskReadOps'
214 metric_status = True
215 elif metric_name == 'DISK_WRITE_OPS':
216 metric_name = 'DiskWriteOps'
217 metric_status = True
218 elif metric_name == 'DISK_READ_BYTES':
219 metric_name = 'DiskReadBytes'
220 metric_status = True
221 elif metric_name == 'DISK_WRITE_BYTES':
222 metric_name = 'DiskWriteBytes'
223 metric_status = True
224 elif metric_name == 'PACKETS_RECEIVED':
225 metric_name = 'NetworkPacketsIn'
226 metric_status = True
227 elif metric_name == 'PACKETS_SENT':
228 metric_name = 'NetworkPacketsOut'
229 metric_status = True
230 elif metric_name == "":
231 metric_name = None
232 metric_status = True
233 log.info("Metric Not Supported by AWS plugin ")
234 else:
235 metric_name = None
236 metric_status = False
237 log.info("Metric Not Supported by AWS plugin ")
238 check_resp['metric_name'] = metric_name
239 #status
240 if metric_status == True:
241 check_resp['status'] = True
242 else:
243 check_resp['status'] = False
244
245 return check_resp
246
247 except Exception as e:
248 log.error("Error in Plugin Inputs %s",str(e))
249 #--------------------------------------------------------------------------------------------------------------------------------------
250
251
252
253
254
255
256
257