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 Connecting with AWS services --CloudWatch/EC2 using Required keys
26 __author__
= "Wajeeha Hamid"
27 __date__
= "18-September-2017"
36 import boto
.ec2
.cloudwatch
37 import boto
.ec2
.connection
39 from boto
.ec2
.cloudwatch
.alarm
import MetricAlarm
40 from boto
.ec2
.cloudwatch
.dimension
import Dimension
41 from boto
.sns
import connect_to_region
42 from boto
.utils
import get_instance_metadata
45 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
47 log
= logging
.getLogger(__name__
)
50 """Connection Establishement with AWS -- VPC/EC2/CloudWatch"""
51 #-----------------------------------------------------------------------------------------------------------------------------
52 def setEnvironment(self
):
54 """Credentials for connecting to AWS-CloudWatch"""
55 #Reads from the environment variables
56 self
.AWS_KEY
= os
.environ
.get("AWS_ACCESS_KEY_ID")
57 self
.AWS_SECRET
= os
.environ
.get("AWS_SECRET_ACCESS_KEY")
58 self
.AWS_REGION
= os
.environ
.get("AWS_EC2_REGION","us-west-2")
60 #TODO Read from the cloudwatch_credentials.txt file
62 return self
.connection_instance()
63 except Exception as e
:
64 log
.error("AWS Credentials not configured, Try setting the access credentials first %s: ",str(e
))
65 #-----------------------------------------------------------------------------------------------------------------------------
66 def connection_instance(self
):
69 self
.vpc_conn
= boto
.vpc
.connect_to_region(self
.AWS_REGION
,
70 aws_access_key_id
=self
.AWS_KEY
,
71 aws_secret_access_key
=self
.AWS_SECRET
)
75 self
.ec2_conn
= boto
.ec2
.connect_to_region(self
.AWS_REGION
,
76 aws_access_key_id
=self
.AWS_KEY
,
77 aws_secret_access_key
=self
.AWS_SECRET
)
79 """ TODO : Required to add actions against alarms when needed """
80 #self.sns = connect_to_region(self.AWS_REGION)
81 #self.topics = self.sns.get_all_topics()
82 #self.topic = self.topics[u'ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn']
84 #Cloudwatch Connection
85 self
.cloudwatch_conn
= boto
.ec2
.cloudwatch
.connect_to_region(
87 aws_access_key_id
=self
.AWS_KEY
,
88 aws_secret_access_key
=self
.AWS_SECRET
)
89 connection_dict
= dict()
90 connection_dict
['ec2_connection'] = self
.ec2_conn
91 connection_dict
['cloudwatch_connection'] = self
.cloudwatch_conn
92 return connection_dict
94 except Exception as e
:
95 log
.error("Failed to Connect with AWS %s: ",str(e
))