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


def inventory_datapower_ldrive(info):
    for controller, ldrive, _raid_level, _num_drives, _status in info:
        item = "%s-%s" % (controller, ldrive)
        yield item, None


def check_datapower_ldrive(item, _no_params, info):
    datapower_ldrive_status = {
        "1": (2, "offline"),
        "2": (2, "partially degraded"),
        "3": (2, "degraded"),
        "4": (0, "optimal"),
        "5": (1, "unknown"),
    }
    datapower_ldrive_raid = {
        "1": "0",
        "2": "1",
        "3": "1E",
        "4": "5",
        "5": "6",
        "6": "10",
        "7": "50",
        "8": "60",
        "9": "undefined",
    }
    for controller, ldrive, raid_level, num_drives, status in info:
        if item == "%s-%s" % (controller, ldrive):
            state, state_txt = datapower_ldrive_status[status]
            raid_level = datapower_ldrive_raid[raid_level]
            infotext = "Status: %s, RAID Level: %s, Number of Drives: %s" % (
                state_txt,
                raid_level,
                num_drives,
            )
            return state, infotext


check_info["datapower_ldrive"] = {
    "inventory_function": inventory_datapower_ldrive,
    "check_function": check_datapower_ldrive,
    "service_description": "Logical Drive %s",
    "snmp_info": (
        ".1.3.6.1.4.1.14685.3.1.259.1",
        [
            "1",  # dpStatusRaidLogicaldrivetatusControllerID
            "2",  # dpStatusRaidLogicaldrivetatusLogicalDriveID
            "4",  # dpStatusRaidLogicaldrivetatusRaidLevel
            "5",  # dpStatusRaidLogicaldrivetatusNumPhysicalDrives
            "6",  # dpStatusRaidLogicaldrivetatusState
        ],
    ),
    "snmp_scan_function": scan_datapower,
}
