#!/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 inventory_isilon_temp(info, is_cpu):
    for sensor_name, _value in info:
        item_name = isilon_temp_item_name(sensor_name)
        if is_cpu == item_name.startswith("CPU"):
            yield item_name, {}


def check_isilon_temp(item, params, info):
    for sensor_name, value in info:
        if item == isilon_temp_item_name(sensor_name):
            return check_temperature(float(value), params, "isilon_%s" % item)


# Expected sensor names:
# "Temp Until CPU Throttle (CPU 0)"
# "Temp Until CPU Throttle (CPU 1)"
# "Temp Chassis 1 (ISI T1)"
# "Temp Front Panel"
# "Temp Power Supply 1"
# "Temp Power Supply 2"
# "Temp System"
def isilon_temp_item_name(sensor_name):
    if "CPU Throttle" in sensor_name:
        return sensor_name.split("(")[1].split(")")[0]  # "CPU 1"
    return sensor_name[5:]  # "Front Panel"


#   .--Air Temperature-----------------------------------------------------.
#   |                              _    _                                  |
#   |                             / \  (_)_ __                             |
#   |                            / _ \ | | '__|                            |
#   |                           / ___ \| | |                               |
#   |                          /_/   \_\_|_|                               |
#   |                                                                      |
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

factory_settings["emc_isilon_temp_default_levels"] = {
    "levels": (28, 33),  # assumed useful levels for ambient / air temperature
}

check_info["emc_isilon_temp"] = {
    "inventory_function": lambda info: inventory_isilon_temp(info, is_cpu=False),
    "check_function": check_isilon_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "snmp_info": (
        ".1.3.6.1.4.1.12124.2.54.1",
        [
            "3",  # ISILON-MIB::tempSensorDescription
            "4",  # ISILON-MIB::tempSensorValue
        ],
    ),
    "snmp_scan_function": lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "default_levels_variable": "emc_isilon_temp_default_levels",
}

# .
#   .--CPU Temperature-----------------------------------------------------.
#   |                           ____ ____  _   _                           |
#   |                          / ___|  _ \| | | |                          |
#   |                         | |   | |_) | | | |                          |
#   |                         | |___|  __/| |_| |                          |
#   |                          \____|_|    \___/                           |
#   |                                                                      |
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

factory_settings["emc_isilon_temp_cpu_default_levels"] = {
    "levels": (75, 85),  # assumed useful levels for ambient / air temperature
}

check_info["emc_isilon_temp.cpu"] = {
    "inventory_function": lambda info: inventory_isilon_temp(info, is_cpu=True),
    "check_function": check_isilon_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "default_levels_variable": "emc_isilon_temp_cpu_default_levels",
}
