#!/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["hwg_temp_defaultlevels"] = {"levels": (30, 35)}


def parse_hwg_temp(info):
    map_units = {
        "1": "c",
        "2": "f",
        "3": "k",
        # Also possible but invalid: "4"
    }

    map_dev_states = {
        "0": "invalid",
        "1": "normal",
        "2": "out of range low",
        "3": "out of range high",
        "4": "alarm low",
        "5": "alarm high",
    }

    parsed = {}
    for index, descr, sensorstatus, current, unit in info:
        try:
            tempval = float(current)
        except ValueError:
            tempval = None

        parsed.setdefault(
            index, {
                "description": descr,
                "dev_unit": map_units.get(unit),
                "temperature": tempval,
                "dev_status_name": map_dev_states.get(sensorstatus, ""),
                "dev_status": sensorstatus,
            })

    return parsed


def inventory_hwg_temp(parsed):
    for index, attrs in parsed.iteritems():
        if attrs["dev_status_name"] not in ["invalid", ""] \
           and attrs["dev_unit"] in ["c", "f", "k"]:
            yield index, {}


@get_parsed_item_data
def check_hwg_temp(item, params, attrs):
    map_readable_states = {
        "invalid": 3,
        "normal": 0,
        "out of range low": 2,
        "out of range high": 2,
        "alarm low": 2,
        "alarm high": 2,
    }

    state = map_readable_states.get(attrs["dev_status_name"], 3)
    state_readable = attrs["dev_status_name"]
    temp = attrs["temperature"]
    if temp is None:
        return state, "Status: %s" % state_readable

    state, infotext, perfdata = check_temperature(temp,
                                                  params,
                                                  "hwg_temp_%s" % item,
                                                  dev_unit=attrs["dev_unit"],
                                                  dev_status=state,
                                                  dev_status_name=state_readable)
    descr = attrs["description"]
    return state, "%s%s" % ("[%s] " % descr if descr else "", infotext), perfdata


check_info['hwg_temp'] = {
    'parse_function': parse_hwg_temp,
    "check_function": check_hwg_temp,
    "inventory_function": inventory_hwg_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.21796.4.1.3",
        [  # DAMOCLES-MIB.txt
            "1.1",  # index
            "1.2",  # sensName
            "1.3",  # sensState
            "1.4",  # sensString
            "1.7",  # sensUnit
        ]),
    "snmp_scan_function": lambda oid: "hwg" in oid(".1.3.6.1.2.1.1.1.0", "").lower(),
    "group": "temperature",
    "includes": ["temperature.include"],
    "default_levels_variable": "hwg_temp_defaultlevels"
}
