#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.humidity import *  # pylint: disable=wildcard-import,unused-wildcard-import

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import

#   .--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",
}

# .
#   .--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",
}
