#!/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 inventory_brocade_info(info):
    data = "".join(brocade_info_try_it(info))
    if data != "----":
        return [ ( None, None ) ]

def brocade_info_try_it(info):
    try:
        model = info[0][0][0]
    except:
        model = "-"
    try:
        wwn = info[2][0][0]
        wwn = " ".join([ "%02X" % ord(tok) for tok in wwn ])
    except:
        wwn = "-"
    try:
        fw = info[1][0][0]
    except:
        fw = "-"
    try:
        ssn = info[1][0][1]
    except:
        ssn = "-"

    return model, ssn, fw, wwn

def brocade_info_parse_wwn(val):
    if val == "":
        val = "-"
    elif val != "-":
        val = ":".join(val.split(" ")[:8])
    return val

def check_brocade_info(item, params, info):
    model, ssn, fw, wwn = brocade_info_try_it(info)
    data = "".join((model, ssn, fw, wwn))
    if data != "----":
        wwn = brocade_info_parse_wwn(wwn)
        infotext = "Model: %s, SSN: %s, Firmware Version: %s, WWN: %s" % ( model, ssn, fw, wwn )
        return 0, infotext
    else:
        return 3, "no information found"

check_info["brocade_info"] = {
    'check_function'        : check_brocade_info,
    'inventory_function'    : inventory_brocade_info,
    'service_description'   : 'Brocade Info',
    'has_perfdata'          : False,
    'snmp_info'             : [
                                ( ".1.3.6.1.2.1.47.1.1.1.1.2",
                                    [
                                        1, # entPhysicalDescr.1
                                    ],
                                ),
                                ( ".1.3.6.1.4.1.1588.2.1.1.1.1",
                                    [
                                        6,  # swFirmwareVersion
                                        10, # swSsn
                                    ]
                                ),
                                ( ".1.3.6.1.3.94.1.6.1",
                                    [
                                        1, # connUnitId
                                    ],
                                ),
                              ],
    'snmp_scan_function'        : lambda oid: (oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588") \
                                               or oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.24.1.1588.2.1.1")) \
                                              and oid(".1.3.6.1.4.1.1588.2.1.1.1.1.6.0") != None,
}
