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 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')
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']
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 except Exception as e
:
100 log
.error("Error returning Metrics Data" + str(e
))
102 #-----------------------------------------------------------------------------------------------------------------------------
103 def updateMetrics(self
,cloudwatch_conn
,metric_info
):
105 '''updateMetrics will be returning the metric_uuid=0 and
106 status=True when the metric is supported by AWS'''
108 supported
=self
.check_metric(metric_info
['metric_name'])
110 if supported
['status'] == True:
111 update_resp
['status'] = True
112 update_resp
['metric_uuid'] = 0
114 update_resp
['status'] = False
115 update_resp
['metric_uuid'] = None
117 update_resp
['resource_uuid'] = metric_info
['resource_uuid']
118 log
.debug("Metric Updated : %s", update_resp
)
121 except Exception as e
:
122 log
.error("Error in Update Metrics" + str(e
))
123 #-----------------------------------------------------------------------------------------------------------------------------
124 def deleteMetrics(self
,cloudwatch_conn
,del_info
):
126 ''' " Not supported in AWS"
127 Returning the required parameters with status = False'''
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
)
143 except Exception as e
:
144 log
.error(" Metric Deletion Not supported in AWS : " + str(e
))
145 #------------------------------------------------------------------------------------------------------------------------------------
147 def listMetrics(self
,cloudwatch_conn
,list_info
):
149 '''Returns the list of available AWS/EC2 metrics on which
150 alarms have been configured and the metrics are being monitored'''
152 supported
= self
.check_metric(list_info
['metric_name'])
155 metrics_data
= dict()
156 metrics_info
= dict()
158 #To get the list of associated metrics with the alarms
159 alarms
= cloudwatch_conn
.describe_alarms()
161 if list_info
['metric_name'] == None:
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
)
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
)
182 log
.debug("Metrics List : %s",metrics_list
)
185 except Exception as e
:
186 log
.error("Error in Getting Metric List " + str(e
))
188 #------------------------------------------------------------------------------------------------------------------------------------
190 def check_metric(self
,metric_name
):
192 ''' Checking whether the metric is supported by AWS '''
196 if metric_name
== 'CPU_UTILIZATION':
197 metric_name
= 'CPUUtilization'
199 elif metric_name
== 'DISK_READ_OPS':
200 metric_name
= 'DiskReadOps'
202 elif metric_name
== 'DISK_WRITE_OPS':
203 metric_name
= 'DiskWriteOps'
205 elif metric_name
== 'DISK_READ_BYTES':
206 metric_name
= 'DiskReadBytes'
208 elif metric_name
== 'DISK_WRITE_BYTES':
209 metric_name
= 'DiskWriteBytes'
211 elif metric_name
== 'PACKETS_RECEIVED':
212 metric_name
= 'NetworkPacketsIn'
214 elif metric_name
== 'PACKETS_SENT':
215 metric_name
= 'NetworkPacketsOut'
219 log
.info("Metric Not Supported by AWS plugin ")
220 metric_status
= False
221 check_resp
['metric_name'] = metric_name
223 if metric_status
== True:
224 check_resp
['status'] = True
226 except Exception as e
:
227 log
.error("Error in Plugin Inputs %s",str(e
))
228 #--------------------------------------------------------------------------------------------------------------------------------------