#!/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.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import


def format_emc_datadomain_temp(descr, encid, index, new_format):
    if new_format:
        return descr + " Enclosure " + encid
    return encid + "-" + index


def inventory_emc_datadomain_temps(info):
    for encid, index, descr, _reading, status in info:
        if status != "2":
            yield format_emc_datadomain_temp(descr, encid, index, True), {}


def check_emc_datadomain_temps(item, params, info):
    state_table = {
        "0": (2, "Failed"),
        "1": (0, "OK"),
        "2": (2, "Not found"),
        "3": (1, "Overheat Warning"),
        "4": (2, "Overheat Critical"),
    }
    for encid, index, descr, reading, status in info:
        name = format_emc_datadomain_temp(descr, encid, index, "Enclosure" in item)
        if item == name:
            dev_status, state_name = state_table[status]
            return check_temperature(
                float(reading),
                params,
                "emc_datadomain_temps_%s" % item,
                dev_status=int(dev_status),
                dev_status_name=state_name,
            )


check_info["emc_datadomain_temps"] = {
    "check_function": check_emc_datadomain_temps,
    "inventory_function": inventory_emc_datadomain_temps,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "snmp_info": (
        ".1.3.6.1.4.1.19746.1.1.2.1.1.1",
        [
            1,  # tempEnclosureID
            2,  # tempSensorIndex
            4,  # tempSensorDescription
            5,  # tempSensorCurrentValue
            6,  # tempSensorStatus
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Data Domain OS"),
}
