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


def bintec_sensors_scan(oid):
    return oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.272.4")


bintec_sensors_info = (
    ".1.3.6.1.4.1.272.4.17.7.1.1.1",
    [
        2,  # sensorNumber
        3,  # sensorDescr
        4,  # sensorType
        5,  # sensorValue
        7,  # sensorMeasurementUnit
    ],
)

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

factory_settings["bintec_sensors_fan_default_levels"] = {
    "lower": (2000, 1000),
}


def inventory_bintec_sensors_fan(info):
    inventory = []
    for _sensor_id, sensor_descr, sensor_type, _sensor_value, _sensor_unit in info:
        if sensor_type == "2":
            inventory.append((sensor_descr, {}))
    return inventory


def check_bintec_sensors_fan(item, params, info):
    for _sensor_id, sensor_descr, _sensor_type, sensor_value, _sensor_unit in info:
        if sensor_descr == item:
            return check_fan(int(sensor_value), params)


check_info["bintec_sensors.fan"] = {
    "check_function": check_bintec_sensors_fan,
    "inventory_function": inventory_bintec_sensors_fan,
    "service_description": "%s",
    "snmp_info": bintec_sensors_info,
    "snmp_scan_function": bintec_sensors_scan,
    "has_perfdata": True,
    "default_levels_variable": "bintec_sensors_fan_default_levels",
    "group": "hw_fans",
}

# .
#   .--temp----------------------------------------------------------------.
#   |                       _                                              |
#   |                      | |_ ___ _ __ ___  _ __                         |
#   |                      | __/ _ \ '_ ` _ \| '_ \                        |
#   |                      | ||  __/ | | | | | |_) |                       |
#   |                       \__\___|_| |_| |_| .__/                        |
#   |                                        |_|                           |
#   '----------------------------------------------------------------------'

factory_settings["bintec_sensors_temp_default_levels"] = {"levels": (35, 40)}


def inventory_bintec_sensors_temp(info):
    for _sensor_id, sensor_descr, sensor_type, _sensor_value, _sensor_unit in info:
        if sensor_type == "1":
            yield sensor_descr, {}


def check_bintec_sensors_temp(item, params, info):
    for _sensor_id, sensor_descr, _sensor_type, sensor_value, _sensor_unit in info:
        if sensor_descr == item:
            return check_temperature(int(sensor_value), params, "bintec_sensors_%s" % item)

    return 3, "Sensor not found in SNMP data"


check_info["bintec_sensors.temp"] = {
    "check_function": check_bintec_sensors_temp,
    "inventory_function": inventory_bintec_sensors_temp,
    "service_description": "Temperature %s",
    "group": "temperature",
    "has_perfdata": True,
    "snmp_info": bintec_sensors_info,
    "snmp_scan_function": bintec_sensors_scan,
    "default_levels_variable": "bintec_sensors_temp_default_levels",
}

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


def inventory_bintec_sensors_voltage(info):
    inventory = []
    for _sensor_id, sensor_descr, sensor_type, _sensor_value, _sensor_unit in info:
        if sensor_type == "3":
            inventory.append((sensor_descr, None))
    return inventory


def check_bintec_sensors_voltage(item, _no_params, info):
    for _sensor_id, sensor_descr, _sensor_type, sensor_value, _sensor_unit in info:
        if sensor_descr == item:
            sensor_value = int(sensor_value) / 1000.0

            message = "%s is at %s V" % (sensor_descr, sensor_value)
            perfdata = [("voltage", str(sensor_value) + "V")]

            return 0, message, perfdata

    return 3, "Sensor %s not found" % item


check_info["bintec_sensors.voltage"] = {
    "check_function": check_bintec_sensors_voltage,
    "inventory_function": inventory_bintec_sensors_voltage,
    "service_description": "Voltage %s",
    "has_perfdata": True,
    "snmp_info": bintec_sensors_info,
    "snmp_scan_function": bintec_sensors_scan,
}

# .
