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


def parse_hp_proliant_raid(info):
    parsed = {}
    for number, name, status, size_str, rebuild in info:
        if name in parsed:
            itemname = "%s %s" % (name, number)
        else:
            itemname = name

        parsed.setdefault(itemname.strip(), {
            "status": status,
            "size": int(size_str) * 1024 * 1024,
            "rebuild_perc": 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 item in parsed:
        data = parsed[item]
        dev_status = data["status"]
        state, state_readable = map_states[dev_status]
        infotext   = "Status: %s, Logical volume size: %s" % \
                     (state_readable, get_bytes_human_readable(data["size"]))

        # 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 dev_status in ["7", "12"]:
            infotext += "Rebuild: %s%%" % data["rebuild_perc"]

        return state, infotext


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,
}
