Ejemplo n.º 1
0
 def connect(self):
     """Connect to API if not already connected; set self.conn."""
     if self.conn is not None:
         return
     if self.ta_region:
         logger.debug("Connecting to Support API (TrustedAdvisor) in %s" % self.region)
         self.conn = self.connect_via(boto.support.connect_to_region)
     else:
         logger.debug("Connecting to Support API (TrustedAdvisor)")
         self.conn = boto.connect_support()
     logger.debug("Connected to Support API")
Ejemplo n.º 2
0
 def connect(self):
     """Connect to API if not already connected; set self.conn."""
     if self.conn is not None:
         return
     if self.ta_region:
         logger.debug("Connecting to Support API (TrustedAdvisor) in %s",
                      self.region)
         self.conn = self.connect_via(boto.support.connect_to_region)
     else:
         logger.debug("Connecting to Support API (TrustedAdvisor)")
         self.conn = boto.connect_support()
     logger.debug("Connected to Support API")
Ejemplo n.º 3
0
def serviceme():
    """Log or Update a support case"""

    response = None
    if request.json is None:
        # Expect application/json request
        response = Response("", status=415)
    else:
        message = dict()
        try:
            # If the message has an SNS envelope, extract the inner message
            if request.json.has_key('TopicArn') and request.json.has_key(
                    'Message'):
                message = json.loads(request.json['Message'])
            else:
                message = request.json

            # Connect to CloudWatch API in default region
            cloudwatch = boto.connect_cloudwatch()

            # Conenct to the support API in default region
            support = boto.connect_support()

            # Connect to dynamoDB in specified region
            try:
                print os.environ['DBREGION']
            except KeyError:
                # Connect to default region
                ddb = boto.dynamodb.connect_to_region('us-east-1')
            else:
                # Connect to specified region
                ddb = boto.dynamodb.connect_to_region(os.environ['DBREGION'])

            # Set the DynamoDB table name
            try:
                print os.environ['DBTABLE']
            except KeyError:
                # Connect to default region
                tablename = 'cw-support-api'
            else:
                # Connect to specified region
                tablename = os.environ['DBTABLE']

            # Open the DynamoDB table
            try:
                configtable = ddb.get_table(tablename)
            except boto.dynamodb.exceptions.DynamoDBResponseError:
                print 'cannot open the table %s' % tablename

            try:
                configitem = configtable.get_item(
                    hash_key=message['AlarmName'])
            except boto.dynamodb.exceptions.DynamoDBKeyNotFoundError:
                # Set defaults
                priority = 'low'
                action = 'Please contact us for more information'
            else:
                # Get alarm specific configuration
                priority = configitem['priority']
                action = json.loads(configitem['action'])
                try:
                    print configitem['ccemail']
                except KeyError:
                    ccemail = None
                else:
                    ccemail = [configitem['ccemail']]

            # Get a list of open cases
            cases = support.describe_cases()

            # Check if one of the open matches the CloudWatch Alert Name
            for x in range(len(cases['cases'])):
                if (cases['cases'][x]['subject'] == (message['AlarmName'])):
                    caseid = (cases['cases'][x]['caseId'])

            # OK is there an existing case
            try:
                print caseid
            except NameError:
                # No existing case exists, lets open a new case
                newcase = support.create_case(
                    subject=(message['AlarmName']),
                    service_code='amazon-elastic-compute-cloud-linux',
                    category_code='other',
                    cc_email_addresses=ccemail,
                    communication_body=BODY %
                    (message['AlarmName'], message['AlarmDescription'],
                     message['StateChangeTime'],
                     json.dumps(action, indent=4, sort_keys=True),
                     json.dumps(message, indent=4, sort_keys=True)),
                    severity_code=priority)
                response = Response("", status=200)
                cloudwatch.put_metric_data('Support',
                                           'Case/Logged',
                                           1,
                                           unit='Count')
            else:
                # There is an existing case, lets add some communication
                result = support.add_communication_to_case(
                    communication_body=BODY2 %
                    (message['AlarmName'], message['AlarmDescription'],
                     message['StateChangeTime'],
                     json.dumps(action, indent=4, sort_keys=True),
                     json.dumps(message, indent=4, sort_keys=True)),
                    case_id=caseid)
                response = Response("", status=200)
                cloudwatch.put_metric_data('Support',
                                           'Case/Updated',
                                           1,
                                           unit='Count')
        except Exception as ex:
            logging.exception('Error processing message: %s' % request.json)
            response = Response(ex.message, status=500)

    return response
Ejemplo n.º 4
0
def serviceme():
        """Log or Update a support case"""

        response = None
        if request.json is None:
                # Expect application/json request
                response = Response("", status=415)
        else:
                message = dict()
                try:
                # If the message has an SNS envelope, extract the inner message
                        if request.json.has_key('TopicArn') and request.json.has_key('Message'):
                                message = json.loads(request.json['Message'])
                        else:
                                message = request.json

                        # Connect to CloudWatch API in default region
                        cloudwatch = boto.connect_cloudwatch()

                        # Conenct to the support API in default region
                        support = boto.connect_support()

                        # Connect to dynamoDB in specified region
                        try:
                            print os.environ['DBREGION']
                        except KeyError:
                            # Connect to default region
                            ddb = boto.dynamodb.connect_to_region('us-east-1')
                        else:
                            # Connect to specified region
                            ddb = boto.dynamodb.connect_to_region(os.environ['DBREGION'])

                        # Set the DynamoDB table name
                        try:
                            print os.environ['DBTABLE']
                        except KeyError:
                            # Connect to default region
                            tablename = 'cw-support-api'
                        else:
                            # Connect to specified region
                            tablename = os.environ['DBTABLE']

                        # Open the DynamoDB table
                        try:
                            configtable = ddb.get_table(tablename)
                        except boto.dynamodb.exceptions.DynamoDBResponseError:
                            print 'cannot open the table %s' % tablename                        

                        try:
                            configitem = configtable.get_item(hash_key=message['AlarmName'])
                        except boto.dynamodb.exceptions.DynamoDBKeyNotFoundError:
                            # Set defaults
                            priority = 'low'
                            action = 'Please contact us for more information'
                        else:
                            # Get alarm specific configuration
                            priority = configitem['priority']
                            action = json.loads(configitem['action'])
                            try:
                                print configitem['ccemail']
                            except KeyError:
                                ccemail = None
                            else:
                                ccemail = [configitem['ccemail']]

                        # Get a list of open cases
                        cases = support.describe_cases()

                        # Check if one of the open matches the CloudWatch Alert Name
                        for x in range(len(cases['cases'])):
                                if (cases['cases'][x]['subject'] == (message['AlarmName'])):
                                        caseid = (cases['cases'][x]['caseId'])

                        # OK is there an existing case
                        try:
                                print caseid
                        except NameError:
                                # No existing case exists, lets open a new case
                                newcase = support.create_case(subject = (message['AlarmName']),
                                                              service_code='amazon-elastic-compute-cloud-linux',
                                                              category_code='other',
                                                              cc_email_addresses=ccemail,
                                                              communication_body=BODY % (message['AlarmName'],
                                                                                         message['AlarmDescription'],
                                                                                         message['StateChangeTime'],
                                                                                         json.dumps(action, indent=4, sort_keys=True),
                                                                                         json.dumps(message, indent=4, sort_keys=True)),
                                                                severity_code=priority)
                                response = Response("", status=200)
                                cloudwatch.put_metric_data('Support','Case/Logged',1,unit='Count')
                        else:
                                # There is an existing case, lets add some communication
                                result = support.add_communication_to_case(communication_body=BODY2 % (message['AlarmName'],
                                                                                                       message['AlarmDescription'],
                                                                                                       message['StateChangeTime'],
                                                                                                       json.dumps(action, indent=4, sort_keys=True),
                                                                                                       json.dumps(message, indent=4, sort_keys=True)),
                                                                           	case_id=caseid)
                                response = Response("", status=200)
                                cloudwatch.put_metric_data('Support','Case/Updated',1,unit='Count')
                except Exception as ex:
                        logging.exception('Error processing message: %s' % request.json)
                        response = Response(ex.message, status=500)

        return response
Ejemplo n.º 5
0
 def connect(self):
     if self.conn is None:
         logger.debug("Connecting to Support API (TrustedAdvisor)")
         self.conn = boto.connect_support()
         logger.debug("Connected to Support API")