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


hwg_temp_defaultlevels = (30, 35)

def inventory_hwg_temp(info):
    return [ (line[0], "hwg_temp_defaultlevels")
              for line in info
              if int(line[2]) != 0 and line[4] in ["1", "2", "3"] ]


def check_hwg_temp(item, params, info):
    status_text = {
        "0" : "Invalid",
        "1" : "Normal",
        "2" : "Out Of Range Low",
        "3" : "Out Of Range High",
        "4" : "Alarm Low",
        "5" : "Alarm High",
    }

    unit_text = {
        "0" : "unknown",
        "1" : "°C",
        "2" : "°F",
        "3" : "°K",
        "4" : "%",
    }

    # Nomenclature in this check: sensorstatus is what the device sends, state is what the check returns.
    for index, descr, sensorstatus, current, unit in info:
        if index == item:
            tempval = float(current)
            if unit == "2":
                temp = fahrenheit_to_celsius(tempval)
            elif unit == "3":
                temp = tempval - 273.15
            elif unit == "4":
                return
            else:
                temp = tempval

            state, infotext, perfdata = check_temperature(temp, params)
            if descr:
                infotext += " (%s)" % descr
            yield state, infotext, perfdata

            if sensorstatus != '1':
                if sensorstatus in ['2', '3', '4', '5']:
                    state = 2
                else:
                    state = 3

                yield state, "Status is %s" % status_text.get(sensorstatus, "UNKNOWN")


check_info['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", [# sensors index (1-2)
            "1.1",
            # sensor name string
            "1.2",
            # unit state: 0=Invalid, 1=Normal, 2=OutOfRangeLo, 3=OutOfRangeHi, 4=AlarmLo, 5=AlarmHi
            "1.3",
            # current value string
            "1.4",
            # sensor unit integer 0=unknown, 1=°C, 2=°F, 3=°K, 4=%
            "1.7",
   ]),
   "snmp_scan_function"       : lambda oid: "hwg" in oid(".1.3.6.1.2.1.1.1.0").lower(),
   "group"                    : "room_temperature",
   "includes"                 : [ "temperature.include" ],
}

