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

# Diese OIDs liefern nicht die LOAD, wie man annehmen könnte, sondern die
# UTILIZATION, da ausschließlich die Auslastung der CPU berücksichtigt wird.
# .1.3.6.1.4.1.272.4.17.4.1.1.15.1.0 5 --> BIANCA-BRICK-MIBRES-MIB::CpuLoadUser60s.1.0
# .1.3.6.1.4.1.272.4.17.4.1.1.16.1.0 1 --> BIANCA-BRICK-MIBRES-MIB::CpuLoadSystem60s.1.0
# .1.3.6.1.4.1.272.4.17.4.1.1.17.1.0 9 --> BIANCA-BRICK-MIBRES-MIB::CpuLoadStreams60s.1.0


def inventory_bintec_cpu(info):
    if info:
        return [(None, {})]


def check_bintec_cpu(_no_item, params, info):
    user = float(info[0][0])
    system = float(info[0][1])
    streams = float(info[0][2])
    util = user + system + streams

    yield 0, "user: %.1f%%" % user
    yield 0, "system: %.1f%%" % system
    yield 0, "streams: %.1f%%" % streams, [("streams", streams)]

    for res in check_cpu_util(util, params):
        yield res


# Migration NOTE: Create a separate section, but a common check plugin for
# tplink_cpu, hr_cpu, cisco_nexus_cpu, bintec_cpu, winperf_processor,
# lxc_container_cpu, docker_container_cpu.
# Migration via cmk/update_config.py!
check_info["bintec_cpu"] = {
    "inventory_function": inventory_bintec_cpu,
    "check_function": check_bintec_cpu,
    "service_description": "CPU utilization",
    "snmp_info": (
        ".1.3.6.1.4.1.272.4.17.4.1.1",
        [
            "15",  # cpuLoadUser60s
            "16",  # cpuLoadSystem60s
            "17",  # cpuLoadStreams60s
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.272.4."),
    "has_perfdata": True,
    "group": "cpu_utilization_os",
}
