Updated Code of AWS plugin including Minor alternations of consumer/producer apps
[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 metric_stats=cloudwatch_conn.get_metric_statistics(60, datetime.datetime.utcnow() - datetime.timedelta(seconds=int(data_info['collection_period'])),
85 datetime.datetime.utcnow(),supported['metric_name'],'AWS/EC2', 'Maximum',
86 dimensions={'InstanceId':data_info['resource_uuid']}, unit='Percent')
87
88 index = 0
89 for itr in range (len(metric_stats)):
90 timestamp_arr[index] = str(metric_stats[itr]['Timestamp'])
91 value_arr[index] = metric_stats[itr]['Maximum']
92 index +=1
93
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
99 except Exception as e:
100 log.error("Error returning Metrics Data" + str(e))
101
102 #-----------------------------------------------------------------------------------------------------------------------------
103 def updateMetrics(self,cloudwatch_conn,metric_info):
104
105 '''updateMetrics will be returning the metric_uuid=0 and
106 status=True when the metric is supported by AWS'''
107 try:
108 supported=self.check_metric(metric_info['metric_name'])
109 update_resp = dict()
110 if supported['status'] == True:
111 update_resp['status'] = True
112 update_resp['metric_uuid'] = 0
113 else:
114 update_resp['status'] = False
115 update_resp['metric_uuid'] = None
116
117 update_resp['resource_uuid'] = metric_info['resource_uuid']
118 log.debug("Metric Updated : %s", update_resp)
119 return update_resp
120
121 except Exception as e:
122 log.error("Error in Update Metrics" + str(e))
123 #-----------------------------------------------------------------------------------------------------------------------------
124 def deleteMetrics(self,cloudwatch_conn,del_info):
125
126 ''' " Not supported in AWS"
127 Returning the required parameters with status = False'''
128 try:
129
130 del_resp = dict()
131 del_resp['schema_version'] = del_info['schema_version']
132 del_resp['schema_type'] = "delete_metric_response"
133 del_resp['metric_name'] = del_info['metric_name']
134 del_resp['metric_uuid'] = del_info['metric_uuid']
135 del_resp['resource_uuid'] = del_info['resource_uuid']
136 # TODO : yet to finalize
137 del_resp['tenant_uuid'] = del_info['tenant_uuid']
138 del_resp['correlation_id'] = del_info['correlation_uuid']
139 del_resp['status'] = False
140 log.info("Metric Deletion Not supported in AWS : %s",del_resp)
141 return del_resp
142
143 except Exception as e:
144 log.error(" Metric Deletion Not supported in AWS : " + str(e))
145 #------------------------------------------------------------------------------------------------------------------------------------
146
147 def listMetrics(self,cloudwatch_conn ,list_info):
148
149 '''Returns the list of available AWS/EC2 metrics on which
150 alarms have been configured and the metrics are being monitored'''
151 try:
152 supported = self.check_metric(list_info['metric_name'])
153
154 metrics_list = []
155 metrics_data = dict()
156 metrics_info = dict()
157
158 #To get the list of associated metrics with the alarms
159 alarms = cloudwatch_conn.describe_alarms()
160 itr = 0
161 if list_info['metric_name'] == None:
162 for alarm in alarms:
163 instance_id = str(alarm.dimensions['InstanceId']).split("'")[1]
164 metrics_info['metric_name'] = str(alarm.metric)
165 metrics_info['metric_uuid'] = 0
166 metrics_info['metric_unit'] = str(alarm.unit)
167 metrics_info['resource_uuid'] = instance_id
168 metrics_list.insert(itr,metrics_info)
169 itr += 1
170 else:
171 for alarm in alarms:
172 print supported['metric_name']
173 if alarm.metric == supported['metric_name']:
174 instance_id = str(alarm.dimensions['InstanceId']).split("'")[1]
175 metrics_info['metric_name'] = str(alarm.metric)
176 metrics_info['metric_uuid'] = 0
177 metrics_info['metric_unit'] = str(alarm.unit)
178 metrics_info['resource_uuid'] = instance_id
179 metrics_list.insert(itr,metrics_info)
180 itr += 1
181
182 log.debug("Metrics List : %s",metrics_list)
183 return metrics_list
184
185 except Exception as e:
186 log.error("Error in Getting Metric List " + str(e))
187
188 #------------------------------------------------------------------------------------------------------------------------------------
189
190 def check_metric(self,metric_name):
191
192 ''' Checking whether the metric is supported by AWS '''
193 try:
194 check_resp = dict()
195 #metric_name
196 if metric_name == 'CPU_UTILIZATION':
197 metric_name = 'CPUUtilization'
198 metric_status = True
199 elif metric_name == 'DISK_READ_OPS':
200 metric_name = 'DiskReadOps'
201 metric_status = True
202 elif metric_name == 'DISK_WRITE_OPS':
203 metric_name = 'DiskWriteOps'
204 metric_status = True
205 elif metric_name == 'DISK_READ_BYTES':
206 metric_name = 'DiskReadBytes'
207 metric_status = True
208 elif metric_name == 'DISK_WRITE_BYTES':
209 metric_name = 'DiskWriteBytes'
210 metric_status = True
211 elif metric_name == 'PACKETS_RECEIVED':
212 metric_name = 'NetworkPacketsIn'
213 metric_status = True
214 elif metric_name == 'PACKETS_SENT':
215 metric_name = 'NetworkPacketsOut'
216 metric_status = True
217 else:
218 metric_name = None
219 log.info("Metric Not Supported by AWS plugin ")
220 metric_status = False
221 check_resp['metric_name'] = metric_name
222 #status
223 if metric_status == True:
224 check_resp['status'] = True
225 return check_resp
226 except Exception as e:
227 log.error("Error in Plugin Inputs %s",str(e))
228 #--------------------------------------------------------------------------------------------------------------------------------------
229
230
231
232
233
234
235
236