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

# oid(".1.3.6.1.4.1.9.9.109.1.1.1.1.5.1") is depreceated by
# oid(".1.3.6.1.4.1.9.9.109.1.1.1.1.8.1"), we recognize both for now

cisco_cpu_default_levels = (80.0, 90.0)


def inventory_cisco_cpu(info):
    if info and (info[0][0].isdigit() or info[0][1].isdigit()):
        return [(None, "cisco_cpu_default_levels")]


def check_cisco_cpu(item, params, info):
    # Value of info could be (None, None) or ("", "").
    if not info[0][0].isdigit() and not info[0][1].isdigit():
        return 3, "No information about the CPU utilization available"

    if info[0][1]:
        util = float(info[0][1])
    else:
        util = float(info[0][0])

    warn, crit = params.get("util", (None, None)) if isinstance(params, dict) else params

    return check_levels(
        util,
        "util",
        (warn, crit),
        human_readable_func=get_percent_human_readable,
        boundaries=(0, 100),
        infoname="Utilization in the last 5 minutes",
    )


check_info["cisco_cpu"] = {
    "check_function": check_cisco_cpu,
    "inventory_function": inventory_cisco_cpu,
    "service_description": "CPU utilization",
    "has_perfdata": True,
    "group": "cpu_utilization",
    "snmp_info": (".1.3.6.1.4.1.9.9.109.1.1.1.1", [5, 8]),
    "snmp_scan_function": snmp_scan_cisco_cpu,
}
