2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
23 AWS-Plugin implements all the methods of MON to interact with AWS using the BOTO client
26 __author__
= "Wajeeha Hamid"
27 __date__
= "18-Sept-2017"
36 import boto
.ec2
.cloudwatch
37 import boto
.ec2
.connection
39 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
41 log
= logging
.getLogger(__name__
)
46 def createMetrics(self
, cloudwatch_conn
, metric_info
):
49 """createMetrics will be returning the metric_uuid=0 and
50 status=True when the metric is supported by AWS"""
52 supported
= self
.check_metric(metric_info
['metric_name'])
54 metric_resp
['resource_uuid'] = metric_info
['resource_uuid']
56 if supported
['status']:
57 metric_resp
['status'] = True
58 metric_resp
['metric_uuid'] = 0
59 log
.debug("Metrics Configured Successfully : %s", metric_resp
)
61 metric_resp
['status'] = False
62 metric_resp
['metric_uuid'] = None
63 log
.error("Metric name is not supported")
67 except Exception as e
:
68 log
.error("Metric Configuration Failed: " + str(e
))
70 def metricsData(self
, cloudwatch_conn
, data_info
):
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"""
78 metric_info_dict
= dict()
82 supported
= self
.check_metric(data_info
['metric_name'])
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(
89 data_info
['collection_period'])),
90 datetime
.datetime
.utcnow(),
91 supported
['metric_name'], 'AWS/EC2', 'Maximum',
93 'InstanceId': data_info
['resource_uuid']},
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']
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
105 log
.error("Collection Period should be a multiple of 60")
109 log
.error("Metric name is not supported")
112 except Exception as e
:
113 log
.error("Error returning Metrics Data" + str(e
))
115 def updateMetrics(self
, cloudwatch_conn
, metric_info
):
117 """updateMetrics will be returning the metric_uuid=0 and
118 status=True when the metric is supported by AWS"""
120 supported
= self
.check_metric(metric_info
['metric_name'])
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
)
128 update_resp
['status'] = False
129 update_resp
['metric_uuid'] = None
130 log
.error("Metric name is not supported")
134 except Exception as e
:
135 log
.error("Error in Update Metrics" + str(e
))
137 def deleteMetrics(self
, cloudwatch_conn
, del_info
):
139 """ " Not supported in AWS"
140 Returning the required parameters with status = False"""
142 supported
= self
.check_metric(del_info
['metric_name'])
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
)
158 log
.error("Metric name is not supported")
161 except Exception as e
:
162 log
.error(" Metric Deletion Not supported in AWS : " + str(e
))
164 def listMetrics(self
, cloudwatch_conn
, list_info
):
166 """Returns the list of available AWS/EC2 metrics on which
167 alarms have been configured and the metrics are being monitored"""
169 supported
= self
.check_metric(list_info
['metric_name'])
170 if supported
['status']:
172 metrics_data
= dict()
174 # To get the list of associated metrics with the alarms
175 alarms
= cloudwatch_conn
.describe_alarms()
177 if list_info
['metric_name'] == "":
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
)
187 log
.info(metrics_list
)
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
)
202 log
.error("Metric name is not supported")
205 except Exception as e
:
206 log
.error("Error in Getting Metric List " + str(e
))
208 def check_metric(self
, metric_name
):
210 """ Checking whether the metric is supported by AWS """
214 if metric_name
== 'CPU_UTILIZATION':
215 metric_name
= 'CPUUtilization'
217 elif metric_name
== 'DISK_READ_OPS':
218 metric_name
= 'DiskReadOps'
220 elif metric_name
== 'DISK_WRITE_OPS':
221 metric_name
= 'DiskWriteOps'
223 elif metric_name
== 'DISK_READ_BYTES':
224 metric_name
= 'DiskReadBytes'
226 elif metric_name
== 'DISK_WRITE_BYTES':
227 metric_name
= 'DiskWriteBytes'
229 elif metric_name
== 'PACKETS_RECEIVED':
230 metric_name
= 'NetworkPacketsIn'
232 elif metric_name
== 'PACKETS_SENT':
233 metric_name
= 'NetworkPacketsOut'
235 elif metric_name
== "":
238 log
.info("Metric Not Supported by AWS plugin ")
241 metric_status
= False
242 log
.info("Metric Not Supported by AWS plugin ")
243 check_resp
['metric_name'] = metric_name
246 check_resp
['status'] = True
248 check_resp
['status'] = False
252 except Exception as e
:
253 log
.error("Error in Plugin Inputs %s", str(e
))