#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# .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 inventory_checkpoint_packets(info):
    if info:
        return [(None, {})]


def check_checkpoint_packets(_no_item, params, info):
    if info:
        value = {}
        value["Accepted"] = int(info[0][0][0])
        value["Rejected"] = int(info[0][0][1])
        value["Dropped"] = int(info[0][0][2])
        value["Logged"] = int(info[0][0][3])
        value["EspEncrypted"] = int(info[1][0][0])
        value["EspDecrypted"] = int(info[1][0][1])
        this_time = time.time()
        for name in value:
            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[name])
            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"] = {
    "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
            ])
    ],
    "includes": ["checkpoint.include"],
}
