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

# Example Agent Output:
# GENUA-MIB:
# .1.3.6.1.4.1.3717.2.1.1.6.1 = INTEGER: 300000
# .1.3.6.1.4.1.3717.2.1.1.6.2 = INTEGER: 1268
# .1.3.6.1.4.1.3717.2.1.1.6.3 = INTEGER: 1

genua_pfstate_default_levels = { "used": ( None , None ) }

def inventory_genua_pfstate(info):
    # remove empty elements due to alternative enterprise id in snmp_info
    info = filter(None, info)

    if info[0]:
        if len(info[0][0]) == 3:
            return [ (None, genua_pfstate_default_levels) ]
    else:
        return []


def pfstate(st):
    names = {
		'0' : 'notOK',
		'1' : 'OK',
		'2' : 'unknown',
	    }
    return names.get(st, st)


def check_genua_pfstate(item, params, info):
    # remove empty elements due to alternative enterprise id in snmp_info
    info = filter(None, info)

    if info[0]:
        if len(info[0][0]) == 3:
            pfstateMax = saveint(info[0][0][0])
            pfstateUsed = saveint(info[0][0][1])
            pfstateStatus = info[0][0][2]
    else:
        return(3, "Invalid Output from Agent")

    warn,crit = params.get("used")
    if crit == None:
        crit = pfstateMax

    state = 0
    usedsym = ""
    statussym = ""
    if pfstateStatus != "1":
        state = 1
        statussym = "(!)"

    if crit and pfstateUsed > crit:
        state = 2
        usedsym = "(!!)"
    elif warn and pfstateUsed > warn:
        state = 1
        usedsym = "(!)"

    pfstatus = pfstate(str(pfstateStatus))
    infotext = "PF State: %s%s States used: %d%s States max: %d" \
		% (pfstatus, statussym, pfstateUsed, usedsym, pfstateMax )
    perfdata = [ ( "statesused", pfstateUsed, None, pfstateMax ) ]
    return (state, infotext, perfdata)

check_info['genua_pfstate'] = {
    "inventory_function" : inventory_genua_pfstate,
    "check_function"     : check_genua_pfstate,
    "service_description": "Paketfilter Status",
    "has_perfdata"       : True,
    "group"              : "pf_used_states",
    "snmp_info"          : [( ".1.3.6.1.4.1.3717.2.1.1.6",[
				1, # "pfstateMax"
				2, # "pfstateUsed"
				3, # "pfstateStatus"
                            ]),
                            ( ".1.3.6.1.4.1.3137.2.1.1.6",[
				1, # "pfstateMax"
				2, # "pfstateUsed"
				3, # "pfstateStatus"
                          ])],
    "snmp_scan_function" : lambda oid: "genuscreen" in oid(".1.3.6.1.2.1.1.1.0").lower()
}
