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


def parse_f5_bigip_vserver(info):
    vservers = {}
    for name, status, tot_conns, cur_conns in info:
        vservers[name] = {
            "status"    : int(status),
            "tot_conns" : int(tot_conns),
            "cur_conns" : int(cur_conns),
        }
    return vservers


def inventory_f5_bigip_vserver(parsed):
    for name, vserver in parsed.items():
        if vserver["status"] == 1:
            yield name, None


def check_f5_bigip_vserver(item, _no_params, parsed):
    if item in parsed:
        vserver = parsed[item]

        # Current number of connections
        yield 0, "Client connections: %d" % vserver["cur_conns"], [("connections", vserver["cur_conns"])]

        # New connections per time
        counter_name = "f5_bigip_vserver.%s.connections" % item
        rate = get_rate(counter_name, time.time(), vserver["tot_conns"])
        yield 0, "Rate: %.2f/sec" % rate, [("conn_rate", rate)]

        # Current server status
        status = vserver["status"]
        if status == 0:
            yield 1, "Virtual Server is disabled"
        elif status == 1:
            yield 0, "Virtual Server is up and available"
        elif status == 2:
            yield 2, "Virtual Server is currently not available"
        elif status == 3:
            yield 2, "Virtual Server is not available"
        elif status == 4:
            yield 1, "Virtual Server status is unknown"
        else:
            yield 3, "Unhandled status (%d)" % status


check_info["f5_bigip_vserver"] = {
    'parse_function'          : parse_f5_bigip_vserver,
    'check_function'          : check_f5_bigip_vserver,
    'inventory_function'      : inventory_f5_bigip_vserver,
    'service_description'     : 'Virtual Server %s',
    "has_perfdata"            : True,
    'snmp_info'               : ('.1.3.6.1.4.1.3375.2.2.10', [
                                                "13.2.1.1", # Name
                                                "13.2.1.2", # Status
                                                "2.3.1.11", # ltmVirtualServStatClientTotConns
                                                "2.3.1.12", # ltmVirtualServStatClientCurConns
                               ]),
    '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(),
}
