#!/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

# 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.diskstat import *  # pylint: disable=wildcard-import,unused-wildcard-import
from cmk.base.plugins.agent_based.utils.aws import extract_aws_metrics_by_labels, parse_aws


def parse_aws_ebs(info):
    return extract_aws_metrics_by_labels(
        [
            "VolumeReadOps",
            "VolumeWriteOps",
            "VolumeReadBytes",
            "VolumeWriteBytes",
            "VolumeQueueLength",
            "BurstBalance",
            # "VolumeThroughputPercentage",
            # "VolumeConsumedReadWriteOps",
            # "VolumeTotalReadTime",
            # "VolumeTotalWriteTime",
            # "VolumeIdleTime",
        ],
        parse_aws(info),
    )


#   .--Disk IO-------------------------------------------------------------.
#   |                     ____  _     _      ___ ___                       |
#   |                    |  _ \(_)___| | __ |_ _/ _ \                      |
#   |                    | | | | / __| |/ /  | | | | |                     |
#   |                    | |_| | \__ \   <   | | |_| |                     |
#   |                    |____/|_|___/_|\_\ |___\___/                      |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def check_aws_ebs(item, params, parsed):
    now = time.time()
    disks = {}
    for disk_name, metrics in parsed.items():
        disks.setdefault(
            disk_name,
            {
                "read_ios": get_rate(
                    "aws_ebs_disk_io_read_ios.%s" % item, now, metrics["VolumeReadOps"]
                ),
                "write_ios": get_rate(
                    "aws_ebs_disk_io_write_ios.%s" % item, now, metrics["VolumeWriteOps"]
                ),
                "read_throughput": get_rate(
                    "aws_ebs_disk_io_read_throughput.%s" % item, now, metrics["VolumeReadBytes"]
                ),
                "write_throughput": get_rate(
                    "aws_ebs_disk_io_write_throughput.%s" % item, now, metrics["VolumeWriteBytes"]
                ),
                "queue_length": get_rate(
                    "aws_ebs_disk_io_queue_len.%s" % item, now, metrics["VolumeQueueLength"]
                ),
            },
        )
    return check_diskstat_dict(item, params, disks)


check_info["aws_ebs"] = {
    "parse_function": parse_aws_ebs,
    "inventory_function": lambda p: inventory_aws_generic(
        p,
        [
            "VolumeReadOps",
            "VolumeWriteOps",
            "VolumeReadBytes",
            "VolumeWriteBytes",
            "VolumeQueueLength",
        ],
    ),
    "check_function": check_aws_ebs,
    "service_description": "AWS/EBS Disk IO %s",
    "group": "diskstat",
    "has_perfdata": True,
}

# .
#   .--burst balance-------------------------------------------------------.
#   |    _                    _     _           _                          |
#   |   | |__  _   _ _ __ ___| |_  | |__   __ _| | __ _ _ __   ___ ___     |
#   |   | '_ \| | | | '__/ __| __| | '_ \ / _` | |/ _` | '_ \ / __/ _ \    |
#   |   | |_) | |_| | |  \__ \ |_  | |_) | (_| | | (_| | | | | (_|  __/    |
#   |   |_.__/ \__,_|_|  |___/\__| |_.__/ \__,_|_|\__,_|_| |_|\___\___|    |
#   |                                                                      |
#   '----------------------------------------------------------------------'


@get_parsed_item_data
def check_aws_ebs_burst_balance(item, params, metrics):
    warn, crit = params.get("burst_balance_levels_lower", (None, None))
    yield check_levels(
        metrics["BurstBalance"],
        "aws_burst_balance",
        (None, None, warn, crit),
        human_readable_func=get_percent_human_readable,
        infoname="Balance",
    )


check_info["aws_ebs.burst_balance"] = {
    "inventory_function": lambda p: inventory_aws_generic(p, ["BurstBalance"]),
    "check_function": check_aws_ebs_burst_balance,
    "service_description": "AWS/EBS Burst Balance %s",
    "group": "aws_ebs_burst_balance",
    "has_perfdata": True,
}
