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

# .1.3.6.1.4.1.7244.1.2.1.3.4.1.1.1 1
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.1.2 2
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.2.1 3
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.2.2 2
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.3.1 "54 65 6D 70 5F 50 43 49 31 5F 4F 75 74 6C 65 74 01 "
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.3.2 Temp_CPU0_Inlet
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.4.1 41
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.4.2 37
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.6.1 85
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.6.2 75
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.7.1 80
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.7.2 70
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.8.1 -99
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.8.2 -99
# ...
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.9.25 -99
# .1.3.6.1.4.1.7244.1.2.1.3.4.1.9.26 5


@get_parsed_item_data
def check_quanta_temperature(item, params, entry):
    if entry.value in (-99, None):
        return entry.status[0], "Status: %s" % entry.status[1]

    return check_temperature(
        entry.value,
        params,
        "quanta_temperature_%s" % entry.name,
        dev_levels=entry.upper_levels,
        dev_levels_lower=entry.lower_levels,
        dev_status=entry.status[0],
        dev_status_name=entry.status[1],
    )


check_info["quanta_temperature"] = {
    "inventory_function": discover(),
    "parse_function": parse_quanta,
    "check_function": check_quanta_temperature,
    "service_description": "Temperature %s",
    "group": "temperature",
    "has_perfdata": True,
    # these is no good oid identifier for quanta devices, thats why the first oid is used here
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(
        ".1.3.6.1.4.1.8072.3.2.10"
    )
    and (oid(".1.3.6.1.4.1.7244.1.2.1.1.1.0") is not None),
    "snmp_info": [
        (
            ".1.3.6.1.4.1.7244.1.2.1.3.4.1",
            [
                1,  # temperatureDeviceInfoIndex
                2,  # temperatureDeviceInfoStatus
                3,  # temperatureDeviceInfoLocationName
                4,  # temperatureDeviceReading
                6,  # temperatureDeviceUpperCriticalThreshold
                7,  # temperatureDeviceUpperNonCriticalThreshold
                8,  # temperatureDeviceLowerNonCriticalThreshold
                9,  # temperatureDeviceLowerCriticalThreshold
            ],
        ),
    ],
}
