#!/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.2.4.4.3.1.1.  index for ifname
# .1.3.6.1.4.1.3375.2.1.2.4.1.2.1.17. index for ifstate
# .1.3.6.1.4.1.3375.2.1.2.4.4.3.1.3.  index for IN bytes
# .1.3.6.1.4.1.3375.2.1.2.4.4.3.1.5.  index for OUT bytes

f5_bigip_interface_states = {
    1 : "down (has no link and is initialized)",
    2 : "disabled (has been forced down)",
    3 : "uninitialized (has not been initialized)",
    4 : "loopback (in loopback mode)",
    5 : "unpopulated (interface not physically populated)",
}


def check_f5_bigip_interfaces(item, params, info):
    for port, ifstate, inbytes, outbytes in info:
        if item != port:
            continue

        if int(ifstate) != 0:
            return (2, "State of %s is %s" %
                    (f5_bigip_interface_states.get(ifstate, "unhandled (%d)" % ifstate), port))

        this_time = int(time.time())
        in_per_sec  = get_rate("f5_interface.in.%s" % item,  this_time, saveint(inbytes))
        out_per_sec  = get_rate("f5_interface.out.%s" % item,  this_time, saveint(outbytes))

        inbytes_h = get_bytes_human_readable(in_per_sec)
        outbytes_h = get_bytes_human_readable(out_per_sec)
        perf = [
            ("bytes_in", in_per_sec),
            ("bytes_out", out_per_sec),
        ]
        return (0, "in bytes: %s/s, out bytes: %s/s" % (inbytes_h, outbytes_h), perf)
    return 3, "Interface not found in SNMP data"


check_info["f5_bigip_interfaces"] = {
    "check_function"        : check_f5_bigip_interfaces,
    "inventory_function"    : lambda info: [ (x[0], {'state': 0 } ) for x in info if int(x[1]) == 0],
    "service_description"   : "f5 Interface %s",
    "has_perfdata"          : True,
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0") in \
                                [ ".1.3.6.1.4.1.3375.2.1.3.4.10", ".1.3.6.1.4.1.3375.2.1.3.4.20" ],
    "snmp_info"             : ( ".1.3.6.1.4.1.3375.2.1.2.4", ["4.3.1.1", "1.2.1.17", "4.3.1.3", "4.3.1.5"]),
}
