#!/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.diskstat import *  # pylint: disable=wildcard-import,unused-wildcard-import


def _fixup_section(section):
    # This plugin used to call get_rate in the parse function, which does not work in 2.1.
    # Putting it here is a quick fix, the solution in 2.2 is a completely different.
    now = time.time()
    fixed = {}
    for name, value in section.items():
        fixed_value = {
            **value,
            "read_throughput": get_rate("readbytes.%s" % name, now, value["read_throughput"]),
            "write_throughput": get_rate("writebytes.%s" % name, now, value["write_throughput"]),
            "ios": get_rate("ios.%s" % name, now, value["ios"]),
        }
        fixed[name] = fixed_value
    return fixed


def parse_emc_vplex_volumes(info):
    volumes = {}

    # Each volume is listed twice, because they are connected to both directors
    for line in info:
        name = line[0]
        read_wait = float(line[5]) / 1000000
        write_wait = float(line[6]) / 1000000

        if name in volumes:
            volumes[name]["read_throughput"] += float(line[3])
            volumes[name]["write_throughput"] += float(line[4])
            volumes[name]["ios"] += float(line[2])
            volumes[name]["average_read_wait"] = max(volumes[name]["average_read_wait"], read_wait)
            volumes[name]["average_write_wait"] = max(
                volumes[name]["average_write_wait"], write_wait
            )
        else:
            volumes[name] = {
                "average_read_wait": read_wait,
                "average_write_wait": write_wait,
                "read_throughput": float(line[3]),
                "write_throughput": float(line[4]),
                "ios": float(line[2]),
            }

    return volumes


def inventory_emc_vplex_volumes(parsed):
    return inventory_diskstat_generic([(None, x) for x in parsed])


def check_emc_vplex_volumes(item, params, parsed):
    return check_diskstat_dict(item, params, _fixup_section(parsed))


check_info["emc_vplex_volumes"] = {
    "parse_function": parse_emc_vplex_volumes,
    "check_function": check_emc_vplex_volumes,
    "inventory_function": inventory_emc_vplex_volumes,
    "service_description": "Disk IO Volume %s",
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0") == ""
    and oid(".1.3.6.1.4.1.1139.21.2.2.8.1.*"),
    "snmp_info": (
        ".1.3.6.1.4.1.1139.21.2.2.8.1",
        [
            1,  # vplexDirectorVirtualVolumeName
            2,  # vplexDirectorVirtualVolumeUuid
            3,  # vplexDirectorVirtualVolumeOps
            4,  # vplexDirectorVirtualVolumeRead
            5,  # vplexDirectorVirtualVolumeWrite
            6,  # vplexDirectorVirtualVolumeReadAvgLatency
            7,  # vplexDirectorVirtualVolumeWriteAvgLatency
        ],
    ),
    "has_perfdata": True,
    "group": "diskstat",
}
