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


#   .--Temperature---------------------------------------------------------.
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   +----------------------------------------------------------------------+

# ambient temperature levels for a datacenter
factory_settings["bluenet_sensor_temp_default_levels"] = {
        "levels"      : (28, 35),
        "levels_lower": (13, 17),
}

def inventory_bluenet_sensor_temp(info):
    for sensor_id, sensor_type, temp, hum in info:
        # temperature and combined temperature/humidity sensor
        if sensor_type in ('1', '2'):
            if sensor_id == "0":
                descr = "internal"
            else:
                descr = "external %s" % sensor_id
            yield descr, {}


def check_bluenet_sensor_temp(item, params, info):
    for sensor_id, sensor_type, temp, hum in info:
        if sensor_id == "0":
            descr = "internal"
        else:
            descr = "external %s" % sensor_id
        if descr == item:
            temperature = float(temp) / 10.0
            return check_temperature(temperature, params, "bluenet_sensor_temp_%s" % item, 'c')


check_info["bluenet_sensor"] = {
    "inventory_function"      : inventory_bluenet_sensor_temp,
    "check_function"          : check_bluenet_sensor_temp,
    "service_description"     : "Temperature %s",
    "group"                   : "temperature",
    "default_levels_variable" : "bluenet_sensor_temp_default_levels",
    "has_perfdata"            : True,
    "snmp_info"               : (".1.3.6.1.4.1.21695.1.10.7.3.1", [
                                        1, # e3lpmSensor
                                        2, # e3lpmSensorType
                                        4, # e3lpmSensorTemperatureCelsius
                                        5, # e3lpmSensorHumidity
                               ]),
    "snmp_scan_function"      : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.21695.1",
    "includes"                : ["temperature.include"],
}

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

# ambient humidity levels for a datacenter
bluenet_sensor_humidity_default_levels = ( 35, 40, 60, 65 )

def inventory_bluenet_sensor_hum(info):
    for sensor_id, sensor_type, temp, hum in info:
        # humidity for combined temperature/humidity sensor
        if sensor_type == '2':
            if sensor_id == "0":
                descr = "internal"
            else:
                descr = "external %s" % sensor_id
            yield descr, 'bluenet_sensor_humidity_default_levels'


def check_bluenet_sensor_hum(item, params, info):
    for sensor_id, sensor_type, temp, hum in info:
        if sensor_id == "0":
            descr = "internal"
        else:
            descr = "external %s" % sensor_id
        if descr == item:
            humidity = float(hum) / 10.0
            return check_humidity(humidity, params)


check_info["bluenet_sensor.hum"] = {
    "inventory_function"      : inventory_bluenet_sensor_hum,
    "check_function"          : check_bluenet_sensor_hum,
    "service_description"     : "Humidity %s",
    "has_perfdata"            : True,
    "group"                   : "humidity",
    "includes"                : ["humidity.include"],
}
