#!/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.
import typing

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


class HpProRaid(typing.NamedTuple):
    name: str
    status: str
    size_bytes: int
    rebuild_percent: int


def parse_hp_proliant_raid(info):
    parsed = {}
    for number, name, status, size_str, rebuild in info:
        itemname = f"{name} {number}".strip()
        parsed.setdefault(
            itemname,
            HpProRaid(
                name=itemname,
                status=status,
                size_bytes=int(size_str) * 1024 * 1024,
                rebuild_percent=int(rebuild),
            ),
        )

    return parsed


def inventory_hp_proliant_raid(parsed):
    for raid in parsed:
        yield raid, None


def check_hp_proliant_raid(item, _no_params, parsed):
    map_states = {
        "1": (3, "other"),
        "2": (0, "OK"),
        "3": (2, "failed"),
        "4": (1, "unconfigured"),
        "5": (1, "recovering"),
        "6": (1, "ready for rebuild"),
        "7": (1, "rebuilding"),
        "8": (2, "wrong drive"),
        "9": (2, "bad connect"),
        "10": (2, "overheating"),
        "11": (1, "shutdown"),
        "12": (1, "automatic data expansion"),
        "13": (2, "not available"),
        "14": (1, "queued for expansion"),
        "15": (1, "multi-path access degraded"),
        "16": (1, "erasing"),
    }

    if not (raid_stats := parsed.get(item)):
        return

    state, state_readable = map_states.get(raid_stats.status, (3, "unknown"))
    yield state, f"Status: {state_readable}"
    yield 0, f"Logical volume size: {get_bytes_human_readable(raid_stats.size_bytes)}"

    # From CPQIDA-MIB:
    # This value is the percent complete of the rebuild.
    # This value is only valid if the Logical Drive Status is
    # rebuilding (7) or expanding (12).
    # If the value cannot be determined or a rebuild is not active,
    # the value is set to 4,294,967,295.
    if raid_stats.status not in {"7", "12"}:
        return

    if raid_stats.rebuild_percent == 4294967295:
        yield 0, "Rebuild: undetermined"
        return

    yield 0, f"Rebuild: {get_percent_human_readable(raid_stats.rebuild_percent)}"


check_info["hp_proliant_raid"] = {
    "parse_function": parse_hp_proliant_raid,
    "check_function": check_hp_proliant_raid,
    "inventory_function": inventory_hp_proliant_raid,
    "service_description": "Logical Device %s",
    "snmp_info": (
        ".1.3.6.1.4.1.232.3.2.3.1.1",
        [
            "2",  # CPQIDA-MIB::cpqDaLogDrvIndex
            "14",  # CPQIDA-MIB::cpqDaLogDrvOsName
            "4",  # CPQIDA-MIB::cpqDaLogDrvStatus
            "9",  # CPQIDA-MIB::cpqDaLogDrvSize
            "12",  # CPQIDA-MIB::cpqDaLogDrvPercentRebuild
        ],
    ),
    "snmp_scan_function": hp_proliant_scan_function,
}
