#!/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
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.2.1.1 1 --> PERLE-MCR-MGT-MIB::mcrChassisSlotIndex.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.2.1.2 2 --> PERLE-MCR-MGT-MIB::mcrChassisSlotIndex.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.3.1.1 PerleMC01 --> PERLE-MCR-MGT-MIB::mcrUserDefinedModuleName.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.3.1.2 CM-1000-SFP --> PERLE-MCR-MGT-MIB::mcrUserDefinedModuleName.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.4.1.1 MCR-MGT --> PERLE-MCR-MGT-MIB::mcrModuleModelName.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.4.1.2 CM-1000-SFP --> PERLE-MCR-MGT-MIB::mcrModuleModelName.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.6.1.1 101-693515M10019 --> PERLE-MCR-MGT-MIB::mcrModuleSerialNumber.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.6.1.2 102-094710M10033 --> PERLE-MCR-MGT-MIB::mcrModuleSerialNumber.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.7.1.1 01.01.0004 --> PERLE-MCR-MGT-MIB::mcrModuleBootloaderVersion.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.7.1.2 1.1 --> PERLE-MCR-MGT-MIB::mcrModuleBootloaderVersion.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.8.1.1 1.8.G4 --> PERLE-MCR-MGT-MIB::mcrModuleFirmwareVersion.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.8.1.2 1.2G1 --> PERLE-MCR-MGT-MIB::mcrModuleFirmwareVersion.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.9.1.1 0 --> PERLE-MCR-MGT-MIB::mcrModuleoOutStandWarnAlarms.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.9.1.2 0 --> PERLE-MCR-MGT-MIB::mcrModuleoOutStandWarnAlarms.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.10.1.1 0 --> PERLE-MCR-MGT-MIB::mcrModuleDiagStatus.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.10.1.2 0 --> PERLE-MCR-MGT-MIB::mcrModuleDiagStatus.1.2
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.11.1.1 -2 --> PERLE-MCR-MGT-MIB::mcrModuleTypeInserted.1.1
# .1.3.6.1.4.1.1966.21.1.1.1.1.3.1.11.1.2 1 --> PERLE-MCR-MGT-MIB::mcrModuleTypeInserted.1.2


def inventory_perle_chassis_slots(info):
    return [ (index, None)
             for index, _name, _modelname, _serial, _bootloader, _fw, \
             _alarms_str, _diagstate, ty, _descr in info if ty != "0" ]


def check_perle_chassis_slots(item, _no_params, info):
    map_diagstates = {
        "0": (0, "passed"),
        "1": (2, "media converter module's PHY is not functional"),
        "2": (1, "firmware download required"),
    }

    for index, name, _modelname, _serial, _bootloader, _fw, \
        alarms_str, diagstate, _ty, _descr in info:
        if item == index:
            state, state_readable = map_diagstates[diagstate]
            yield state, "[%s] Diagnostic result: %s" % (name, state_readable)
            yield perle_check_alarms(alarms_str)


check_info['perle_chassis_slots'] = {
    'inventory_function': inventory_perle_chassis_slots,
    'check_function': check_perle_chassis_slots,
    'service_description': 'Chassis status slot %s',
    # 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.3.1',
        [
            '2',  # PERLE-MCR-MGT-MIB::mcrChassisSlotIndex
            '3',  # PERLE-MCR-MGT-MIB::mcrDefinedModuleName
            '4',  # PERLE-MCR-MGT-MIB::mcrModuleModelName
            '6',  # PERLE-MCR-MGT-MIB::mcrModuleSerialNumber
            '7',  # PERLE-MCR-MGT-MIB::mcrModuleBootloaderVersion
            '8',  # PERLE-MCR-MGT-MIB::mcrModuleFirmwareVersion
            '9',  # PERLE-MCR-MGT-MIB::mcrModuleoOutStandWarnAlarms
            '10',  # PERLE-MCR-MGT-MIB::mcrModuleDiagStatus
            '11',  # PERLE-MCR-MGT-MIB::mcrModuleTypeInserted
            '19',  # PERLE-MCR-MGT-MIB::mcrModuleModelDesc
        ]),
    'snmp_scan_function': perle_scan_function,
}
