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

# example output


def inventory_dell_idrac_power(info):
    for index, _status, _count in info[0]:
        yield index, None


def check_dell_idrac_power(item, _no_params, info):
    translate_status = {
        "1": (3, "other"),
        "2": (3, "unknown"),
        "3": (0, "full"),
        "4": (1, "degraded"),
        "5": (2, "lost"),
        "6": (0, "not redundant"),
        "7": (1, "redundancy offline"),
    }

    for index, status, _count in info[0]:
        if index == item:
            state, state_readable = translate_status[status]
            yield state, "Status: %s" % state_readable


check_info["dell_idrac_power"] = {
    "inventory_function": inventory_dell_idrac_power,
    "check_function": check_dell_idrac_power,
    "service_description": "Power Supply Redundancy %s",
    "snmp_info": [
        (
            ".1.3.6.1.4.1.674.10892.5.4.600.10.1",
            [
                "2",  # IDRAC-MIB::powerUnitIndex
                "5",  # IDRAC-MIB::powerUnitRedundancyStatus
                "6",  # IDRAC-MIB::powerSupplyCountForRedundancy
            ],
        ),
        (
            ".1.3.6.1.4.1.674.10892.5.4.600.12.1",
            [
                "2",  # IDRAC-MIB::powerSupplyIndex
                "5",  # IDRAC-MIB::powerSupplyStatus
                "7",  # IDRAC-MIB::powerSupplyType
                "8",  # IDRAC-MIB::powerSupplyLocationName
            ],
        ),
    ],
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(
        ".1.3.6.1.4.1.674.10892.5"
    ),
}


def inventory_dell_idrac_power_unit(info):
    for index, _status, _psu_type, _location in info[1]:
        yield index, None


def check_dell_idrac_power_unit(item, _no_params, info):
    translate_status = {
        "1": (3, "OTHER"),
        "2": (3, "UNKNOWN"),
        "3": (0, "OK"),
        "4": (1, "NONCRITICAL"),
        "5": (2, "CRITICAL"),
        "6": (2, "NONRECOVERABLE"),
    }

    translate_type = {
        "1": "OTHER",
        "2": "UNKNOWN",
        "3": "LINEAR",
        "4": "SWITCHING",
        "5": "BATTERY",
        "6": "UPS",
        "7": "CONVERTER",
        "8": "REGULATOR",
        "9": "AC",
        "10": "DC",
        "11": "VRM",
    }

    for index, status, psu_type, location in info[1]:
        if index == item:
            state, state_readable = translate_status[status]
            psu_type_readable = translate_type[psu_type]
            yield state, "Status: %s, Type: %s, Name: %s" % (
                state_readable,
                psu_type_readable,
                location,
            )


check_info["dell_idrac_power.unit"] = {
    "inventory_function": inventory_dell_idrac_power_unit,
    "check_function": check_dell_idrac_power_unit,
    "service_description": "Power Supply %s",
}
