示例#1
0
    def test_deliver_datadog_messages_should_not_send_metric_if_metrics_are_empty(
            self, mock_datadog_api, mock_time):
        datadog_delivery = DataDogDelivery(self.config, self.session,
                                           self.logger)
        datadog_delivery.deliver_datadog_messages([], SQS_MESSAGE_3)

        mock_datadog_api.assert_not_called()
    def _deliver_datadog_message(self, queue_message):
        from c7n_mailer.datadog_delivery import DataDogDelivery
        datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
        datadog_message_packages = datadog_delivery.get_datadog_message_packages(queue_message)

        try:
            self.logger.info('Sending message to Datadog.')
            datadog_delivery.deliver_datadog_messages(datadog_message_packages, queue_message)
        except Exception as error:
            self.logger.exception(error)
    def test_deliver_datadog_messages_should_send_correct_metric_value_to_datadog(
            self, mock_datadog_api, mock_time):
        datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
        datadog_message_packages = datadog_delivery.get_datadog_message_packages(SQS_MESSAGE_3)
        datadog_delivery.deliver_datadog_messages(datadog_message_packages, SQS_MESSAGE_3)

        answer = mock_datadog_api.mock_calls[0][1][0]
        answer[0]['tags'].sort()

        assert answer == DATADOG_METRIC_SQS_MESSAGE_3
示例#4
0
    def process_azure_queue_message(self, encoded_azure_queue_message):
        queue_message = json.loads(
            zlib.decompress(base64.b64decode(encoded_azure_queue_message.content)))

        self.logger.debug("Got account:%s message:%s %s:%d policy:%s recipients:%s" % (
            queue_message.get('account', 'na'),
            encoded_azure_queue_message.id,
            queue_message['policy']['resource'],
            len(queue_message['resources']),
            queue_message['policy']['name'],
            ', '.join(queue_message['action'].get('to'))))

        if any(e.startswith('slack') or e.startswith('https://hooks.slack.com/')
                for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.slack_delivery import SlackDelivery
            slack_delivery = SlackDelivery(self.config,
                                           self.logger,
                                           SendGridDelivery(self.config, self.logger))
            slack_messages = slack_delivery.get_to_addrs_slack_messages_map(queue_message)
            try:
                slack_delivery.slack_handler(queue_message, slack_messages)
            except Exception:
                traceback.print_exc()
                pass

        # this section gets the map of metrics to send to datadog and delivers it
        if any(e.startswith('datadog') for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.datadog_delivery import DataDogDelivery
            datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
            datadog_message_packages = datadog_delivery.get_datadog_message_packages(queue_message)

            try:
                datadog_delivery.deliver_datadog_messages(datadog_message_packages, queue_message)
            except Exception:
                traceback.print_exc()
                pass

        # this section sends a notification to the resource owner via SendGrid
        try:
            sendgrid_delivery = SendGridDelivery(self.config, self.logger)
            email_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map(queue_message)

            if 'smtp_server' in self.config:
                smtp_delivery = SmtpDelivery(config=self.config,
                                             session=self.session,
                                             logger=self.logger)
                for to_addrs, message in six.iteritems(email_messages):
                    smtp_delivery.send_message(message=message, to_addrs=list(to_addrs))
            else:
                return sendgrid_delivery.sendgrid_handler(queue_message, email_messages)
        except Exception:
            traceback.print_exc()

        return True
示例#5
0
    def test_deliver_datadog_messages_should_send_correct_metric_value_to_datadog(
            self, mock_datadog_api, mock_time):
        datadog_delivery = DataDogDelivery(self.config, self.session,
                                           self.logger)
        datadog_message_packages = datadog_delivery.get_datadog_message_packages(
            SQS_MESSAGE_3)
        datadog_delivery.deliver_datadog_messages(datadog_message_packages,
                                                  SQS_MESSAGE_3)

        answer = mock_datadog_api.mock_calls[0][1][0]
        answer[0]['tags'].sort()

        assert answer == DATADOG_METRIC_SQS_MESSAGE_3
    def process_azure_queue_message(self, encoded_azure_queue_message):
        queue_message = json.loads(
            zlib.decompress(base64.b64decode(encoded_azure_queue_message.content)))

        self.logger.debug("Got account:%s message:%s %s:%d policy:%s recipients:%s" % (
            queue_message.get('account', 'na'),
            encoded_azure_queue_message.id,
            queue_message['policy']['resource'],
            len(queue_message['resources']),
            queue_message['policy']['name'],
            ', '.join(queue_message['action'].get('to'))))

        if any(e.startswith('slack') or e.startswith('https://hooks.slack.com/')
                for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.slack_delivery import SlackDelivery
            slack_delivery = SlackDelivery(self.config,
                                           self.logger,
                                           SendGridDelivery(self.config, self.logger))
            slack_messages = slack_delivery.get_to_addrs_slack_messages_map(queue_message)
            try:
                slack_delivery.slack_handler(queue_message, slack_messages)
            except Exception:
                traceback.print_exc()
                pass

        # this section gets the map of metrics to send to datadog and delivers it
        if any(e.startswith('datadog') for e in queue_message.get('action', ()).get('to')):
            from c7n_mailer.datadog_delivery import DataDogDelivery
            datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
            datadog_message_packages = datadog_delivery.get_datadog_message_packages(queue_message)

            try:
                datadog_delivery.deliver_datadog_messages(datadog_message_packages, queue_message)
            except Exception:
                traceback.print_exc()
                pass

        # this section sends a notification to the resource owner via SendGrid
        try:
            sendgrid_delivery = SendGridDelivery(self.config, self.logger)
            sendgrid_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map(queue_message)
            return sendgrid_delivery.sendgrid_handler(queue_message, sendgrid_messages)
        except Exception:
            traceback.print_exc()
    def test_deliver_datadog_messages_should_not_send_metric_if_metrics_are_empty(
            self, mock_datadog_api, mock_time):
        datadog_delivery = DataDogDelivery(self.config, self.session, self.logger)
        datadog_delivery.deliver_datadog_messages([], SQS_MESSAGE_3)

        mock_datadog_api.assert_not_called()