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

factory_settings["nvidia_temp_default_levels"] = {"levels": (60, 65)}

factory_settings["nvidia_temp_core_default_levels"] = {"levels": (90, 95)}


def format_nvidia_name(identifier):
    identifier = identifier.replace("Temp", "")
    if identifier == "GPUCore":
        return "GPU NVIDIA"

    # afaik temperature sensors can be GPU or Board, maybe memory
    return "System NVIDIA %s" % identifier


def inventory_nvidia_temp(core, info):
    for line in info:
        line_san = line[0].strip(":")
        if line_san.lower().endswith("temp"):
            if core == (line_san == "GPUCoreTemp"):
                yield format_nvidia_name(line_san), {}


def check_nvidia_temp(item, params, info):
    for line in info:
        if format_nvidia_name(line[0].strip(":")) == item or item == line[0].strip(
            ":"
        ):  # compatibility code for "old discovered" services
            return check_temperature(int(line[1]), params, "nvidia_%s" % item)


check_info["nvidia.temp"] = {
    "check_function": check_nvidia_temp,
    "inventory_function": lambda info: inventory_nvidia_temp(False, info),
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "default_levels_variable": "nvidia_temp_default_levels",
}

check_info["nvidia.temp_core"] = {
    "check_function": check_nvidia_temp,
    "inventory_function": lambda info: inventory_nvidia_temp(True, info),
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "default_levels_variable": "nvidia_temp_core_default_levels",
}


def inventory_nvidia_errors(info):
    for line in info:
        if line[0] == "GPUErrors:":
            return [(None, None)]


def check_nvidia_errors(_no_item, _no_params, info):
    for line in info:
        if line[0] == "GPUErrors:":
            errors = int(line[1])
            if errors == 0:
                return (0, "No GPU errors")
            return (2, "%d GPU errors" % errors)
    return (3, "incomplete output from agent")


check_info["nvidia.errors"] = {
    "check_function": check_nvidia_errors,
    "inventory_function": inventory_nvidia_errors,
    "service_description": "NVIDIA GPU Errors",
    "group": "hw_errors",
}
