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

factory_settings["emc_isilon_fan_default_levels"] = {"lower": (3000, 2500)}


# Examples for sensor names:
# Chassis Fan1 (ISI F1) --> Chassis 1
# Chassis Fan2 (ISI F2)
# Chassis Fan3 (ISI F3)
# Power Supply 1 Fan1 --> Power Supply 1 1
# Power Supply 2 Fan1
def isilon_fan_item_name(sensor_name):
    return sensor_name.replace("Fan", "").split("(")[0].strip()


def inventory_emc_isilon_fans(info):
    for fan_name, _value in info:
        yield isilon_fan_item_name(fan_name), {}


def check_emc_isilon_fans(item, params, info):
    for fan_name, value in info:
        if item == isilon_fan_item_name(fan_name):
            return check_fan(float(value), params)


check_info["emc_isilon_fans"] = {
    "check_function": check_emc_isilon_fans,
    "inventory_function": inventory_emc_isilon_fans,
    "service_description": "Fan %s",
    "snmp_info": (
        ".1.3.6.1.4.1.12124.2.53.1",
        [
            3,  # ISILON-MIB::fanDescription,
            4,  # ISILON-MIB::fanSpeed,
        ],
    ),
    "snmp_scan_function": lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "has_perfdata": True,
    "group": "hw_fans",
    "default_levels_variable": "emc_isilon_fan_default_levels",
}
