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

# Example output from agent:
# [['1', '24', 'SLOT #0: TEMP #1'],
# ['2', '12', 'SLOT #0: TEMP #2'],
# ['3', '12', 'SLOT #0: TEMP #3'],
# ['4', '4687', 'FAN #1'],
# ['5', '4560', 'FAN #2'],
# ['6', '4821', 'FAN #3'],
# ['7', '1', 'Power Supply #1'],
# ['8', '1', 'Power Supply #2']]


def brocade_sensor_convert(info, what):
    return_list = []
    for presence, state, name in info:
        if name.startswith(what) and presence != "6" and (saveint(state) > 0 or what == "Power"):
            sensor_id  = name.split('#')[-1]
            return_list.append([sensor_id, name, state])
    return return_list

def brocade_scan(oid):
    return oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.1.1") or \
           oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.24.1.1588.2.1.1") or \
           oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.2.1") or \
           oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.3.3.1")

brocade_info = ('.1.3.6.1.4.1.1588.2.1.1.1.1.22.1', [
                    3, # swSensorStatus, 6 = absent
                    4, # swSensorValue, -2147483648 = unknown
                    5, # swSensorInfo
                ])

brocade_fan_default_levels = { "lower": (3000 , 2800 ) }

def inventory_brocade_fan(info):
    converted = brocade_sensor_convert(info, "FAN")
    return [ (x[0], 'brocade_fan_default_levels') for x in converted ]

def check_brocade_fan(item, params, info):
    converted = brocade_sensor_convert(info, "FAN")
    if type(params) is tuple:  # old format
        warn, crit = params
    else:  # new format
        warn, crit = params.get("lower")
        if params.get("upper"):
            upperwarn, uppercrit  = params["upper"]
        else:
            upperwarn, uppercrit = ( None, None )

    for snmp_item, name, value in converted:
        if item == snmp_item:
            state = 0
            label = ""
            value = saveint(value)
            perf = [ ('fan', value, warn, crit) ]
            if value <= crit:
                state = 2
                label = "(Levels below: %d/%d)" % (warn, crit)
            elif value <= warn:
                state = 1
                label = "(Levels below: %d/%d)" % (warn, crit)
            elif uppercrit and value >= uppercrit:
                state = 2
                label = "(Levels above: %d/%d)" % (upperwarn, uppercrit)
            elif upperwarn and value >= upperwarn:
                state = 1
                label = "(Levels above: %d/%d)" % (upperwarn, uppercrit)
            return state, "Fans at %drpm %s " % (value, label), perf
    return 3, "FAN not found"

check_info["brocade.fan"] = {
    "check_function"        : check_brocade_fan,
    "inventory_function"    : inventory_brocade_fan,
    "service_description"   : "FAN %s",
    "has_perfdata"          : True,
    "group"                 : "hw_fans",
    "snmp_info"             : brocade_info,
    'snmp_scan_function'    : brocade_scan,
}

def inventory_brocade_power(info):
    converted = brocade_sensor_convert(info, "Power")
    return [ (x[0], None) for x in converted ]

def check_brocade_power(item, _no_params, info):
    converted = brocade_sensor_convert(info, "Power")
    for snmp_item, name, value in converted:
        if item == snmp_item:
            value = saveint(value)
            if value != 1:
                return 2, "Error on supply %s" % name
            return 0, "No problems found"

    return 3, "Supply not found"

check_info["brocade.power"] = {
    "check_function"        : check_brocade_power,
    "inventory_function"    : inventory_brocade_power,
    "service_description"   : "Power supply %s",
    "has_perfdata"          : False,
    "snmp_info"             : brocade_info,
    'snmp_scan_function'    : brocade_scan,
}

brocade_temp_default_levels = ( 30, 40 )

def inventory_brocade_temp(info):
    converted = brocade_sensor_convert(info, "SLOT")
    return [ (x[0], 'brocade_temp_default_levels') for x in converted ]

def check_brocade_temp(item, params, info):
    converted = brocade_sensor_convert(info, "SLOT")
    for snmp_item, name, value in converted:
        if item == snmp_item:
            return check_temperature(int(value), params)


check_info["brocade.temp"] = {
    "check_function"        : check_brocade_temp,
    "inventory_function"    : inventory_brocade_temp,
    "service_description"   : "Temperature %s",
    "has_perfdata"          : True,
    "group"                 : "hw_temperature",
    "snmp_info"             : brocade_info,
    'snmp_scan_function'    : brocade_scan,
    "includes"              : [ "temperature.include" ],
}
