5faa659853f5bc4719829f873f5674c6682926b3
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"
38 import boto
.ec2
.cloudwatch
39 import boto
.ec2
.connection
41 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
47 def createMetrics(self
,cloudwatch_conn
,metric_info
):
50 '''createMetrics will be returning the metric_uuid=0 and
51 status=True when the metric is supported by AWS'''
53 supported
=self
.check_metric(metric_info
['metric_name'])
55 if supported
['status'] == True:
56 metric_resp
['status'] = True
57 metric_resp
['metric_uuid'] = 0
59 metric_resp
['status'] = False
60 metric_resp
['metric_uuid'] = None
62 metric_resp
['resource_uuid'] = metric_info
['resource_uuid']
63 log
.debug("Metrics Configured Succesfully : %s" , metric_resp
)
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")
105 except Exception as e
:
106 log
.error("Error returning Metrics Data" + str(e
))
108 #-----------------------------------------------------------------------------------------------------------------------------
109 def updateMetrics(self
,cloudwatch_conn
,metric_info
):
111 '''updateMetrics will be returning the metric_uuid=0 and
112 status=True when the metric is supported by AWS'''
114 supported
=self
.check_metric(metric_info
['metric_name'])
116 if supported
['status'] == True:
117 update_resp
['status'] = True
118 update_resp
['metric_uuid'] = 0
120 update_resp
['status'] = False
121 update_resp
['metric_uuid'] = None
123 update_resp
['resource_uuid'] = metric_info
['resource_uuid']
124 log
.debug("Metric Updated : %s", update_resp
)
127 except Exception as e
:
128 log
.error("Error in Update Metrics" + str(e
))
129 #-----------------------------------------------------------------------------------------------------------------------------
130 def deleteMetrics(self
,cloudwatch_conn
,del_info
):
132 ''' " Not supported in AWS"
133 Returning the required parameters with status = False'''
135 supported
=self
.check_metric(del_info
['metric_name'])
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
)
153 except Exception as e
:
154 log
.error(" Metric Deletion Not supported in AWS : " + str(e
))
155 #------------------------------------------------------------------------------------------------------------------------------------
157 def listMetrics(self
,cloudwatch_conn
,list_info
):
159 '''Returns the list of available AWS/EC2 metrics on which
160 alarms have been configured and the metrics are being monitored'''
162 supported
= self
.check_metric(list_info
['metric_name'])
163 if supported
['status'] == True:
165 metrics_data
= dict()
167 #To get the list of associated metrics with the alarms
168 alarms
= cloudwatch_conn
.describe_alarms()
170 if list_info
['metric_name'] == "":
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
)
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
)
194 log
.debug("Metrics List : %s",metrics_list
)
198 except Exception as e
:
199 log
.error("Error in Getting Metric List " + str(e
))
201 #------------------------------------------------------------------------------------------------------------------------------------
203 def check_metric(self
,metric_name
):
205 ''' Checking whether the metric is supported by AWS '''
209 if metric_name
== 'CPU_UTILIZATION':
210 metric_name
= 'CPUUtilization'
212 elif metric_name
== 'DISK_READ_OPS':
213 metric_name
= 'DiskReadOps'
215 elif metric_name
== 'DISK_WRITE_OPS':
216 metric_name
= 'DiskWriteOps'
218 elif metric_name
== 'DISK_READ_BYTES':
219 metric_name
= 'DiskReadBytes'
221 elif metric_name
== 'DISK_WRITE_BYTES':
222 metric_name
= 'DiskWriteBytes'
224 elif metric_name
== 'PACKETS_RECEIVED':
225 metric_name
= 'NetworkPacketsIn'
227 elif metric_name
== 'PACKETS_SENT':
228 metric_name
= 'NetworkPacketsOut'
230 elif metric_name
== "":
233 log
.info("Metric Not Supported by AWS plugin ")
236 metric_status
= False
237 log
.info("Metric Not Supported by AWS plugin ")
238 check_resp
['metric_name'] = metric_name
240 if metric_status
== True:
241 check_resp
['status'] = True
243 check_resp
['status'] = False
247 except Exception as e
:
248 log
.error("Error in Plugin Inputs %s",str(e
))
249 #--------------------------------------------------------------------------------------------------------------------------------------