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

# this is currently here only to prevent error messages when upgrading
factory_settings["adva_fsp_temp_default_levels"] = {}


def inventory_adva_fsp_temp(info):
    for line in info:
        # Ignore unconnected sensors
        if len(line) == 5 and line[0] != "" and line[4] != "" and int(line[0]) >= -2730:
            yield line[4], {}


def check_adva_fsp_temp(item, params, info):
    for line in info:
        if len(line) == 5 and line[4] == item:
            temp, high, low, _descr = line[0:4]
            temp = float(temp) / 10
            high = float(high) / 10
            low = float(low) / 10

            if temp <= -2730:
                return 3, "Invalid sensor data"

            if low > -273:
                return check_temperature(
                    temp,
                    params,
                    "adva_fsp_temp_%s" % item,
                    dev_levels=(high, high),
                    dev_levels_lower=(low, low),
                )

            return check_temperature(
                temp, params, "adva_fsp_temp_%s" % item, dev_levels=(high, high)
            )


check_info["adva_fsp_temp"] = {
    "inventory_function": inventory_adva_fsp_temp,
    "check_function": check_adva_fsp_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.2544",
        [
            "1.11.2.4.2.1.1.1",  # moduleDiagnosticsTemp
            "1.11.2.4.2.1.1.2",  # moduleDiagnosticsUpperTempThres
            "1.11.2.4.2.1.1.3",  # moduleDiagnosticsLowerTempThres
            "2.5.5.1.1.1",
            "2.5.5.2.1.5",
            # "2.5.5.1.1.10",
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0") == "Fiber Service Platform F7",
    "group": "temperature",
    "default_levels_variable": "adva_fsp_temp_default_levels",
}
