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

# FIXME
# The WATO group 'memory_simple' needs an item and the service_description should
# have a '%s'.  At the moment the current item 'total'/'TMM' and 'Memory' without '%s'
# works but is not consistent.  This will be fixed in the future.
# If we change this we loose history and parameter sets have to be adapted.

# Example output:
# Overall memory
# .1.3.6.1.4.1.3375.2.1.7.1.1.0 8396496896 sysHostMemoryTotal
# .1.3.6.1.4.1.3375.2.1.7.1.2.0 1331092416 sysHostMemoryUsed
#
# TMM (Traffic Management Module) memory
# .1.3.6.1.4.1.3375.2.1.1.2.1.143 0 sysStatMemoryTotalKb
# .1.3.6.1.4.1.3375.2.1.1.2.1.144 0 sysStatMemoryUsedKb

factory_settings["f5_bigip_mem_default_levels"] = {"levels": ("perc_used", (80.0, 90.0))}


def parse_f5_bigip_mem(info):
    parsed = {}
    try:
        parsed["total"] = (float(info[0][0]), float(info[0][1]))
    except ValueError:
        pass

    try:
        parsed["TMM"] = (float(info[0][2]) * 1024, float(info[0][3]) * 1024)
    except ValueError:
        pass

    return parsed


def discover_f5_bigip_mem(parsed):
    if "total" in parsed:
        yield "total", {}


def check_f5_bigip_mem(_item, params, parsed):
    if "total" not in parsed:
        return

    mem_total, mem_used = parsed["total"]
    return check_memory_element(
        "Usage",
        mem_used,
        mem_total,
        params.get("levels"),
        metric_name="mem_used",
    )


check_info["f5_bigip_mem"] = {
    "parse_function": parse_f5_bigip_mem,
    "inventory_function": discover_f5_bigip_mem,
    "check_function": check_f5_bigip_mem,
    "service_description": "Memory",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.3375.2.1",
        [
            "7.1.1",  # F5-BIGIP-SYSTEM-MIB::sysHostMemoryTotal
            "7.1.2",  # F5-BIGIP-SYSTEM-MIB::sysHostMemoryUsed
            "1.2.1.143",  # F5-BIGIP-SYSTEM-MIB::sysStatMemoryTotalKb
            "1.2.1.144",  # F5-BIGIP-SYSTEM-MIB::sysStatMemoryUsedKb
        ],
    ),
    "snmp_scan_function": lambda oid: ".1.3.6.1.4.1.3375" in oid(".1.3.6.1.2.1.1.2.0"),
    "default_levels_variable": "f5_bigip_mem_default_levels",
    "group": "memory_simple",
}


def discover_f5_bigip_mem_tmm(parsed):
    if parsed.get("TMM", (0, 0))[0] > 0:
        yield "TMM", {}


def check_f5_bigip_mem_tmm(_item, params, parsed):
    if "TMM" not in parsed:
        return

    mem_total, mem_used = parsed["TMM"]
    return check_memory_element(
        "Usage",
        mem_used,
        mem_total,
        params.get("levels"),
        metric_name="mem_used",
    )


check_info["f5_bigip_mem.tmm"] = {
    "inventory_function": discover_f5_bigip_mem_tmm,
    "check_function": check_f5_bigip_mem_tmm,
    "service_description": "Memory",
    "has_perfdata": True,
    "default_levels_variable": "f5_bigip_mem_default_levels",
    "group": "memory_simple",
}
