#!/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.dell_om 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
factory_settings["dell_om_sensors_default_levels"] = {"levels": (50, 60)}


def dell_om_sensors_item(name):
    return name.replace("Temp", "").strip()


def inventory_dell_om_sensors(info):
    for line in info:
        if line[3]:
            yield dell_om_sensors_item(line[3]), {}


def check_dell_om_sensors(item, params, info):
    sensor_states = {
        1: "other",
        2: "unknown",
        10: "failed",
    }
    for idx, sensor_state, reading, location_name, dev_crit, dev_warn, dev_warn_lower, dev_crit_lower in info:
        if item == idx or dell_om_sensors_item(location_name) == item:
            sensor_state = int(sensor_state)
            if sensor_state in [1, 2, 10]:
                return 2, "Sensor is: " + sensor_states[sensor_state]

            temp = int(reading) / 10.0

            dev_warn, dev_crit, dev_warn_lower, dev_crit_lower = \
               [float(v)/10 if v else None for v in [ dev_warn, dev_crit, dev_warn_lower, dev_crit_lower ]]
            if not dev_warn_lower:
                dev_warn_lower = dev_crit_lower
            if not dev_warn:
                dev_warn = dev_crit

            return check_temperature(temp,
                                     params,
                                     "dell_om_sensors_%s" % item,
                                     dev_levels=(dev_warn, dev_crit),
                                     dev_levels_lower=(dev_warn_lower, dev_crit_lower))


check_info["dell_om_sensors"] = {
    "check_function": check_dell_om_sensors,
    "inventory_function": inventory_dell_om_sensors,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    # There is no other way to find out that openmanage is present.
    "snmp_scan_function": scan_dell_om,
    "snmp_info": (
        ".1.3.6.1.4.1.674.10892.1.700.20.1",
        [
            '2',  # ProbeIndex
            '5',  # ProbeStatus
            '6',  # ProbeReading
            #'7', # ProbeType
            '8',  # ProbeLocationName
            '10',  # ProbeUpperCriticalThreshold',
            '11',  # ProbeUpperNonCriticalThreshold',
            '12',  # ProbeLowerNonCriticalThreshold',
            '13',  # ProbeLowerCriticalThreshold',
            #'16.1', # ProbeDiscreteReading',
        ]),
    "default_levels_variable": "dell_om_sensors_default_levels"
}
