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__
)
45 def createMetrics(self
,cloudwatch_conn
,metric_info
):
48 '''createMetrics will be returning the metric_uuid=0 and
49 status=True when the metric is supported by AWS'''
51 supported
=self
.check_metric(metric_info
['metric_name'])
53 metric_resp
['resource_uuid'] = metric_info
['resource_uuid']
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
)
60 metric_resp
['status'] = False
61 metric_resp
['metric_uuid'] = None
62 log
.error("Metric name is not supported")
66 except Exception as e
:
67 log
.error("Metric Configuration Failed: " + str(e
))
68 #-----------------------------------------------------------------------------------------------------------------------------
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'] == 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')
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']
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
99 log
.error("Collection Period should be a multiple of 60")
103 log
.error("Metric name is not supported")
106 except Exception as e
:
107 log
.error("Error returning Metrics Data" + str(e
))
109 #-----------------------------------------------------------------------------------------------------------------------------
110 def updateMetrics(self
,cloudwatch_conn
,metric_info
):
112 '''updateMetrics will be returning the metric_uuid=0 and
113 status=True when the metric is supported by AWS'''
115 supported
=self
.check_metric(metric_info
['metric_name'])
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
)
123 update_resp
['status'] = False
124 update_resp
['metric_uuid'] = None
125 log
.error("Metric name is not supported")
129 except Exception as e
:
130 log
.error("Error in Update Metrics" + str(e
))
131 #-----------------------------------------------------------------------------------------------------------------------------
132 def deleteMetrics(self
,cloudwatch_conn
,del_info
):
134 ''' " Not supported in AWS"
135 Returning the required parameters with status = False'''
137 supported
=self
.check_metric(del_info
['metric_name'])
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
)
153 log
.error("Metric name is not supported")
156 except Exception as e
:
157 log
.error(" Metric Deletion Not supported in AWS : " + str(e
))
158 #------------------------------------------------------------------------------------------------------------------------------------
160 def listMetrics(self
,cloudwatch_conn
,list_info
):
162 '''Returns the list of available AWS/EC2 metrics on which
163 alarms have been configured and the metrics are being monitored'''
165 supported
= self
.check_metric(list_info
['metric_name'])
166 if supported
['status'] == True:
168 metrics_data
= dict()
170 #To get the list of associated metrics with the alarms
171 alarms
= cloudwatch_conn
.describe_alarms()
173 if list_info
['metric_name'] == "":
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
)
183 log
.info(metrics_list
)
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
)
197 log
.debug("Metrics List : %s",metrics_list
)
199 log
.error("Metric name is not supported")
202 except Exception as e
:
203 log
.error("Error in Getting Metric List " + str(e
))
205 #------------------------------------------------------------------------------------------------------------------------------------
207 def check_metric(self
,metric_name
):
209 ''' Checking whether the metric is supported by AWS '''
213 if metric_name
== 'CPU_UTILIZATION':
214 metric_name
= 'CPUUtilization'
216 elif metric_name
== 'DISK_READ_OPS':
217 metric_name
= 'DiskReadOps'
219 elif metric_name
== 'DISK_WRITE_OPS':
220 metric_name
= 'DiskWriteOps'
222 elif metric_name
== 'DISK_READ_BYTES':
223 metric_name
= 'DiskReadBytes'
225 elif metric_name
== 'DISK_WRITE_BYTES':
226 metric_name
= 'DiskWriteBytes'
228 elif metric_name
== 'PACKETS_RECEIVED':
229 metric_name
= 'NetworkPacketsIn'
231 elif metric_name
== 'PACKETS_SENT':
232 metric_name
= 'NetworkPacketsOut'
234 elif metric_name
== "":
237 log
.info("Metric Not Supported by AWS plugin ")
240 metric_status
= False
241 log
.info("Metric Not Supported by AWS plugin ")
242 check_resp
['metric_name'] = metric_name
244 if metric_status
== True:
245 check_resp
['status'] = True
247 check_resp
['status'] = False
251 except Exception as e
:
252 log
.error("Error in Plugin Inputs %s",str(e
))
253 #--------------------------------------------------------------------------------------------------------------------------------------