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

# comNET GmbH, Fabian Binder - 2018-05-30

# .1.3.6.1.4.1.9.9.719.1.9.44.1.4  cucsComputeRackUnitMbTempStatsAmbientTemp
# .1.3.6.1.4.1.9.9.719.1.9.44.1.8  cucsComputeRackUnitMbTempStatsFrontTemp
# .1.3.6.1.4.1.9.9.719.1.9.44.1.13 cucsComputeRackUnitMbTempStatsIoh1Temp
# .1.3.6.1.4.1.9.9.719.1.9.44.1.21 cucsComputeRackUnitMbTempStatsRearTemp

factory_settings["cisco_ucs_temp_env_default_levels"] = {"levels": (30, 35)}


def parse_cisco_ucs_temp_env(info):
    new_info = {
        "Ambient": info[0][0],
        "Front": info[0][1],
        "IO-Hub": info[0][2],
        "Rear": info[0][3],
    }
    return new_info


def inventory_cisco_ucs_temp_env(info):
    for name, _temp in info.items():
        yield name, {}


def check_cisco_ucs_temp_env(item, params, info):
    for name, temp in info.items():
        if item == name:
            yield check_temperature(int(temp), params, "cisco_ucs_temp_env_%s" % name)


check_info["cisco_ucs_temp_env"] = {
    "parse_function": parse_cisco_ucs_temp_env,
    "inventory_function": inventory_cisco_ucs_temp_env,
    "check_function": check_cisco_ucs_temp_env,
    "default_levels_variable": "cisco_ucs_temp_env_default_levels",
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_scan_function": scan_cisco_ucs,
    "snmp_info": (
        ".1.3.6.1.4.1.9.9.719.1.9.44.1",
        [
            "4",  # cucsComputeRackUnitMbTempStatsAmbientTemp
            "8",  # cucsComputeRackUnitMbTempStatsFrontTemp
            "13",  # cucsComputeRackUnitMbTempStatsIoh1Temp
            "21",  # cucsComputeRackUnitMbTempStatsRearTemp
        ],
    ),
    "group": "temperature",
}
