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

# 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

# Example output from agent:
# [['1', '24', 'SLOT #0: TEMP #1'],
# ['2', '12', 'SLOT #0: TEMP #2'],
# ['3', '12', 'SLOT #0: TEMP #3'],
# ['4', '4687', 'FAN #1'],
# ['5', '4560', 'FAN #2'],
# ['6', '4821', 'FAN #3'],
# ['7', '1', 'Power Supply #1'],
# ['8', '1', 'Power Supply #2']]


def brocade_sensor_convert(info, what):
    return_list = []
    for presence, state, name in info:
        name = name.lstrip()  # remove leading spaces provided via SNMP
        if name.startswith(what) and presence != "6" and (saveint(state) > 0 or what == "Power"):
            sensor_id = name.split("#")[-1]
            return_list.append([sensor_id, name, state])
    return return_list


def brocade_scan(oid):
    return (
        oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.1.1")
        or oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.24.1.1588.2.1.1")
        or oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.2.1")
        or oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.3.3.1")
        or
        # Extreme SLX9640; SLX Operating System Version 20.1.X:
        oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.1916.2.306"
    )


brocade_info = (
    ".1.3.6.1.4.1.1588.2.1.1.1.1.22.1",
    [
        3,  # swSensorStatus, 6 = absent
        4,  # swSensorValue, -2147483648 = unknown
        5,  # swSensorInfo
    ],
)

brocade_fan_default_levels = {"lower": (3000, 2800)}


def inventory_brocade_fan(info):
    converted = brocade_sensor_convert(info, "FAN")
    return [(x[0], "brocade_fan_default_levels") for x in converted]


def check_brocade_fan(item, params, info):
    converted = brocade_sensor_convert(info, "FAN")
    if isinstance(params, tuple):  # old format
        params = {"lower": params}

    for snmp_item, _name, value in converted:
        if item == snmp_item:
            return check_fan(int(value), params)


check_info["brocade.fan"] = {
    "check_function": check_brocade_fan,
    "inventory_function": inventory_brocade_fan,
    "service_description": "FAN %s",
    "group": "hw_fans",
    "snmp_info": brocade_info,
    "snmp_scan_function": brocade_scan,
    "has_perfdata": True,
}


def inventory_brocade_power(info):
    converted = brocade_sensor_convert(info, "Power")
    return [(x[0], None) for x in converted]


def check_brocade_power(item, _no_params, info):
    converted = brocade_sensor_convert(info, "Power")
    for snmp_item, name, value in converted:
        if item == snmp_item:
            value = int(value)
            if value != 1:
                return 2, "Error on supply %s" % name
            return 0, "No problems found"

    return 3, "Supply not found"


check_info["brocade.power"] = {
    "check_function": check_brocade_power,
    "inventory_function": inventory_brocade_power,
    "service_description": "Power supply %s",
    "snmp_info": brocade_info,
    "snmp_scan_function": brocade_scan,
}

factory_settings["brocade_temp_default_levels"] = {"levels": (55.0, 60.0)}


def inventory_brocade_temp(info):
    converted = brocade_sensor_convert(info, "SLOT")
    return [(x[0], "brocade_temp_default_levels") for x in converted]


def check_brocade_temp(item, params, info):
    converted = brocade_sensor_convert(info, "SLOT")
    for snmp_item, _name, value in converted:
        if item == snmp_item:
            return check_temperature(int(value), params, "brocade_temp_%s" % item)


check_info["brocade.temp"] = {
    "check_function": check_brocade_temp,
    "inventory_function": inventory_brocade_temp,
    "service_description": "Temperature Ambient %s",
    "has_perfdata": True,
    "group": "temperature",
    "snmp_info": brocade_info,
    "snmp_scan_function": brocade_scan,
    "default_levels_variable": "brocade_temp_default_levels",
}
