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

# FIXME
# The WATO group 'memory_simple' needs an item and the service_description should
# have a '%s'.  At the moment the current empty item '' 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.

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


def inventory_datapower_mem(info):
    # TODO: Cleanup empty string and change manpage
    if info:
        return [("", {})]


def check_datapower_mem(item, params, info):

    mem_total_bytes = int(info[0][0]) * 1024
    mem_used_bytes = int(info[0][1]) * 1024

    return check_memory_element(
        "Usage", mem_used_bytes, mem_total_bytes, params.get("levels"), metric_name="mem_used"
    )


check_info["datapower_mem"] = {
    "inventory_function": inventory_datapower_mem,
    "check_function": check_datapower_mem,
    "service_description": "Memory",
    "snmp_info": (
        ".1.3.6.1.4.1.14685.3.1.5",
        [
            "2",  # dpStatusMemoryStatusTotalMemory
            "3",  # dpStatusMemoryStatusUsedMemory
        ],
    ),
    "snmp_scan_function": scan_datapower,
    "group": "memory_simple",
    "default_levels_variable": "datapower_mem_default_levels",
    "has_perfdata": True,
}
