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

# -- Statistics
# .1.3.6.1.4.1.1230.2.7.2.1.2.0 200 --> MCAFEE-MWG-MIB::stMalwareDetected.0
# .1.3.6.1.4.1.1230.2.7.2.1.5.0 4394370 --> MCAFEE-MWG-MIB::stConnectionsBlocked.0


def parse_mcaffee_webgateway(info):
    parsed = []
    for index, key, label in (
        (0, "infections", "Infections"),
        (1, "connections_blocked", "Connections blocked"),
    ):
        try:
            parsed.append((key, int(info[0][index]), label))
        except (IndexError, ValueError):
            pass
    return parsed


def check_mcafee_webgateway(_no_item, params, parsed):
    now = time.time()
    for key, value, label in parsed:
        rate = get_rate("check_mcafee_webgateway.%s" % key, now, value)
        yield check_levels(
            rate,
            "%s_rate" % key,
            params.get(key),
            human_readable_func=lambda f: "%.1f/s" % f,
            infoname=label,
        )


check_info["mcafee_webgateway"] = {
    "parse_function": parse_mcaffee_webgateway,
    "inventory_function": inventory_mcafee_gateway_generic,
    "check_function": check_mcafee_webgateway,
    "service_description": "Web gateway statistics",
    "snmp_info": (
        ".1.3.6.1.4.1.1230.2.7.2.1",
        [
            "2",  # MCAFEE-MWG-MIB::stMalwareDetected
            "5",  # MCAFEE-MWG-MIB::stConnectionsBlocked
        ],
    ),
    "snmp_scan_function": scan_mcafee_webgateway,
    "has_perfdata": True,
    "group": "mcafee_web_gateway",
}
