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

brocade_mlx_temperature_default_levels = (50, 60)

def compose_item(id, name):
    id = re.sub("\.[0-9]+", "", id)
    name = re.sub(" *temperature", "", name)
    name = re.sub(", sensor [0-9]+", "", name)
    name = re.sub("module ?[0-9]*", "Module %s" % id, name)
    return name

def inventory_brocade_mlx_temp(info):
    inventory = []
    for temp_descr, temp_id, temp_value in info:
        temp_descr = compose_item(temp_id, temp_descr)
        # BigIron RX devices have problems when queried by SNMPv2c bulk walk and
        # do not deliver values. So in this case we do not inventorize them to
        # avoid the check to break
        if ((temp_descr, "brocade_mlx_temperature_default_levels") not in inventory and \
            temp_value != ""):
            inventory.append( (temp_descr, "brocade_mlx_temperature_default_levels") )
    return inventory

def check_brocade_mlx_temp(item, params, info):
    warn, crit = params
    status = 0
    message = []
    perfdata = []

    for temp_descr, temp_id, temp_value in info:
        current_item = compose_item(temp_id, temp_descr)
        if current_item == item:
            # OID_END needs to be used for sensor id because especially
            # Active management modules may have more temperature sensors
            # with the same description
            temp_id = re.sub("[0-9]+\.", "", temp_id)

            # some devices do not deliver values on one single check in
            # between - setting to unknown in this case
            if temp_value == "":
               return 3, "No temperature value delivered by SNMP for sensor " + temp_id

            # Info from the MIB: "Each unit is 0.5 degrees Celcius."
            temp_value = int(temp_value) / 2

            txt = "Sensor %s: %s°C" % (temp_id, temp_value)
            if temp_value > crit:
                status = max(status, 2)
                txt += "(!!)"
            elif temp_value > warn:
                status = max(status, 1)
                txt += "(!)"

            perfdata.append(('sensor%s' % temp_id, temp_value, warn, crit))
            message.append(txt)

    if not message:
        return 3, "Temperature sensor not found"
    else:
        return status, ', '.join(message), perfdata

check_info["brocade_mlx_temp"] = {
    "check_function"        : check_brocade_mlx_temp,
    "inventory_function"    : inventory_brocade_mlx_temp,
    "service_description"   : "Temperature %s",
    "snmp_info"             : ('.1.3.6.1.4.1.1991.1.1.2.13.1.1', [ 3, OID_END, 4 ]), # descr, sensor ID, temperature
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1991.1."),
    "has_perfdata"          : True,
    "group"                 : "hw_temperature",
}
