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

# Unfortunalty we can not use the normal interface names here, because
# the interface IDs from the enterprise MIBs and RFC are not the same.
# We decided using the interface description for inventory (best practise)

def inventory_stormshield_packets(info):
    for index, descrip, name, iftype, pktaccepted, pktblocked, pkticmp, tcp, udp in info:
        if iftype.lower() in [ 'ethernet', 'ipsec' ]:
            yield descrip, {}


def check_stormshield_packets(item, params, info):
    for index, descrip, name, iftype, pktaccepted, pktblocked, pkticmp, tcp, udp in info:
        if item == descrip:
            now = time.time()
            rate_pktaccepted = get_rate('acc_%s'%item, now, int(pktaccepted))
            rate_pktblocked = get_rate('block_%s'%item, now, int(pktblocked))
            rate_pkticmp = get_rate('icmp_%s'%item, now, int(pkticmp))
            infotext = '[%s], tcp: %s, udp: %s' % (name, tcp, udp)
            perfdata = [
                ( 'tcp_active_sessions', tcp ),
                ( 'udp_active_sessions', udp ),
                ( 'packages_accepted', rate_pktaccepted ),
                ( 'packages_blocked', rate_pktblocked ),
                ( 'packages_icmp_total', rate_pkticmp )
                ]
            yield 0, infotext, perfdata


check_info['stormshield_packets'] = {
    'inventory_function'        : inventory_stormshield_packets,
    'check_function'            : check_stormshield_packets,
    'default_levels_variable'   : 'stormshield_packets_default_levels',
    'service_description'       : 'Packet Stats %s',
    'has_perfdata'              : True,
    'snmp_info'                 : ('.1.3.6.1.4.1.11256.1.4.1.1', [
                                  '1',    # ID
                                  '2',    # Description
                                  '3',    # Name
                                  '6',    # IfType
                                  '11',   # PktAccepted
                                  '12',   # PktBlocked
                                  '16',   # PktIcmp
                                  '23',   # TcpConnCount
                                  '24'    # UdpConnCount
                                  ] ),
    'snmp_scan_function'        : stormshield_scan_function,
    'includes'                  : [ 'stormshield.include' ],
}
