from osm_mon.plugins.CloudWatch.plugin_alarm import plugin_alarms
from osm_mon.plugins.CloudWatch.plugin_metric import plugin_metrics
+from osm_mon.plugins.CloudWatch.connection import Connection
+from osm_mon.plugins.CloudWatch.access_credentials import AccessCredentials
from osm_mon.plugins.vRealiseOps import plugin_receiver
# Create CloudWatch alarm and metric instances
cloudwatch_alarms = plugin_alarms()
cloudwatch_metrics = plugin_metrics()
+aws_connection = Connection()
+aws_access_credentials = AccessCredentials()
#Create vROps plugin_receiver class instance
vrops_rcvr = plugin_receiver.PluginReceiver()
message, openstack_auth, auth_token)
elif vim_type == "aws":
- cloudwatch_metrics.metric_calls(message)
log.info("This message is for the CloudWatch plugin.")
+ aws_conn = aws_connection.setEnvironment()
+ cloudwatch_metrics.metric_calls(message,aws_conn)
elif vim_type == "vmware":
log.info("This metric_request message is for the vROPs plugin.")
openstack_alarms.alarming(message, openstack_auth, auth_token)
elif vim_type == "aws":
- cloudwatch_alarms.alarm_calls(message)
log.info("This message is for the CloudWatch plugin.")
+ aws_conn = aws_connection.setEnvironment()
+ cloudwatch_alarms.alarm_calls(message, aws_conn)
elif vim_type == "vmware":
log.info("This alarm_request message is for the vROPs plugin.")
auth_token = openstack_auth._authenticate(message=message)
elif vim_type == "aws":
- #TODO Access credentials later
log.info("This message is for the CloudWatch plugin.")
+ aws_access_credentials.access_credential_calls(message)
elif vim_type == "vmware":
log.info("This access_credentials message is for the vROPs plugin.")
--- /dev/null
+##
+# Copyright 2017 xFlow Research Pvt. Ltd
+# This file is part of MON module
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact with: usman.javaid@xflowresearch.com
+##
+
+'''
+Access credentials class implements all the methods to store the access credentials for AWS
+'''
+
+__author__ = "Usman Javaid"
+__date__ = "20-December-2017"
+
+import os
+import sys
+import json
+import logging
+
+logging.basicConfig(filename='MON_plugins.log',
+ format='%(asctime)s %(message)s',
+ datefmt='%m/%d/%Y %I:%M:%S %p', filemode='a',
+ level=logging.INFO)
+log = logging.getLogger(__name__)
+
+class AccessCredentials():
+
+ def access_credential_calls(self,message):
+ try:
+ message = json.loads(message.value)['access_config']
+
+ AWS_KEY = message['user']
+ AWS_SECRET = message['password']
+ AWS_REGION = message['vim_tenant_name']
+
+ os.environ['AWS_ACCESS_KEY_ID'] = AWS_KEY
+ os.environ['AWS_SECRET_ACCESS_KEY'] = AWS_SECRET
+ os.environ['AWS_EC2_REGION'] = AWS_REGION
+
+
+ #aws_credentials.txt file to save the access credentials
+ cloudwatch_credentials = open("../../plugins/CloudWatch/cloudwatch_credentials.txt","w+")
+ cloudwatch_credentials.write("AWS_ACCESS_KEY_ID="+AWS_KEY+
+ "\nAWS_SECRET_ACCESS_KEY="+AWS_SECRET+
+ "\nAWS_EC2_REGION="+AWS_REGION)
+
+ #Closing the file
+ cloudwatch_credentials.close()
+ log.info("Access credentials sourced for CloudWatch MON plugin")
+
+ except Exception as e:
+ log.error("Access credentials not provided correctly: %s", str(e))
"""Connection Establishement with AWS -- VPC/EC2/CloudWatch"""
#-----------------------------------------------------------------------------------------------------------------------------
def setEnvironment(self):
+ try:
+ """Credentials for connecting to AWS-CloudWatch"""
+ #Reads from the environment variables
+ self.AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID")
+ self.AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY")
+ self.AWS_REGION = os.environ.get("AWS_EC2_REGION","us-west-2")
- """Credentials for connecting to AWS-CloudWatch"""
- self.AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID")
- self.AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY")
- self.AWS_REGION = os.environ.get("AWS_EC2_REGION","us-west-2")
- #TOPIC = 'YOUR_TOPIC'
+ #TODO Read from the cloudwatch_credentials.txt file
+
+ return self.connection_instance()
+ except Exception as e:
+ log.error("AWS Credentials not configured, Try setting the access credentials first %s: ",str(e))
#-----------------------------------------------------------------------------------------------------------------------------
def connection_instance(self):
try:
#EC2 Connection
self.ec2_conn = boto.ec2.connect_to_region(self.AWS_REGION,
aws_access_key_id=self.AWS_KEY,
- aws_secret_access_key=self.AWS_SECRET)
-
+ aws_secret_access_key=self.AWS_SECRET)
""" TODO : Required to add actions against alarms when needed """
#self.sns = connect_to_region(self.AWS_REGION)
class plugin_alarms():
"""Receives Alarm info from MetricAlarm and connects with the consumer/producer"""
def __init__ (self):
- self.conn = Connection()
self.metricAlarm = MetricAlarm()
self.metric = Metrics()
- self.connection()
self.producer = KafkaProducer('')
-#---------------------------------------------------------------------------------------------------------------------------
- def connection(self):
- """Connecting instances with CloudWatch"""
- self.conn.setEnvironment()
- self.conn = self.conn.connection_instance()
- self.cloudwatch_conn = self.conn['cloudwatch_connection']
- self.ec2_conn = self.conn['ec2_connection']
#---------------------------------------------------------------------------------------------------------------------------
def configure_alarm(self,alarm_info):
alarm_id = self.metricAlarm.config_alarm(self.cloudwatch_conn,alarm_info)
return self.metric.metricsData(self.cloudwatch_conn,metric_name,period,instance_id)
#---------------------------------------------------------------------------------------------------------------------------
- def alarm_calls(self,message):
+ def alarm_calls(self,message,aws_conn):
"""Gets the message from the common consumer"""
- try:
+ try:
+ self.cloudwatch_conn = aws_conn['cloudwatch_connection']
+ self.ec2_conn = aws_conn['ec2_connection']
+
log.info("Action required against: %s" % (message.topic))
alarm_info = json.loads(message.value)
class plugin_metrics():
"""Receives Alarm info from MetricAlarm and connects with the consumer/producer """
def __init__ (self):
- self.conn = Connection()
self.metric = Metrics()
self.producer = KafkaProducer('')
- self.connection()
-#---------------------------------------------------------------------------------------------------------------------------
- def connection(self):
- try:
- """Connecting instances with CloudWatch"""
- self.conn.setEnvironment()
- self.conn = self.conn.connection_instance()
- self.cloudwatch_conn = self.conn['cloudwatch_connection']
- self.ec2_conn = self.conn['ec2_connection']
-
- except Exception as e:
- log.error("Failed to Connect with AWS %s: " + str(e))
#---------------------------------------------------------------------------------------------------------------------------
def create_metric_request(self,metric_info):
'''Comaptible API using normalized parameters'''
return data_resp
#---------------------------------------------------------------------------------------------------------------------------
- def metric_calls(self,message):
+ def metric_calls(self,message,aws_conn):
'''Consumer will consume the message from SO,
1) parse the message and trigger the methods ac
cording to keys and topics provided in request.
'''
try:
+ self.cloudwatch_conn = aws_conn['cloudwatch_connection']
+ self.ec2_conn = aws_conn['ec2_connection']
+
metric_info = json.loads(message.value)
metric_response = dict()