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


def inventory_tplink_mem(info):
    if len(info) >= 1:
        return [(None, {})]


def check_tplink_mem(_no_item, params, info):
    num_units = 0
    mem_used = 0
    for line in info:
        unit_used = int(line[0])
        mem_used += unit_used
        num_units += 1

    if num_units == 0:
        return

    mem_used = float(mem_used) / num_units

    return check_levels(
        mem_used,
        "mem_used_percent",
        params.get("levels", (None, None)),
        infoname="Usage",
        human_readable_func=get_percent_human_readable,
    )


check_info["tplink_mem"] = {
    "check_function": check_tplink_mem,
    "inventory_function": inventory_tplink_mem,
    "service_description": "Memory",
    "has_perfdata": True,
    "snmp_info": (".1.3.6.1.4.1.11863.6.4.1.2.1.1", [2]),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.11863.5"),
    "group": "memory_percentage_used",
}
