#!/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_avaya_88xx(info):
    parsed = {"fanstate": [], "temp": []}
    for line in info:
        parsed["fanstate"].append(line[0])
        parsed["temp"].append(line[1])
    return parsed

def inventory_avaya_88xx_fan(parsed):
    for idx, state in enumerate(parsed["fanstate"]):
        yield str(idx), None

def check_avaya_88xx_fan(item, _no_params, parsed):
    fans = parsed["fanstate"]
    if len(fans) < int(item):
        return

    map_fan_state = {
        "1": ("Reported Unknown", 3),
        "2": ("Running", 0),
        "3": ("Down", 2),
    }
    text, state = map_fan_state.get(fans[int(item)], (None, None))
    if not text:
        return

    return state, text

factory_settings["avaya_88xx_default_levels"] = {
    "levels" : (55, 60),
}

def inventory_avaya_88xx(parsed):
    sensors = parsed["temp"]
    for idx, temp in enumerate(sensors):
        yield str(idx), {}

def check_avaya_88xx(item, params, parsed):
    sensors = parsed["temp"]
    if len(sensors) < int(item):
        return

    return check_temperature(int(sensors[int(item)]), params, "avaya_88xx_%s" % item)

check_info["avaya_88xx"] = {
    'parse_function':       parse_avaya_88xx,
    'check_function':       check_avaya_88xx,
    'inventory_function':   inventory_avaya_88xx,
    'service_description':  'Temperature Fan %s',
    'default_levels_variable': "avaya_88xx_default_levels",
    'has_perfdata':         True,
    'group':                'temperature',
    # RAPID-CITY MIB
    'snmp_info':            ( ".1.3.6.1.4.1.2272.1.4.7.1.1", [ 2, 3 ]), # rcChasFanOperStatus, rcChasFanAmbientTemperature
    'snmp_scan_function':   lambda oid: ".1.3.6.1.4.1.2272.59" in oid(".1.3.6.1.2.1.1.2.0"),
    'includes':             [ 'temperature.include' ],
}

check_info["avaya_88xx.fan"] = {
    'check_function':       check_avaya_88xx_fan,
    'inventory_function':   inventory_avaya_88xx_fan,
    'service_description':  'Fan %s Status',
}
