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

inventory_fujitsu_ca_ports = []


def parse_fjdarye500_ca_ports(info):
    map_modes = {
        "11": "CA",
        "12": "RA",
        "13": "CARA",
        "20": "Initiator",
    }

    parsed = {}
    for index, mode, read_iops, write_iops, read_mb, write_mb in info:
        mode_readable = map_modes[mode]
        port = parsed.setdefault(
            index,
            {
                "mode": mode_readable,
                "read_ios": int(read_iops),
                "read_throughput": int(read_mb) * 1024**2,
            },
        )
        if mode_readable != "Initiator":
            port.update(
                {
                    "write_ios": int(write_iops),
                    "write_throughput": int(write_mb) * 1024**2,
                }
            )
    return parsed


def inventory_fjdarye500_ca_ports(parsed):
    settings = host_extra_conf_merged(host_name(), inventory_fujitsu_ca_ports)
    indices = settings.get("indices")
    modes = settings.get("modes", ["CA", "CARA"])
    for index, attrs in parsed.items():
        if indices and index not in indices:
            continue
        if modes and attrs["mode"] not in modes:
            continue
        yield index, {}


def check_fjdarye500_ca_ports(item, params, parsed):
    if item in parsed:
        mode = parsed[item]["mode"]
        yield 0, "Mode: %s" % mode
        for state, infotext, perfdata in check_diskstat_dict(item, params, parsed):
            if infotext and "Read: " in infotext and mode in ["CARA", "Initiator"]:
                infotext.replace("Read: ", "Initiator: ")
            if infotext and "Write: " in infotext and mode == "CARA":
                infotext.replace("Write: ", "Target: ")
            yield state, infotext, perfdata


check_info["fjdarye500_ca_ports"] = {
    "parse_function": parse_fjdarye500_ca_ports,
    "inventory_function": inventory_fjdarye500_ca_ports,
    "check_function": check_fjdarye500_ca_ports,
    "service_description": "CA Port IO %s",
    "snmp_info": (
        ".1.3.6.1.4.1.211.1.21.1.150.5.5.2.1",
        [
            # fjdaryPfCaPortRdIOPS
            #     "This shows the READ IOPS for the CA,CARA mode.
            #      The Initiator IOPS is shown for RA,Initiator mode."
            # fjdaryPfCaPortWtIOPS
            #     "This shows the WRITE IOPS for the CA,CARA mode.
            #      The Target IOPS is shown for the RA mode.
            #      This information is an invalid value for the Initiator mode."
            # fjdaryPfCaPortRdTp
            #     "This shows the amount of the READ Throughput for the CA,CARA mode.
            #      The Initiator Throughput is shown for RA,Initiator mode.
            #      The unit is MB/sec."
            # fjdaryPfCaPortWtTp
            #     "This shows the amount of the WRITE Throughput for the CA,CARA mode.
            #      The Target Throughput is shown for the RA mode.
            #      The unit is MB/sec.
            #      This information is an invalid value for the Initiator mode."
            "1",  # FJDARY-E150::fjdaryPfCaPortIndex
            "2",  # FJDARY-E150::fjdaryPfCaPortMode
            "3",  # FJDARY-E150::fjdaryPfCaPortRdIOPS
            "4",  # FJDARY-E150::fjdaryPfCaPortWtIOPS
            "5",  # FJDARY-E150::fjdaryPfCaPortRdTp
            "6",  # FJDARY-E150::fjdaryPfCaPortWtTp
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.211.1.21.1.150",
    "has_perfdata": True,
    "group": "diskstat",
}
