#!/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.4.1.3375.2.1.1.2.1.8.0 594 --> F5-BIGIP-SYSTEM-MIB::sysStatClientCurConns.0
# .1.3.6.1.4.1.3375.2.1.1.2.9.2.0 --> F5-BIGIP-SYSTEM-MIB::sysClientsslStatCurConns.0


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):
    for val, what, perfkey, title in [
        (info[0][0], "conns",     "connections",     "Connections"),
        (info[0][1], "ssl_conns", "connections_ssl", "SSL connections")]:

        state    = 0
        infotext = "%s:" % title
        perfdata = []

        # SSL may not be configured, eg. on test servers
        if val == "":
            infotext += " not configured"

        else:
            warn, crit = params.get(what)
            perfdata.append((perfkey, val, warn, crit))

            state, extrainfo, extraperf = check_levels(int(val), what, params.get(what))
            infotext += " %s%s" % (val, extrainfo)

            if len(extraperf) > 0:
                perfdata.append(extraperf[0])

        yield state, infotext, perfdata


check_info["f5_bigip_conns"] = {
    'check_function'            : check_f5_bigip_conns,
    'inventory_function'        : inventory_f5_bigip_conns,
    'service_description'       : 'Open Connections',
    'snmp_info'                 : ( '.1.3.6.1.4.1.3375.2.1.1.2', [
                                        '1.8',  # F5-BIGIP-SYSTEM-MIB::sysStatServerCurConns
                                        '9.2',  # F5-BIGIP-SYSTEM-MIB::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(),
    'has_perfdata'              : True,
    'group'                     : 'f5_connections',
    'default_levels_variable'   : 'f5_bigip_conns_default_levels',
}
