#!/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"]),
            "read_ios": get_rate("read_ios.%s" % name, now, value["read_ios"]),
            "write_ios": get_rate("write_ios.%s" % name, now, value["write_ios"]),
        }
        fixed[name] = fixed_value
    return fixed


def parse_emc_director_stats(info):
    directors = {}
    now = time.time()
    for line in info:
        directors["%s_FE" % line[0]] = {
            "node": None,
            "read_ios": float(line[1]),
            "write_ios": float(line[2]),
            "queue_length": int(line[3]),
            "average_read_wait": float(line[5]) / 1000000,
            "average_write_wait": float(line[6]) / 1000000,
            "read_throughput": float(line[7]),
            "write_throughput": float(line[8]),
        }
        directors["%s_BE" % line[0]] = {
            "node": None,
            "read_ios": float(line[9]),
            "write_ios": float(line[10]),
            "average_read_wait": float(line[11]) / 1000000,
            "average_write_wait": float(line[12]) / 1000000,
            "read_throughput": float(line[13]),
            "write_throughput": float(line[14]),
        }

    return directors


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


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


factory_settings["emc_vplex_director_stats_default_levels"] = diskstat_default_levels


check_info["emc_vplex_director_stats"] = {
    "parse_function": parse_emc_director_stats,
    "check_function": check_emc_director_stats,
    "inventory_function": inventory_emc_director_stats,
    "service_description": "Disk IO Director %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",
        [
            "1.1.3",  # vplexDirectorName
            "4.1.1",  # vplexDirectorFEOpsRead
            "4.1.2",  # vplexDirectorFEOpsWrite
            "4.1.3",  # vplexDirectorFEOpsQueued
            "4.1.4",  # vplexDirectorFEOpsActive
            "4.1.5",  # vplexDirectorFEOpsAvgReadLatency
            "4.1.6",  # vplexDirectorFEOpsAvgWriteLatency
            "4.1.7",  # vplexDirectorFEBytesRead
            "4.1.8",  # vplexDirectorFEBytesWrite
            "6.1.1",  # vplexDirectorBEOpsRead
            "6.1.2",  # vplexDirectorBEOpsWrite
            "6.1.5",  # vplexDirectorBEOpsAvgReadLatency
            "6.1.6",  # vplexDirectorBEOpsAvgWriteLatency
            "6.1.7",  # vplexDirectorBEBytesRead
            "6.1.8",  # vplexDirectorBEBytesWrite
            OID_END,
        ],
    ),
    "has_perfdata": True,
    "group": "diskstat",
    "default_levels_variable": "emc_vplex_director_stats_default_levels",
}
