Updated logging for CloudWatch plugin
[osm/MON.git] / osm_mon / plugins / CloudWatch / connection.py
1 ##
2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
4 # All Rights Reserved.
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
20 ##
21
22 '''
23 Connecting with AWS services --CloudWatch/EC2 using Required keys
24 '''
25
26 __author__ = "Wajeeha Hamid"
27 __date__ = "18-September-2017"
28
29 import sys
30 import os
31
32 try:
33 import boto
34 import boto.ec2
35 import boto.vpc
36 import boto.ec2.cloudwatch
37 import boto.ec2.connection
38 import logging
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
43
44 except:
45 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
46
47 log = logging.getLogger(__name__)
48
49 class Connection():
50 """Connection Establishement with AWS -- VPC/EC2/CloudWatch"""
51 #-----------------------------------------------------------------------------------------------------------------------------
52 def setEnvironment(self):
53 try:
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")
59
60 #TODO Read from the cloudwatch_credentials.txt file
61
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):
67 try:
68 #VPC Connection
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)
72
73
74 #EC2 Connection
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)
78
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']
83
84 #Cloudwatch Connection
85 self.cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
86 self.AWS_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
93
94 except Exception as e:
95 log.error("Failed to Connect with AWS %s: ",str(e))
96