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

# .1.3.6.1.4.1.3652.3.2.1.1.0 M9-2 --> SPEEDCARRIER-MIB::nmCarrierName.0
# .1.3.6.1.4.1.3652.3.2.1.2.0 4 --> SPEEDCARRIER-MIB::nmCarrierType.0
# .1.3.6.1.4.1.3652.3.2.1.3.0 3 --> SPEEDCARRIER-MIB::nmPSU1Status.0
# .1.3.6.1.4.1.3652.3.2.1.4.0 3 --> SPEEDCARRIER-MIB::nmPSU2Status.0
# .1.3.6.1.4.1.3652.3.2.1.5.0 3 --> SPEEDCARRIER-MIB::nmFanState.0
# .1.3.6.1.4.1.3652.3.2.1.6.0 8 --> SPEEDCARRIER-MIB::nmCarrierPSU1Type.0
# .1.3.6.1.4.1.3652.3.2.1.7.0 8 --> SPEEDCARRIER-MIB::nmCarrierPSU2Type.0
# .1.3.6.1.4.1.3652.3.2.1.8.0 --> SPEEDCARRIER-MIB::nmCarrierPSU1Text.0
# .1.3.6.1.4.1.3652.3.2.1.9.0 --> SPEEDCARRIER-MIB::nmCarrierPSU2Text.0
# .1.3.6.1.4.1.3652.3.2.1.10.0 --> SPEEDCARRIER-MIB::nmCarrierPSU3Text.0
# .1.3.6.1.4.1.3652.3.2.1.11.0 0 --> SPEEDCARRIER-MIB::nmCarrierPSU3Type.0
# .1.3.6.1.4.1.3652.3.2.1.12.0 0 --> SPEEDCARRIER-MIB::nmPSU3Status.0


def parse_pandacom_psu(info):
    map_psu_type = {
        "0": "type not configured",
        "1": "230 V AC 75 W",
        "2": "230 V AC 160 W",
        "3": "48 V DC 75 W",
        "4": "48 V DC 150 W",
        "5": "48 V DC 60 W",
        "6": "230 V AC 60 W",
        "7": "48 V DC 250 W",
        "8": "230 V AC 250 W",
        "9": "48 V DC 1100 W",
        "10": "230 V AC 1100 W",
        "255": "type not available",
    }
    map_psu_state = {
        "0": (3, "not installed"),
        "1": (2, "fail"),
        "2": (1, "temperature warning"),
        "3": (0, "pass"),
        "255": (3, "not available"),
    }
    parsed = {}
    for psu_nr, type_index, state_index in [
        ("1", 5, 2),
        ("2", 6, 3),
        ("3", 10, 11),
    ]:
        if info[state_index][0] not in ["0", "255"]:
            parsed[psu_nr] = {
                "type": map_psu_type[info[type_index][0]],
                "state": map_psu_state[info[state_index][0]],
            }

    return parsed


def inventory_pandacom_psu(parsed):
    return [(psu_nr, None) for psu_nr in parsed]


def check_pandacom_psu(item, _no_params, parsed):
    if item in parsed:
        state, state_readable = parsed[item]["state"]
        return state, "[%s] Operational status: %s" % (parsed[item]["type"], state_readable)


check_info["pandacom_psu"] = {
    "parse_function": parse_pandacom_psu,
    "inventory_function": inventory_pandacom_psu,
    "check_function": check_pandacom_psu,
    "service_description": "Power Supply %s",
    "snmp_info": (".1.3.6.1.4.1.3652.3.2.1", [""]),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.3652.3",
}
