#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


# .1.3.6.1.2.1.47.1.1.1.1.2.1   "CISCO1921/K9 chassis" --> ENTITY-MIB::entPhysicalDescr.1
# .1.3.6.1.2.1.47.1.1.1.1.2.2   "C1921 Chassis Slot" --> ENTITY-MIB::entPhysicalDescr.2
# .1.3.6.1.2.1.47.1.1.1.1.2.3   "C1921 Mother board 2GE, integrated VPN and 2W" --> ENTITY-MIB::entPhysicalDescr.3
# .1.3.6.1.2.1.47.1.1.1.1.2.4   "C1921 DaughterCard Slot" --> ENTITY-MIB::entPhysicalDescr.4
# .1.3.6.1.2.1.47.1.1.1.1.2.5   "ADSL/VDSL over POTS supporting ADSL1, ADSL2, ADSL2+ and VDSL2" --> ENTITY-MIB::entPhysicalDescr.5
# .1.3.6.1.2.1.47.1.1.1.1.2.6   "MPC ATMSAR" --> ENTITY-MIB::entPhysicalDescr.6
# .1.3.6.1.2.1.47.1.1.1.1.2.7   "VDSL_ETHERNET" --> ENTITY-MIB::entPhysicalDescr.7
# .1.3.6.1.2.1.47.1.1.1.1.2.8   "C1921 DaughterCard Slot" --> ENTITY-MIB::entPhysicalDescr.8
# .1.3.6.1.2.1.47.1.1.1.1.2.9   "C1921 ISM Slot" --> ENTITY-MIB::entPhysicalDescr.9
# .1.3.6.1.2.1.47.1.1.1.1.2.10  "Embedded Service Engine" --> ENTITY-MIB::entPhysicalDescr.10
# .1.3.6.1.2.1.47.1.1.1.1.2.11  "CN Gigabit Ethernet" --> ENTITY-MIB::entPhysicalDescr.11
# .1.3.6.1.2.1.47.1.1.1.1.2.12  "CN Gigabit Ethernet" --> ENTITY-MIB::entPhysicalDescr.12
# .1.3.6.1.2.1.47.1.1.1.1.2.13  "Container of powerSupply Containers" --> ENTITY-MIB::entPhysicalDescr.13
# .1.3.6.1.2.1.47.1.1.1.1.2.14  "Container of Power Supply" --> ENTITY-MIB::entPhysicalDescr.14
# .1.3.6.1.2.1.47.1.1.1.1.2.15  "" --> ENTITY-MIB::entPhysicalDescr.15
# .1.3.6.1.2.1.47.1.1.1.1.5.1   3  --> ENTITY-MIB::entPhysicalClass.1
# .1.3.6.1.2.1.47.1.1.1.1.5.2   5  --> ENTITY-MIB::entPhysicalClass.2
# .1.3.6.1.2.1.47.1.1.1.1.5.3   9  --> ENTITY-MIB::entPhysicalClass.3
# .1.3.6.1.2.1.47.1.1.1.1.5.4   5  --> ENTITY-MIB::entPhysicalClass.4
# .1.3.6.1.2.1.47.1.1.1.1.5.5   9  --> ENTITY-MIB::entPhysicalClass.5
# .1.3.6.1.2.1.47.1.1.1.1.5.6   10 --> ENTITY-MIB::entPhysicalClass.6
# .1.3.6.1.2.1.47.1.1.1.1.5.7   10 --> ENTITY-MIB::entPhysicalClass.7
# .1.3.6.1.2.1.47.1.1.1.1.5.8   5  --> ENTITY-MIB::entPhysicalClass.8
# .1.3.6.1.2.1.47.1.1.1.1.5.9   5  --> ENTITY-MIB::entPhysicalClass.9
# .1.3.6.1.2.1.47.1.1.1.1.5.10  10 --> ENTITY-MIB::entPhysicalClass.10
# .1.3.6.1.2.1.47.1.1.1.1.5.11  10 --> ENTITY-MIB::entPhysicalClass.11
# .1.3.6.1.2.1.47.1.1.1.1.5.12  10 --> ENTITY-MIB::entPhysicalClass.12
# .1.3.6.1.2.1.47.1.1.1.1.5.13  5  --> ENTITY-MIB::entPhysicalClass.13
# .1.3.6.1.2.1.47.1.1.1.1.5.14  5  --> ENTITY-MIB::entPhysicalClass.14
# .1.3.6.1.2.1.47.1.1.1.1.5.15  6  --> ENTITY-MIB::entPhysicalClass.15
# .1.3.6.1.2.1.47.1.1.1.1.7.1   "CISCO1921/K9 chassis" --> ENTITY-MIB::entPhysicalName.1
# .1.3.6.1.2.1.47.1.1.1.1.7.2   "C1921 Chassis Slot 0" --> ENTITY-MIB::entPhysicalName.2
# .1.3.6.1.2.1.47.1.1.1.1.7.3   "C1921 Mother board 2GE, integrated VPN and 2W on Slot 0" --> ENTITY-MIB::entPhysicalName.3
# .1.3.6.1.2.1.47.1.1.1.1.7.4   "DaughterCard Slot 0 on Card 0" --> ENTITY-MIB::entPhysicalName.4
# .1.3.6.1.2.1.47.1.1.1.1.7.5   "ADSL/VDSL over POTS supporting ADSL1, ADSL2, ADSL2+ and VDSL2 on Slot 0 SubSlot 0" --> ENTITY-MIB::entPhysicalName.5
# .1.3.6.1.2.1.47.1.1.1.1.7.6   "ATM0/0/0" --> ENTITY-MIB::entPhysicalName.6
# .1.3.6.1.2.1.47.1.1.1.1.7.7   "Ethernet0/0/0" --> ENTITY-MIB::entPhysicalName.7
# .1.3.6.1.2.1.47.1.1.1.1.7.8   "DaughterCard Slot 1 on Card 0" --> ENTITY-MIB::entPhysicalName.8
# .1.3.6.1.2.1.47.1.1.1.1.7.9   "C1921 ISM Slot 0" --> ENTITY-MIB::entPhysicalName.9
# .1.3.6.1.2.1.47.1.1.1.1.7.10  "Embedded-Service-Engine0/0" --> ENTITY-MIB::entPhysicalName.10
# .1.3.6.1.2.1.47.1.1.1.1.7.11  "GigabitEthernet0/0" --> ENTITY-MIB::entPhysicalName.11
# .1.3.6.1.2.1.47.1.1.1.1.7.12  "GigabitEthernet0/1" --> ENTITY-MIB::entPhysicalName.12
# .1.3.6.1.2.1.47.1.1.1.1.7.13  "Container of powerSupply Containers" --> ENTITY-MIB::entPhysicalName.13
# .1.3.6.1.2.1.47.1.1.1.1.7.14  "Container of Power Supply" --> ENTITY-MIB::entPhysicalName.14
# .1.3.6.1.2.1.47.1.1.1.1.7.15  "" --> ENTITY-MIB::entPhysicalName.15

# .1.3.6.1.4.1.9.9.117.1.2.1.1.2.5  2 --> CISCO-ENTITY-FRU-CONTROL-MIB::cefcModuleOperStatus.5


def parse_cisco_fru_module_status(info):
    map_state = {
        "1"  : (2, "unknown"),
        "2"  : (0, "OK"),
        "3"  : (1, "disabled"),
        "4"  : (1, "OK but diag failed"),
        "5"  : (1, "boot"),
        "6"  : (1, "self test"),
        "7"  : (2, "failed"),
        "8"  : (2, "missing"),
        "9"  : (2, "mismatch with parent"),
        "10" : (2, "mismatch config"),
        "11" : (2, "diag failed"),
        "12" : (2, "dormant"),
        "13" : (2, "out of service (admin)"),
        "14" : (2, "out of service (temperature)"),
        "15" : (2, "powered down"),
        "16" : (1, "powered up"),
        "17" : (2, "power denied"),
        "18" : (1, "power cycled"),
        "19" : (1, "OK but power over warning"),
        "20" : (1, "OK but power over critical"),
        "21" : (1, "sync in progress"),
        "22" : (1, "upgrading"),
        "23" : (1, "OK but auth failed"),
        "24" : (1, "minimum disruptive restart upgrade"),
        "25" : (1, "firmware mismatch found"),
        "26" : (1, "firmware download success"),
        "27" : (2, "firmware download failure"),
    }
    collected_entities = {}
    for oid, hardware_descr, hardware_type, hardware_name in info[0]:
        if hardware_type == "9":
            collected_entities.setdefault(oid, hardware_name)

    parsed = {}
    for oid, module_state in info[1]:
        if oid in collected_entities:
            parsed[oid] = {
                "state" : map_state[module_state],
                "name"  : collected_entities[oid],
            }

    return parsed


def inventory_cisco_fru_module_status(parsed):
    for module_index in parsed:
        yield (module_index, None)


def check_cisco_fru_module_status(item, _no_params, parsed):
    if item in parsed:
        extra_info = ""
        if parsed[item]["name"]:
            extra_info += "[%s] " % parsed[item]["name"]
        state, state_readable = parsed[item]["state"]
        return state, "%sOperational status: %s" % \
                      (extra_info, state_readable)


check_info['cisco_fru_module_status'] = {
    'parse_function'      : parse_cisco_fru_module_status,
    'inventory_function'  : inventory_cisco_fru_module_status,
    'check_function'      : check_cisco_fru_module_status,
    'service_description' : 'FRU Module Status %s',
    'snmp_info'           : [(".1.3.6.1.2.1.47.1.1.1.1", [
                                  OID_END,
                                  "2",    # entPhysicalDescription
                                  "5",    # entPhysicalClass
                                  "7",    # entPhysicalName
                             ]),
                             (".1.3.6.1.4.1.9.9.117.1.2.1.1", [
                                  OID_END,
                                  "2",    # cefcModuleOperStatus
                             ])],
    'snmp_scan_function'  : lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower() \
                               and oid(".1.3.6.1.4.1.9.9.117.1.*"),
}
