#!/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.

def brocade_mlx_power_combine_item(id, descr):
    if descr == "" or "AC " in descr or "Power" in descr:
        return id
    else:
        return "%s %s" % (id, descr)


def parse_brocade_mlx_power(info):
    parsed = []

    if len(info[1]) > 0:
        for power_unit, power_id, power_descr, power_state in info[1]:
            if power_state != "1":
                parsed.append([power_unit, power_descr, power_state])
    else:
        for power_id, power_descr, power_state in info[0]:
            if power_state != "1":
                parsed.append([power_id, power_descr, power_state])
    return parsed


def inventory_brocade_mlx_power(parsed):
    inventory = []

    for supply in parsed:
        inventory.append( (brocade_mlx_power_combine_item(supply[0], supply[1]), None) )
    return inventory


def check_brocade_mlx_power(item, _no_params, parsed):
    for supply in parsed:
        if supply[0] == item:
            if supply[2] == "2":
                return 0, "Power supply reports state: normal"
            elif supply[2] == "3":
                return 2, "Power supply reports state: failure"
            elif supply[2] == "1":
                return 3, "Power supply reports state: other"
            else:
                return 3, "Power supply reports an unhandled state (%s)" % supply[2]
    return 3, "Power supply not found"


check_info["brocade_mlx_power"] = {
    "parse_function"        : parse_brocade_mlx_power,
    "check_function"        : check_brocade_mlx_power,
    "inventory_function"    : inventory_brocade_mlx_power,
    "service_description"   : "Power supply %s",
    "snmp_info"             : [ ('.1.3.6.1.4.1.1991.1.1.1.2.1.1', [
                                  1, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyIndex
                                  2, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyDescription
                                  3, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyOperStatus
                                ]),
                                ('.1.3.6.1.4.1.1991.1.1.1.2.2.1', [
                                  1, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyUnit
                                  2, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyIndex
                                  3, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyDescription
                                  4, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupplyOperStatus
                                ])
                              ],
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1991.1."),
}
