#!/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_pdrive(info):
    for controller, device, _ldrive, _position, status, _progress, _vendor, _product, _fail in info:
        if status != "12":
            item = "%s-%s" % (controller, device)
            yield item, None


def check_datapower_pdrive(item, _no_params, info):
    datapower_pdrive_status = {
        "1": (0, "Unconfigured/Good"),
        "2": (0, "Unconfigured/Good/Foreign"),
        "3": (1, "Unconfigured/Bad"),
        "4": (1, "Unconfigured/Bad/Foreign"),
        "5": (0, "Hot spare"),
        "6": (1, "Offline"),
        "7": (2, "Failed"),
        "8": (1, "Rebuilding"),
        "9": (0, "Online"),
        "10": (1, "Copyback"),
        "11": (1, "System"),
        "12": (1, "Undefined"),
    }
    datapower_pdrive_fail = {
        "1": (2, "disk reports failure"),
        "2": (0, "disk reports no failure"),
    }
    datapower_pdrive_position = {
        "1": "HDD 0",
        "2": "HDD 1",
        "3": "HDD 2",
        "4": "HDD 3",
        "5": "undefined",
    }
    for controller, device, ldrive, position, status, progress, vendor, product, fail in info:
        if item == "%s-%s" % (controller, device):
            member_of_ldrive = "%s-%s" % (controller, ldrive)
            state, state_txt = datapower_pdrive_status[status]
            position_txt = datapower_pdrive_position[position]
            if int(progress) != 0:
                progress_txt = " - Progress: %s%%" % progress
            else:
                progress_txt = ""
            infotext = "%s%s, Position: %s, Logical Drive: %s, Product: %s %s" % (
                state_txt,
                progress_txt,
                position_txt,
                member_of_ldrive,
                vendor,
                product,
            )
            yield state, infotext

            if fail:
                yield datapower_pdrive_fail[fail]


check_info["datapower_pdrive"] = {
    "inventory_function": inventory_datapower_pdrive,
    "check_function": check_datapower_pdrive,
    "service_description": "Physical Drive %s",
    "snmp_info": (
        ".1.3.6.1.4.1.14685.3.1.260.1",
        [
            "1",  # dpStatusRaidPhysicaldrivetatusControllerID
            "2",  # dpStatusRaidPhysicaldrivetatusDeviceID
            "4",  # dpStatusRaidPhysicaldrivetatusLogicalDriveID
            "6",  # dpStatusRaidPhysicaldrivetatusPosition
            "7",  # dpStatusRaidPhysicaldrivetatusState
            "8",  # dpStatusRaidPhysicaldrivetatusProgressPercent
            "14",  # dpStatusRaidPhysicaldrivetatusVendorID
            "15",  # dpStatusRaidPhysicaldrivetatusProductID
            "18",  # dpStatusRaidPhysicaldrivetatusFailure
        ],
    ),
    "snmp_scan_function": scan_datapower,
}
