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

# .1.3.6.1.2.1.1.1.0 Linux gateway1 2.6.18-92cp #1 SMP Tue Dec 4 21:44:22 IST 2012 i686
# .1.3.6.1.4.1.2620.1.1.4.0 131645
# .1.3.6.1.4.1.2620.1.1.5.0 0
# .1.3.6.1.4.1.2620.1.1.6.0 1495
# .1.3.6.1.4.1.2620.1.1.7.0 16297

factory_settings["checkpoint_packets_default_levels"] = {
    "accepted": (100000, 200000),
    "rejected": (100000, 200000),
    "dropped": (100000, 200000),
    "logged": (100000, 200000),
    "espencrypted": (100000, 200000),
    "espdecrypted": (100000, 200000),
}


def parse_checkpoint_packets(info):
    parsed = {}
    for key, main_index, sub_index in [
        ("Accepted", 0, 0),
        ("Rejected", 0, 1),
        ("Dropped", 0, 2),
        ("Logged", 0, 3),
        ("EspEncrypted", 1, 0),
        ("EspDecrypted", 1, 1),
    ]:
        try:
            parsed[key] = int(info[main_index][0][sub_index])
        except (IndexError, ValueError):
            pass
    return parsed


def inventory_checkpoint_packets(parsed):
    if parsed:
        return [(None, {})]


def check_checkpoint_packets(_no_item, params, parsed):
    this_time = time.time()
    for name, value in parsed.items():
        key = name.lower()
        if params.get(key) is None:
            warn, crit = (None, None)
        else:
            warn, crit = params[key]

        rate = get_rate(key, this_time, value)
        infotext = "%s: %.1f pkts/s" % (name, rate)
        state = 0
        if crit is not None and rate >= crit:
            state = 2
        elif warn is not None and rate >= warn:
            state = 1
        if state:
            infotext += " (warn/crit at %s/%s pkts/s)" % (warn, crit)

        yield state, infotext, [(key, rate, warn, crit, 0)]


check_info["checkpoint_packets"] = {
    "parse_function": parse_checkpoint_packets,
    "check_function": check_checkpoint_packets,
    "inventory_function": inventory_checkpoint_packets,
    "service_description": "Packet Statistics",
    "has_perfdata": True,
    "group": "checkpoint_packets",
    "snmp_scan_function": scan_checkpoint,
    "default_levels_variable": "checkpoint_packets_default_levels",
    "snmp_info": [
        (
            ".1.3.6.1.4.1.2620.1.1",
            [
                4,  # fwAccepted
                5,  # fwRejected
                6,  # fwDropped
                7,  # fwLogged
            ],
        ),
        (
            ".1.3.6.1.4.1.2620.1.2.5.4",
            [
                5,  # cpvIpsecEspEncPkts
                6,  # cpvIpsecEspDecPkts
            ],
        ),
    ],
}
