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

# .1.3.6.1.4.1.19011.1.3.2.1.3.1.1.1.2.1 "Temperature-R" --> ISPRO-MIB::isDeviceMonitorTemperatureName
# .1.3.6.1.4.1.19011.1.3.2.1.3.1.1.1.3.1 2230 --> ISPRO-MIB::isDeviceMonitorTemperature
# .1.3.6.1.4.1.19011.1.3.2.1.3.1.1.1.4.1 3 --> ISPRO-MIB::isDeviceMonitorTemperatureAlarm
# .1.3.6.1.4.1.19011.1.3.2.1.3.2.2.1.3.1 2300 --> ISPRO-MIB::isDeviceConfigTemperatureLowWarning
# .1.3.6.1.4.1.19011.1.3.2.1.3.2.2.1.4.1 2000 --> ISPRO-MIB::isDeviceConfigTemperatureLowCritical
# .1.3.6.1.4.1.19011.1.3.2.1.3.2.2.1.5.1 3000 --> ISPRO-MIB::isDeviceConfigTemperatureHighWarning
# .1.3.6.1.4.1.19011.1.3.2.1.3.2.2.1.6.1 3800 --> ISPRO-MIB::isDeviceConfigTemperatureHighCritical


def inventory_ispro_sensors_temp(info):
    return [(line[0], {}) for line in info if line[2] not in ["1", "2"]]


def check_ispro_sensors_temp(item, params, info):
    for name, reading_str, status, warn_low, crit_low, warn, crit in info:
        if item == name:
            devstatus, devstatus_name = ispro_sensors_alarm_states(status)
            return check_temperature(
                float(reading_str) / 100.0,
                params,
                "ispro_sensors_temp_%s" % item,
                dev_levels=(float(warn) / 100.0, float(crit) / 100.0),
                dev_levels_lower=(float(warn_low) / 100.0, float(crit_low) / 100.0),
                dev_status=devstatus,
                dev_status_name=devstatus_name,
            )


check_info["ispro_sensors_temp"] = {
    "inventory_function": inventory_ispro_sensors_temp,
    "check_function": check_ispro_sensors_temp,
    "service_description": "Temperature %s",
    "snmp_info": (
        ".1.3.6.1.4.1.19011.1.3.2.1.3",
        [
            "1.1.1.2",  # ISPRO-MIB::isDeviceMonitorTemperatureName
            "1.1.1.3",  # ISPRO-MIB::isDeviceMonitorTemperature (unit is 0.01 degree)
            "1.1.1.4",  # ISPRO-MIB::isDeviceMonitorTemperatureAlarm
            "2.2.1.3",  # ISPRO-MIB::isDeviceConfigTemperatureLowWarning
            "2.2.1.4",  # ISPRO-MIB::isDeviceConfigTemperatureLowCritical
            "2.2.1.5",  # ISPRO-MIB::isDeviceConfigTemperatureHighWarning
            "2.2.1.6",  # ISPRO-MIB::isDeviceConfigTemperatureHighCritical
        ],
    ),
    "snmp_scan_function": ispro_scan_function,
    "has_perfdata": True,
    "group": "temperature",
}
