#!/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_raritan_emx(info):
    raritan_map_type = {
        '0'  : ('temp',     'Air'),
        '1'  : ('temp',     'Water'),
        '2'  : ('fanspeed', ''),
        '3'  : ('binary',   ''),
        '4'  : ('valve',    ''),
    }
    parsed = {}
    for rack_id, rack_name, sensor_number, value_text, unit, sensor_state in info:
        rack_type, rack_type_readable = raritan_map_type[sensor_number]

        extra_name = ""
        if rack_type_readable != '':
            extra_name += " " + rack_type_readable

        rack_name = ("Rack %s%s %s" % (rack_id, extra_name, rack_name)).replace("DC", "").strip()

        if rack_type in [ 'binary', '' ]:
            rack_value = None
        else:
            if rack_type == "temp":
                rack_value = float(value_text) / 10
            else:
                rack_value = int(value_text)

        parsed[rack_name] = {
            "rack_type"  : rack_type,
            "rack_unit"  : raritan_map_unit[unit],
            "rack_value" : rack_value,
            "state"      : raritan_map_state[sensor_state],
        }

    return parsed


def inventory_raritan_emx(parsed, rack_type):
    for rack_name, values in parsed.items():
        if values["rack_type"] == rack_type:
            yield rack_name, None


#   .--temperature---------------------------------------------------------.
#   |      _                                      _                        |
#   |     | |_ ___ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |     | __/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |     | ||  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      \__\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def inventory_raritan_emx_temp(parsed):
    for rack_name, values in parsed.items():
        if values["rack_type"] == "temp":
            yield rack_name, {}


def check_raritan_emx_temp(item, params, parsed):
    if "Temperature" in item:
        # old style (pre 1.2.8) item name, convert
        item = "Rack " + item.replace(" Temperature", "")
    elif "Fan Speed" in item:
        item = "Rack " + item.replace(" Fan Speed", "")
        return check_raritan_emx_fan(item, params, parsed)
    elif "Door Contact" in item:
        item = "Rack " + item.replace(" Door Contact DC", "")
        return check_raritan_sensors_binary(item, params, parsed)

    if item in parsed:
        rack_value = parsed[item]["rack_value"]
        rack_unit  = parsed[item]["rack_unit"]
        state, state_readable = parsed[item]["state"]
        return check_temperature(rack_value, params, item,
                                 dev_unit=rack_unit, dev_status = state,
                                 dev_status_name = state_readable)


check_info['raritan_emx'] = {
    "parse_function"        : parse_raritan_emx,
    "inventory_function"    : inventory_raritan_emx_temp,
    "check_function"        : check_raritan_emx_temp,
    "service_description"   : "Temperature %s",
    "has_perfdata"          : True,
    "snmp_info"             : ( ".1.3.6.1.4.1.13742.9", [
                                    "1.4.1.1.1",    # Rack ID
                                    "1.4.1.1.4",    # Name
                                    "1.4.1.1.2",    # Sensor
                                    "2.1.1.3",      # Value
                                    "1.4.1.1.5",    # Unit
                                    "2.1.1.2",      # State
                              ]),
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0") in [".1.3.6.1.4.1.13742.8"],
    "group"                 : "temperature",
    "includes"              : [ "temperature.include", "raritan.include" ],
}

#.
#   .--fan-----------------------------------------------------------------.
#   |                            __                                        |
#   |                           / _| __ _ _ __                             |
#   |                          | |_ / _` | '_ \                            |
#   |                          |  _| (_| | | | |                           |
#   |                          |_|  \__,_|_| |_|                           |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def check_raritan_emx_fan(item, _no_params, parsed):
    if item in parsed:
        fan_speed = parsed[item]["rack_value"]
        fan_unit  = parsed[item]["rack_unit"]
        state, state_readable = parsed[item]["state"]
        return state, "Speed: %d%s, status: %s" % (fan_speed, fan_unit, state_readable)


check_info['raritan_emx.fan'] = {
    "inventory_function"    : lambda parsed: inventory_raritan_emx(parsed, "fanspeed"),
    "check_function"        : check_raritan_emx_fan,
    "service_description"   : "Fan %s",
}

#.
#   .--binary--------------------------------------------------------------.
#   |                   _     _                                            |
#   |                  | |__ (_)_ __   __ _ _ __ _   _                     |
#   |                  | '_ \| | '_ \ / _` | '__| | | |                    |
#   |                  | |_) | | | | | (_| | |  | |_| |                    |
#   |                  |_.__/|_|_| |_|\__,_|_|   \__, |                    |
#   |                                            |___/                     |
#   '----------------------------------------------------------------------'

check_info['raritan_emx.binary'] = {
    "inventory_function"    : lambda parsed: inventory_raritan_emx(parsed, "binary"),
    "check_function"        : check_raritan_sensors_binary,
    "service_description"   : "Door %s",
}

