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


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):
    # Connection rate
    now = time.time()
    total_native_compat_rate = 0
    conns_dict = {}

    for line in info:
        if line[2] != "":
            native_conn_rate = get_rate("native", now, int(line[2]))
        else:
            native_conn_rate = 0

        if line[3] != "":
            compat_conn_rate = get_rate("compat", now, int(line[3]))
        else:
            compat_conn_rate = 0

            total_native_compat_rate += native_conn_rate + compat_conn_rate

        if line[0] != "":
            conns_dict.setdefault("total", 0)
            conns_dict["total"] += int(line[0])

        if line[1] != "":
            conns_dict.setdefault("total_ssl", 0)
            conns_dict["total_ssl"] += int(line[1])

    conn_rate_params =  params.get("connections_rate")       or (None, None)
    conn_rate_params += params.get("connections_rate_lower") or (None, None)

    # Current connections
    for val, dsname, params_values, perfkey, title in [
        (conns_dict.get("total", None),     "conns", params.get("conns"),         "connections",      "Connections"),
        (conns_dict.get("total_ssl", None), "ssl_conns", params.get("ssl_conns"), "connections_ssl",  "SSL connections"),
        (total_native_compat_rate,  "connections_rate", conn_rate_params, "connections_rate", "Connections/s")
        ]:

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

        # SSL may not be configured, eg. on test servers
        if val == None:
            infotext += " not configured"
        else:
            # The check_parameters are a bit tricky: If thresholds are disabled,
            # the value is None; otherwise it's a Tuple.
            # TODO: Refactor the check parameter f5_connections and this check plugin!
            if params_values == None:
                warn, crit = (None, None)
            else:
                warn, crit = params_values[:2]
            perfdata.append((perfkey, val, warn, crit))

            state, extrainfo, extraperf = check_levels(val, dsname, params_values)
            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::sysStatClientCurConns
                                        '9.2',  # F5-BIGIP-SYSTEM-MIB::sysClientsslStatCurConns
                                        '9.6',  # F5-BIGIP-SYSTEM-MIB::sysClientSslStatTotNativeConns
                                        '9.9',  # F5-BIGIP-SYSTEM-MIB::sysClientSslStatTotCompatConns
                                  ]),
    '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',
}
