#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.aws import *  # pylint: disable=wildcard-import,unused-wildcard-import

factory_settings["aws_rds_limits_default_levels"] = {
    "db_instances": (None, 80.0, 90.0),
    "reserved_db_instances": (None, 80.0, 90.0),
    "allocated_storage": (None, 80.0, 90.0),
    "db_security_groups": (None, 80.0, 90.0),
    "auths_per_db_security_groups": (None, 80.0, 90.0),
    "db_parameter_groups": (None, 80.0, 90.0),
    "manual_snapshots": (None, 80.0, 90.0),
    "event_subscriptions": (None, 80.0, 90.0),
    "db_subnet_groups": (None, 80.0, 90.0),
    "option_groups": (None, 80.0, 90.0),
    "subnet_per_db_subnet_groups": (None, 80.0, 90.0),
    "read_replica_per_master": (None, 80.0, 90.0),
    "db_clusters": (None, 80.0, 90.0),
    "db_cluster_parameter_groups": (None, 80.0, 90.0),
    "db_cluster_roles": (None, 80.0, 90.0),
}


def parse_aws_rds_limits(info):
    limits_by_region: AWSLimitsByRegion = {}
    for line in parse_aws(info):
        resource_key, resource_title, limit, amount, region = line

        if resource_key == "allocated_storage":
            # Allocated Storage has unit TiB
            factor = 1024**4 / 1000.0
            limit = limit * factor
            amount = amount * factor
            human_readable_f = get_bytes_human_readable
        else:
            human_readable_f = int
        limits_by_region.setdefault(region, []).append(
            [resource_key, resource_title, limit, amount, human_readable_f]
        )
    return limits_by_region


@get_parsed_item_data
def check_aws_rds_limits(item, params, region_data):
    return check_aws_limits("rds", params, region_data)


check_info["aws_rds_limits"] = {
    "parse_function": parse_aws_rds_limits,
    "inventory_function": discover(),
    "check_function": check_aws_rds_limits,
    "service_description": "AWS/RDS Limits %s",
    "group": "aws_rds_limits",
    "default_levels_variable": "aws_rds_limits_default_levels",
    "has_perfdata": True,
}
