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


factory_settings["ibm_imm_fan_default_levels"] = {
    "levels_lower" : (28, 25), # Just a guess. Please give feedback.
}


def inventory_ibm_imm_fan(info):
    for descr, speed_text in info:
        if speed_text.lower() != 'offline':
            yield descr, {}


def check_ibm_imm_fan(item, params, info):
    for descr, speed_text in info:
        if descr == item:

            if speed_text.lower() == "offline":
                yield 2, "is offline"
                return

            # speed_text can be "34 %", or "34%", or "34 % of maximum"
            # or simply a text without quotes..
            rpm_perc = int(speed_text.strip().replace("[\"%]", " ").replace("%", " ").split(" ")[0])
            yield 0, "%d%% of max RPM" % rpm_perc

            levels_info = ""

            warn_lower, crit_lower = params['levels_lower']
            warn, crit = params.get('levels', (None, None))

            if warn_lower:
                if rpm_perc < crit_lower:
                    state = 2
                elif rpm_perc < warn_lower:
                    state = 1
                else:
                    state = 0
                if state > 0:
                    yield state, "too low (warn/crit below %d%%/%d%%)" % (warn_lower, crit_lower)

            if warn:
                if rpm_perc >= crit:
                    state = 2
                elif rpm_perc >= warn:
                    state = 1
                else:
                    state = 0
                if state > 0:
                    yield state, "too high (warn/crit at %d%%/%d%%)" % (warn, crit)



check_info["ibm_imm_fan"] = {
    'check_function'          : check_ibm_imm_fan,
    'inventory_function'      : inventory_ibm_imm_fan,
    'service_description'     : 'Fan %s',
    'snmp_scan_function'      :
            lambda oid: oid('.1.3.6.1.2.1.1.1.0').lower().endswith(" mips") or \
                        oid('.1.3.6.1.2.1.1.1.0').lower().endswith(" sh4a"),
    'snmp_info'               : ('.1.3.6.1.4.1.2.3.51.3.1.3.2.1', [ # fanTable.fanEntry
                                        2, # fanDescr
                                        3, # fanSpeed (as text)
                                ]),
    "default_levels_variable" : "ibm_imm_fan_default_levels",
    'group'                   : 'hw_fans_perc',
}
