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


# {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 3375 bigipTrafficMgmt(2) bigipSystem(1) sysGlobals(1) sysGlobalStats(2) sysGlobalStat(1) sysStatClientCurConns(8)}

# {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 3375 bigipTrafficMgmt(2) bigipSystem(1) sysGlobals(1) sysGlobalStats(2) sysGlobalClientSslStat(9) sysClientsslStatCurConns(2)}

factory_settings["f5_bigip_conns_default_levels"] = {
        "conns"     : (25000, 30000),
        "ssl_conns" : (25000, 30000),
}

def inventory_f5_bigip_conns(info):
    if info:
        return [ ( None, {} ) ]

def check_f5_bigip_conns(item, params, info):
    type_list = { 'conns': ('Connections', 0),
                  'ssl_conns': ('SSL Connections', 1),
                }
    perfdata = []
    infotext = ""
    state = 0

    separator = ""
    for typ, values in type_list.iteritems():
        param = params.get(typ)
        desc = values[0]
        index = values[1]
        conns   = int(info[0][index])
        infotext += separator
        separator = " - "
        if type(param) == tuple:
            warn, crit = param
            perfdata.append( (typ, conns, warn, crit) )
            if conns >=crit:
                sstate = 2
                sym = "(!!)"
            elif conns >= warn:
                sstate = 1
                sym = "(!)"
            else:
                sstate = 0
                sym = ""
            infotext += "%d %s%s (%d/%d)" % (conns, desc, sym, warn, crit)
        else:
            warn, crit = None, None
            perf = ( (typ, conns, warn, crit) )
            sstate, text, extraperf = check_levels(conns, typ, param)
            if sstate == 2:
                sym == "(!!)"
            elif sstate == 1:
                sym = "(!)"
            else:
                sym = ""
            perfdata.append(perf)
            if len(extraperf) > 0:
                perfdata.append(extraperf[0])
            infotext += "%d %s%s" % (conns, desc, sym)
            if text:
                infotext += ", " + text
        if state < sstate:
            state = sstate

    return (state, infotext, perfdata)

check_info["f5_bigip_conns"] = {
    'check_function'            : check_f5_bigip_conns,
    'inventory_function'        : inventory_f5_bigip_conns,
    'service_description'       : 'Open Connections',
    'has_perfdata'              : True,
    'group'                     : 'f5_connections',
    'default_levels_variable'   : 'f5_bigip_conns_default_levels',
    'snmp_info'                 : ( '.1.3.6.1.4.1.3375.2.1.1.2', [
                                        '1.8', # sysStatServerCurConns
                                        '9.2', # sysClientsslStatCurConns
                                    ] ),
    'snmp_scan_function'        : lambda oid: '.1.3.6.1.4.1.3375.2' in \
                        oid(".1.3.6.1.2.1.1.2.0") and "big-ip" in \
                        oid(".1.3.6.1.4.1.3375.2.1.4.1.0").lower(),
}
