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

innovaphone_channels_default_levels = (75.0, 80.0)


def inventory_innovaphone_channels(info):
    return [
        (x[0], "innovaphone_channels_default_levels") for x in info if x[1] == "Up" and x[2] == "Up"
    ]


def check_innovaphone_channels(item, params, info):
    for line in info:
        if line[0] == item:
            link, physical = line[1:3]
            if link != "Up" or physical != "Up":
                return 2, "Link: %s, Physical: %s" % (link, physical)
            idle, total = map(float, line[3:])
            perc_used = (idle / total) * 100  # fixed: true-division
            perc_free = 100 - perc_used
            message = "(used: %.0f, free: %.0f, total: %.0f)" % (total - idle, idle, total)
            return check_innovaphone(params, [[None, perc_free]], "%", message)
    return 3, "No Channel information found"


check_info["innovaphone_channels"] = {
    "check_function": check_innovaphone_channels,
    "inventory_function": inventory_innovaphone_channels,
    "service_description": "Channel %s",
    "has_perfdata": True,
    "group": "hw_single_channelserature",
}
