#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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.4.1.9.9.117.1.4.1.1.1.534 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.535 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.536 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.537 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.538 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.539 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.540 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.541 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.113000534 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.113000535 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.113000536 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.116000534 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.116000535 2
# .1.3.6.1.4.1.9.9.117.1.4.1.1.1.116000536 2

# .1.3.6.1.2.1.47.1.1.1.1.7.10 Fabric [VPC domain:12]
# .1.3.6.1.2.1.47.1.1.1.1.7.22 Nexus 56128P Supervisor in Fixed Module-1
# .1.3.6.1.2.1.47.1.1.1.1.7.23 24 ports 10GE SFP+ and 2xQSFP ports Ethernet Module
# .1.3.6.1.2.1.47.1.1.1.1.7.149 Nexus 56128P Chassis
# .1.3.6.1.2.1.47.1.1.1.1.7.214 Fixed Slot-1
# .1.3.6.1.2.1.47.1.1.1.1.7.215 Module Slot-2
# .1.3.6.1.2.1.47.1.1.1.1.7.216 Module Slot-3
# .1.3.6.1.2.1.47.1.1.1.1.7.278 PowerSupplyBay-1
# .1.3.6.1.2.1.47.1.1.1.1.7.279 PowerSupplyBay-2
# .1.3.6.1.2.1.47.1.1.1.1.7.280 PowerSupplyBay-3
# .1.3.6.1.2.1.47.1.1.1.1.7.281 PowerSupplyBay-4
# .1.3.6.1.2.1.47.1.1.1.1.7.342 FanBay-1
# .1.3.6.1.2.1.47.1.1.1.1.7.343 FanBay-2
# .1.3.6.1.2.1.47.1.1.1.1.7.344 FanBay-3
# .1.3.6.1.2.1.47.1.1.1.1.7.345 FanBay-4
# .1.3.6.1.2.1.47.1.1.1.1.7.470 PowerSupply-1
# .1.3.6.1.2.1.47.1.1.1.1.7.471 PowerSupply-2
# .1.3.6.1.2.1.47.1.1.1.1.7.472 PowerSupply-3
# .1.3.6.1.2.1.47.1.1.1.1.7.473 PowerSupply-4
# .1.3.6.1.2.1.47.1.1.1.1.7.534 FanModule-1
# .1.3.6.1.2.1.47.1.1.1.1.7.535 FanModule-2
# .1.3.6.1.2.1.47.1.1.1.1.7.536 FanModule-3
# .1.3.6.1.2.1.47.1.1.1.1.7.537 FanModule-4
# .1.3.6.1.2.1.47.1.1.1.1.7.538 PowerSupply-1 Fan-1
# .1.3.6.1.2.1.47.1.1.1.1.7.539 PowerSupply-1 Fan-2
# .1.3.6.1.2.1.47.1.1.1.1.7.540 PowerSupply-2 Fan-1
# .1.3.6.1.2.1.47.1.1.1.1.7.541 PowerSupply-2 Fan-2
# ...


def parse_cisco_fantray(info):
    map_states = {
        '1': (3, "unknown"),
        '2': (0, "powered on"),
        '3': (2, "powered down"),
        '4': (2, "partial failure, needs replacement as soon as possible.")
    }

    ppre_parsed = {}
    for end_oid, oper_state in info[0]:
        ppre_parsed.setdefault(end_oid, map_states.get(oper_state, (3, "unexpected(%s)" % oper_state)))

    pre_parsed = {}
    for end_oid, name in info[1]:
        if end_oid in ppre_parsed:
            pre_parsed.setdefault(name, [])
            pre_parsed[name].append(ppre_parsed[end_oid])

    parsed = {}
    for name, infos in pre_parsed.items():
        if len(infos) > 1:
            for k, state_info in enumerate(infos):
                parsed["%s-%d" % (name, k+1)] = state_info
        else:
            parsed[name] = infos[0]

    return parsed


def inventory_cisco_fantray(parsed):
    for item in parsed:
        yield item, None


def check_cisco_fantray(item, _no_params, parsed):
    if item in parsed:
        state, state_readable = parsed[item]
        return state, "Status: %s" % state_readable


check_info["cisco_fantray"] = {
    'parse_function'      : parse_cisco_fantray,
    'inventory_function'  : inventory_cisco_fantray,
    'check_function'      : check_cisco_fantray,
    'service_description' : 'Fan %s',
    'snmp_info'           : [('.1.3.6.1.4.1.9.9.117.1.4.1.1', [
                                OID_END,
                                '1',        # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFanTrayOperStatus
                             ]),
                             ('.1.3.6.1.2.1.47.1.1.1.1', [
                                OID_END,
                                CACHED_OID(7), # ENTITY-MIB::entPhysicalName
                            ])],
    'snmp_scan_function'  : lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower() \
                            # Exclude cisco_fan check
                            and not oid(".1.3.6.1.4.1.9.9.13.1.4.1.2.*"),
}
