#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             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.

# .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.4.3.1.13. index for ifstate
# .1.3.6.1.4.1.3375.2.1.2.4.4.3.1.3. index for OUT bytes
# .1.3.6.1.4.1.3375.2.1.2.4.4.3.1.5. index for IN bytes

def check_f5_bigip_interfaces(item, params, info):
    for port, ifstate, inbytes, outbytes in info:
        if item == port:
            if int(ifstate) != 0:
                states = {
                 2 : "down (has no link and is initialized)",
                 3 : "disabled has been forced down)",
                 4 : "uninitialized (has not been initialized)",
                 5 : "loopback (in loopback mode)",
                 6 : "unpopulated (interface not physically populated)",
                }
                return(2, "CRIT - State of %s is %s" % (states[ifstate], port))
        this_time = int(time.time())
        in_timedif,  in_per_sec  = get_counter("f5.interface.in.%s" % item,  this_time, int(inbytes))
        out_timedif,  out_per_sec  = get_counter("f5_interface.out.%s" % item,  this_time, int(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, "OK - in bytes: %s/s, out bytes: %s/s" % (inbytes_h, outbytes_h), perf)


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: ".1.3.6.1.4.1.3375.2.1.3.4.20" in oid(".1.3.6.1.2.1.1.2.0"),
    "snmp_info"             : ( ".1.3.6.1.4.1.3375.2.1.2.4.4.3.1", [1, 13, 3, 5]),
}
