#!/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 parse_brocade_mlx_power(info):
    parsed = {}

    if len(info[1]) > 0:
    # .1.3.6.1.4.1.1991.1.1.1.2.2.1
        for power_id, power_desc, power_state in info[1]:
            if power_state != "1":
                parsed[power_id] = { 'desc'  : power_desc,
                                       'state' : power_state }
    else:
    # .1.3.6.1.4.1.1991.1.1.1.2.1.1
        for power_id, power_desc, power_state in info[0]:
            if power_state != "1":
                parsed[power_id] = { 'desc'  : power_desc,
                                     'state' : power_state }
    return parsed


def inventory_brocade_mlx_power(parsed):
    inventory = []

    for powersupply_id in parsed.keys():
        inventory.append((powersupply_id, None))
    return inventory


def check_brocade_mlx_power(item, _no_params, parsed):
    if item not in parsed.keys():
        yield 3, "Power supply not found"

    for powersupply_id in parsed.keys():
        if powersupply_id == item:
            if parsed[powersupply_id]['state'] == "2":
                yield 0, "Power supply reports state: normal"
            elif parsed[powersupply_id]['state'] == "3":
                yield 2, "Power supply reports state: failure"
            elif parsed[powersupply_id]['state'] == "1":
                yield 3, "Power supply reports state: other"
            else:
                yield 3, "Power supply reports an unhandled state (%s)" % parsed[powersupply_id]['state']


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', [
                                  2, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupply2Index
                                  3, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupply2Description
                                  4, # FOUNDRY-SN-AGENT-MIB::snChasPwrSupply2OperStatus
                                ])
                              ],
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1991.1."),
}
