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

# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.2.1 MCR1900 --> PERLE-MCR-MGT-MIB::chassisModelName.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.4.1 103-001715T10033 --> PERLE-MCR-MGT-MIB::chassisSerialNumber.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.5.1 0.0 --> PERLE-MCR-MGT-MIB::chassisBootloaderVersion.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.6.1 1.0G6 --> PERLE-MCR-MGT-MIB::chassisFirmwareVersion.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.7.1 0 --> PERLE-MCR-MGT-MIB::chassisOutStandWarnAlarms.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.8.1 0 --> PERLE-MCR-MGT-MIB::chassisDiagStatus.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.1.1.9.1 23 --> PERLE-MCR-MGT-MIB::chassisTemperature.1


def inventory_perle_chassis(info):
    return [(None, None)]


def check_perle_chassis(_no_item, _no_params, info):
    map_diag_states = {
        "0": (0, "passed"),
        "1": (1, "firmware download required"),
        "2": (2, "temperature sensor not functional"),
    }

    _model, _serial, _bootloader, _fw, alarms_str, diagstate, _temp_str = info[0]
    state, state_readable = map_diag_states[diagstate]
    yield state, "Diagnostic result: %s" % state_readable
    yield perle_check_alarms(alarms_str)


check_info["perle_chassis"] = {
    "inventory_function": inventory_perle_chassis,
    "check_function": check_perle_chassis,
    "service_description": "Chassis status",
    # If you change snmp info please adapt the related inventory plugin
    "snmp_info": (
        ".1.3.6.1.4.1.1966.21.1.1.1.1.1.1",
        [
            "2",  # PERLE-MCR-MGT-MIB::chassisModelName
            "4",  # PERLE-MCR-MGT-MIB::chassisSerialNumber
            "5",  # PERLE-MCR-MGT-MIB::chassisBootloaderVersion
            "6",  # PERLE-MCR-MGT-MIB::chassisFirmwareVersion
            "7",  # PERLE-MCR-MGT-MIB::chassisOutStandWarnAlarms
            "8",  # PERLE-MCR-MGT-MIB::chassisDiagStatus
            "9",  # PERLE-MCR-MGT-MIB::chassisTemperature
        ],
    ),
    "snmp_scan_function": perle_scan_function,
}


def inventory_perle_chassis_temp(info):
    return [("chassis", {})]


def check_perle_chassis_temp(item, params, info):
    return check_temperature(float(info[0][6]), params, "perle_chassis_temp")


check_info["perle_chassis.temp"] = {
    "inventory_function": inventory_perle_chassis_temp,
    "check_function": check_perle_chassis_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
}
