@@ -0,0 +1,2187 @@
+%22%22%22%0AChecks the last hour of AWS Billing Data from S3 buckets, and sends an alert if%0Aany servers have exceeded the specified threshold.%0A%0ANote: This Action assumes that you have one or more configured Alert Channels%0A    that can be used for notifying the appropraite users. These channels must%0A    be listed in ALERT_CHANNEL_NAMES below.%0A%0AVersion:%0A    Requires %3E= v9.0.1%0A%22%22%22%0A%0Aimport json%0Afrom typing import List%0A%0Afrom alerts.methods import alert%0Afrom resourcehandlers.aws.models import AWSHandler%0A%0AAWS_COST_THRESHOLD = 100%0AALERT_CHANNEL_NAMES: List%5Bstr%5D = %5B%5D%0A%0A%0Aclass AWSCostAlert:%0A    def __init__(self):%0A        self.over_threshold = %5B%5D%0A%0A    def __call__(self):%0A        %22%22%22%0A        Retrieve cost data for all AWS Resource Handlers and alert if any%0A        servers exceed the specified threshold.%0A        %22%22%22%0A        aws_instances = AWSHandler.objects.all()%0A        for aws in aws_instances:%0A            last_hours_data = aws.get_last_hour_billing_data()%0A            self.__check_threshold(last_hours_data)%0A%0A        _ = self.__alert_over_threshold()%0A%0A    def __check_threshold(self, data_list):%0A        %22%22%22%0A        Parse server information for instances that exceed the threshold.%0A%0A        Args:%0A            data_list (List%5BTuple%5Bstr, float%5D%5D): %5B(server_id, cost)%5D%0A        %22%22%22%0A        _exceed_threshold_list = %5B%5D%0A        for server_id, cost in data_list:%0A            if cost %3E= AWS_COST_THRESHOLD:%0A                _exceed_threshold_list.append((server_id, cost))%0A        self.over_threshold.extend(_exceed_threshold_list)%0A        return%0A%0A    def __alert_over_threshold(self):%0A        %22%22%22%0A        Send alert for all server instances that exceed the threshold.%0A%0A        Returns:%0A            None%0A        %22%22%22%0A        instance_dict = dict(self.over_threshold)%0A        instance_json = json.dumps(instance_dict, indent=4)%0A        message = (%0A            f%22The following servers exceeded the AWS cost threshold of %7BAWS_COST_THRESHOLD%7D:%22%0A            f%22%7Binstance_json%7D%22%0A        )%0A        for channel_name in ALERT_CHANNEL_NAMES:%0A            alert(channel_name, message)%0A%0A        return None%0A%0A%0Adef run(*args, **kwargs):%0A    cost_alert = AWSCostAlert()%0A    cost_alert()%0A%0A    return %22SUCCESS%22, %22%22, %22%22%0A

Add AWS Hourly Spending Anomaly Detection