#!/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.
"""Cisco Prime access point check
see https://solutionpartner.cisco.com/media/prime-infrastructure/api-reference/
      szier-m8-106.cisco.com/webacs/api/v1/data/AccessPointscc3b.html
"""
import collections

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

factory_settings["cisco_prime_wifi_access_points"] = {
    "levels": (20, 40),
}


def check_cisco_prime_wifi_access_points(item, params, parsed):
    """Sum up all individual counts for each connection type (as well as their sums
    indicated by 'count')"""
    counts = collections.Counter(k["status"] for k in parsed.values())
    count_total, count_critical = len(parsed), counts["CRITICAL"]
    critical_percent = 100.0 * count_critical / count_total
    yield check_levels(
        critical_percent,
        "ap_devices_percent_unhealthy",
        params.get("levels", (None, None)),
        human_readable_func=get_percent_human_readable,
        infoname="Percent Critical",
    )
    for k, v in counts.items():
        yield 0, "%s: %r" % (k.title(), v), [("ap_devices_%s" % k.lower(), v)]


check_info["cisco_prime_wifi_access_points"] = {
    "parse_function": lambda info: parse_cisco_prime("accessPointsDTO", info),
    "inventory_function": discover_single,
    "check_function": check_cisco_prime_wifi_access_points,
    "default_levels_variable": "cisco_prime_wifi_access_points",
    "service_description": "Cisco Prime WiFi Access Points",
    "group": "cisco_prime_wifi_access_points",
    "has_perfdata": True,
}
