#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             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.


# .1.3.6.1.4.1.18248.20.1.2.1.1.1.1 0
# .1.3.6.1.4.1.18248.20.1.2.1.1.1.2 0
# .1.3.6.1.4.1.18248.20.1.2.1.1.1.3 0
# .1.3.6.1.4.1.18248.20.1.2.1.1.2.1 249
# .1.3.6.1.4.1.18248.20.1.2.1.1.2.2 317
# .1.3.6.1.4.1.18248.20.1.2.1.1.2.3 69
# .1.3.6.1.4.1.18248.20.1.2.1.1.3.1 0
# .1.3.6.1.4.1.18248.20.1.2.1.1.3.2 3
# .1.3.6.1.4.1.18248.20.1.2.1.1.3.3 0


def parse_papouch_th2e_sensors(info):
    map_sensor_type = {
        "1" : "temp",
        "2" : "humidity",
        "3" : "dewpoint",
    }

    map_units = {
        "0" : "c",
        "1" : "f",
        "2" : "k",
        "3" : "percent",
    }

    map_states = {
        "0" : (0, "OK"),
        "1" : (3, "not available"),
        "2" : (1, "over-flow"),
        "3" : (1, "under-flow"),
        "4" : (2, "error"),
    }

    parsed = {}
    for oidend, state, reading_str, unit in info:
        if state != "3":
            sensor_ty   = map_sensor_type[oidend]
            sensor_unit = map_units[unit]
            parsed.setdefault(sensor_ty, {})
            parsed[sensor_ty].setdefault("Sensor %s" % oidend,
                (map_states[state], float(reading_str)/10, sensor_unit))

    return parsed


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


def inventory_papouch_th2e_sensors_temp(parsed, what):
    for item in parsed[what]:
        yield item, {}


def check_papouch_th2e_sensors_temp(item, params, parsed, what):
    if item in parsed[what]:
        (state, state_readable), reading, unit = parsed[what][item]
        return check_temperature(reading, params,
                    "papouch_th2e_sensors_%s_%s" % (what, item),
                    dev_unit=unit, dev_status=state, dev_status_name=state_readable)


check_info['papouch_th2e_sensors'] = {
    'parse_function'            : parse_papouch_th2e_sensors,
    'inventory_function'        : lambda parsed: \
        inventory_papouch_th2e_sensors_temp(parsed, "temp"),
    'check_function'            : lambda item, params, parsed: \
        check_papouch_th2e_sensors_temp(item, params, parsed, "temp"),
    'service_description'       : 'Temperature %s',
    'has_perfdata'              : True,
    'snmp_info'                 : (".1.3.6.1.4.1.18248.20.1.2.1.1", [
                                        OID_END,
                                        "1",        # the_v01-MIB::inChStatus
                                        "2",        # the_v01-MIB::inChValue
                                        "3",        # the_v01-MIB::inChUnits
                                  ]),
    'snmp_scan_function'        : lambda oid: "th2e" in oid(".1.3.6.1.2.1.1.1.0").lower() and \
                                         oid(".1.3.6.1.2.1.1.2.0").startswith(".0.10.43.6.1.4.1"),
    'includes'                  : [ 'temperature.include' ],
    'group'                     : 'temperature',
}


#.
#   .--dew point-----------------------------------------------------------.
#   |                _                             _       _               |
#   |             __| | _____      __  _ __   ___ (_)_ __ | |_             |
#   |            / _` |/ _ \ \ /\ / / | '_ \ / _ \| | '_ \| __|            |
#   |           | (_| |  __/\ V  V /  | |_) | (_) | | | | | |_             |
#   |            \__,_|\___| \_/\_/   | .__/ \___/|_|_| |_|\__|            |
#   |                                 |_|                                  |
#   '----------------------------------------------------------------------'


check_info['papouch_th2e_sensors.dewpoint'] = {
    'inventory_function'        : lambda parsed: \
        inventory_papouch_th2e_sensors_temp(parsed, "dewpoint"),
    'check_function'            : lambda item, params, parsed: \
        check_papouch_th2e_sensors_temp(item, params, parsed, "dewpoint"),
    'service_description'       : 'Dew point %s',
    'has_perfdata'              : True,
    'includes'                  : [ 'temperature.include' ],
    'group'                     : 'temperature',
}


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


# Suggested by customer
papouch_th2e_sensors_humidity_default_levels = (8, 12, 30, 35)


def inventory_papouch_th2e_sensors_humidity(parsed):
    for item in parsed["humidity"]:
        yield item, "papouch_th2e_sensors_humidity_default_levels"


def check_papouch_th2e_sensors_humidity(item, params, parsed):
    if item in parsed["humidity"]:
        (state, state_readable), reading, unit = parsed["humidity"][item]
        yield state, "Status: %s" % state_readable
        yield check_humidity(reading, params)


check_info['papouch_th2e_sensors.humidity'] = {
    'inventory_function'        : inventory_papouch_th2e_sensors_humidity,
    'check_function'            : check_papouch_th2e_sensors_humidity,
    'service_description'       : 'Humidity %s',
    'has_perfdata'              : True,
    'includes'                  : [ 'humidity.include' ],
    'group'                     : 'humidity',
}
