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

# .1.3.6.1.4.1.10222.2.1.6.5.1.2.6.1 Fan 201 --> ICS-CHASSIS-MIB::icsChassisFanDescription.6.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.7.1 Fan 202 --> ICS-CHASSIS-MIB::icsChassisFanDescription.7.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.8.1 Fan 203 --> ICS-CHASSIS-MIB::icsChassisFanDescription.8.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.9.1 Fan 204 --> ICS-CHASSIS-MIB::icsChassisFanDescription.9.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.10.1 Fan 205 --> ICS-CHASSIS-MIB::icsChassisFanDescription.10.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.11.1 Fan 101 --> ICS-CHASSIS-MIB::icsChassisFanDescription.11.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.12.1 Fan 102 --> ICS-CHASSIS-MIB::icsChassisFanDescription.12.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.2.13.1 Fan 103 --> ICS-CHASSIS-MIB::icsChassisFanDescription.13.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.6.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.6.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.7.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.7.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.8.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.8.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.9.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.9.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.10.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.10.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.11.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.11.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.12.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.12.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.3.13.1 2 --> ICS-CHASSIS-MIB::icsChassisFanOperStatus.13.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.6.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.6.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.7.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.7.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.8.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.8.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.9.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.9.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.10.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.10.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.11.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.11.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.12.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.12.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.4.13.1 2 --> ICS-CHASSIS-MIB::icsChassisFanSpeed.13.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.6.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.6.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.7.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.7.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.8.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.8.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.9.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.9.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.10.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.10.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.11.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.11.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.12.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.12.1
# .1.3.6.1.4.1.10222.2.1.6.5.1.5.13.1 0 --> ICS-CHASSIS-MIB::icsChassisFanNonFatalErrors.13.1


def inventory_intel_true_scale_fans(info):
    return [
        (fan_name.replace("Fan", "").strip(), None)
        for fan_name, operstate, _speed_state in info
        if operstate != "4"
    ]


def check_intel_true_scale_fans(item, _no_params, info):
    map_states = {
        "operational": {
            "1": (0, "online"),
            "2": (0, "operational"),
            "3": (2, "failed"),
            "4": (1, "offline"),
        },
        "speed": {
            "1": (2, "low"),
            "2": (0, "normal"),
            "3": (2, "high"),
            "4": (3, "unknown"),
        },
    }

    for fan_name, operstate, speedstate in info:
        if item == fan_name.replace("Fan", "").strip():
            for what, what_descr, what_mapping in [
                (operstate, "Operational", "operational"),
                (speedstate, "Speed", "speed"),
            ]:
                state, state_readable = map_states[what_mapping][what]
                yield state, "%s status: %s" % (what_descr, state_readable)


check_info["intel_true_scale_fans"] = {
    "inventory_function": inventory_intel_true_scale_fans,
    "check_function": check_intel_true_scale_fans,
    "service_description": "Fan %s",
    "snmp_info": (
        ".1.3.6.1.4.1.10222.2.1.6.5.1",
        [
            "2",  # ICS-CHASSIS-MIB::icsChassisFanDescription
            "3",  # ICS-CHASSIS-MIB::icsChassisFanOperStatus
            "4",  # ICS-CHASSIS-MIB::icsChassisFanSpeed
        ],
    ),
    "snmp_scan_function": scan_intel_true_scale,
}
