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

# 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_mem 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.mem import *  # pylint: disable=wildcard-import,unused-wildcard-import

# example output


def parse_cisco_cpu_memory_multiitem(info):
    ph_idx_to_desc = {}
    for idx, desc in info[1]:
        if desc.lower().startswith("cpu "):
            desc = desc[4:]
        ph_idx_to_desc[idx] = desc

    parsed = {}
    for idx, used, free, reserved in info[0]:
        if used == "0" and free == "0":
            continue

        name = ph_idx_to_desc.get(idx, idx)
        try:
            parsed[name] = {
                "mem_used": float(used),
                "mem_free": float(free),
                "mem_reserved": float(reserved),
            }
        except ValueError:
            pass
    return parsed


@get_parsed_item_data
def check_cisco_cpu_memory_multiitem(_item, params, data):
    mem_used = data["mem_used"]
    mem_free = data["mem_free"]
    mem_reserved = data["mem_reserved"]
    mem_occupied = (mem_used + mem_reserved) * 1024  # convert from kilobyte to byte
    mem_total = (mem_used + mem_free) * 1024
    yield check_cisco_mem_sub(None, params, mem_occupied, mem_total)


check_info["cisco_cpu_memory"] = {
    "parse_function": parse_cisco_cpu_memory_multiitem,
    "check_function": check_cisco_cpu_memory_multiitem,
    "inventory_function": discover(),
    "group": "cisco_cpu_memory",
    "service_description": "CPU Memory utilization %s",
    "has_perfdata": True,
    "snmp_info": [
        (
            ".1.3.6.1.4.1.9.9.109.1.1.1",
            [
                "1.2",  # cpmCPUTotalPhysicalIndex
                "1.12",  # cpmCPUMemoryUsed
                "1.13",  # cpmCPUMemoryFree
                "1.14",  # cpmCPUMemoryKernelReserved
            ],
        ),
        (
            ".1.3.6.1.2.1.47.1.1.1",
            [
                OID_END,  # OID index
                "1.7",  # entPhysicalName
            ],
        ),
    ],
    "snmp_scan_function": snmp_scan_cisco_cpu_multiitem,
}
