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

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

# .1.3.6.1.4.1.10222.2.1.4.7.1.2.2.1 Power Supply 201 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyDescription.2.1
# .1.3.6.1.4.1.10222.2.1.4.7.1.2.3.2 Power Supply 202 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyDescription.3.2
# .1.3.6.1.4.1.10222.2.1.4.7.1.2.4.3 Power Supply 203 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyDescription.4.3
# .1.3.6.1.4.1.10222.2.1.4.7.1.2.5.4 Power Supply 204 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyDescription.5.4
# .1.3.6.1.4.1.10222.2.1.4.7.1.3.2.1 6 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOperStatus.2.1
# .1.3.6.1.4.1.10222.2.1.4.7.1.3.3.2 6 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOperStatus.3.2
# .1.3.6.1.4.1.10222.2.1.4.7.1.3.4.3 6 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOperStatus.4.3
# .1.3.6.1.4.1.10222.2.1.4.7.1.3.5.4 6 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOperStatus.5.4
# .1.3.6.1.4.1.10222.2.1.4.7.1.4.2.1 1 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyInputSource.2.1
# .1.3.6.1.4.1.10222.2.1.4.7.1.4.3.2 1 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyInputSource.3.2
# .1.3.6.1.4.1.10222.2.1.4.7.1.4.4.3 1 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyInputSource.4.3
# .1.3.6.1.4.1.10222.2.1.4.7.1.4.5.4 1 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyInputSource.5.4
# .1.3.6.1.4.1.10222.2.1.4.7.1.5.2.1 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyVoltage.2.1
# .1.3.6.1.4.1.10222.2.1.4.7.1.5.3.2 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyVoltage.3.2
# .1.3.6.1.4.1.10222.2.1.4.7.1.5.4.3 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyVoltage.4.3
# .1.3.6.1.4.1.10222.2.1.4.7.1.5.5.4 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyVoltage.5.4
# .1.3.6.1.4.1.10222.2.1.4.7.1.6.2.1 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOutputPower.2.1
# .1.3.6.1.4.1.10222.2.1.4.7.1.6.3.2 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOutputPower.3.2
# .1.3.6.1.4.1.10222.2.1.4.7.1.6.4.3 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOutputPower.4.3
# .1.3.6.1.4.1.10222.2.1.4.7.1.6.5.4 0 --> ICS-CHASSIS-MIB::icsChassisPowerSupplyOutputPower.5.4


def parse_intel_true_scale_psus(info):
    map_states = {
        "1": (3, "unknown"),
        "2": (3, "disabled"),
        "3": (2, "failed"),
        "4": (1, "warning"),
        "5": (0, "standby"),
        "6": (0, "engaged"),
        "7": (0, "redundant"),
        "8": (3, "not present"),
    }
    map_sources = {
        "0": "invalid",
        "1": "ac line",
        "2": "dc line",
        "3": "none",
        "4": "unknown",
    }

    parsed = {}
    for descr, operstate, source, voltage_str, power_str in info:
        name = descr.replace("Power Supply", "").strip()

        parsed.setdefault(
            name,
            {
                "voltage": float(voltage_str),
                "power": float(power_str),
                "state": map_states[operstate],
                "source": map_sources[source],
            },
        )

    return parsed


def inventory_intel_true_scale_psus(parsed):
    for psu in parsed:
        if parsed[psu]["state"][-1] not in ["not present", "disabled"]:
            yield psu, {}


def check_intel_true_scale_psus(item, params, parsed):
    if item in parsed:
        state, state_readable = parsed[item]["state"]
        yield state, "Operational status: %s, Source: %s" % (state_readable, parsed[item]["source"])

        for res in check_elphase(item, params, parsed):
            yield res


check_info["intel_true_scale_psus"] = {
    "parse_function": parse_intel_true_scale_psus,
    "inventory_function": inventory_intel_true_scale_psus,
    "check_function": check_intel_true_scale_psus,
    "service_description": "Power supply %s",
    "snmp_info": (
        ".1.3.6.1.4.1.10222.2.1.4.7.1",
        [
            "2",  # ICS-CHASSIS-MIB::icsChassisPowerSupplyDescription
            "3",  # ICS-CHASSIS-MIB::icsChassisPowerSupplyOperStatus
            "4",  # ICS-CHASSIS-MIB::icsChassisPowerSupplyInputSource
            "5",  # ICS-CHASSIS-MIB::icsChassisPowerSupplyVoltage
            "6",  # ICS-CHASSIS-MIB::icsChassisPowerSupplyOutputPower
        ],
    ),
    "snmp_scan_function": scan_intel_true_scale,
    "group": "el_inphase",
}
