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

aironet_default_error_levels = (1.0, 10.0)  # per second


def inventory_aironet_errors(info):
    yield from ((line[0], "aironet_default_error_levels") for line in info)


def check_aironet_errors(item, params, info):
    for line in info:
        if line[0] == item:
            value = int(line[1])
            this_time = time.time()
            persec = get_rate("aironet_errors.%s" % item, this_time, value)
            warn, crit = params
            perfdata = [("errors", persec, None, None, warn, crit)]
            infotxt = "%.2f errors/s" % persec
            return (
                2 if persec >= crit else 1 if persec >= warn else 0,  #  #
                infotxt,
                perfdata,
            )


check_info["aironet_errors"] = {
    "check_function": check_aironet_errors,
    "inventory_function": inventory_aironet_errors,
    "service_description": "MAC CRC errors radio %s",
    "has_perfdata": True,
    # CISCO-DOT11-IF-MIB::cd11IfRecFrameMacCrcErrors
    "snmp_info": (".1.3.6.1.4.1.9.9.272.1.2.1.1.1", [0, 2]),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0")
    in [
        ".1.3.6.1.4.1.9.1.525",
        ".1.3.6.1.4.1.9.1.618",
        ".1.3.6.1.4.1.9.1.685",
        ".1.3.6.1.4.1.9.1.758",
        ".1.3.6.1.4.1.9.1.1034",
        ".1.3.6.1.4.1.9.1.1247",
    ],
}
