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