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


# Parse an InetAddress type object as defined in the SNMP-FRAMEWORK-MIB
def parse_framework_mib_inet_address(ip_address_type, ip_address):
    if ip_address_type == 1:
        return "%d.%d.%d.%d" % tuple(ip_address)
    elif ip_address_type == 2:
        return "%x:%x:%x:%x:%x:%x:%x:%x" % tuple(ip_address)
    elif ip_address_type == 3:
        return "%d.%d.%d.%d%%%d%d%d%d" % tuple(ip_address)
    elif ip_address_type == 4:
        return "%x:%x:%x:%x:%x:%x:%x:%x%%%d%d%d%d" % tuple(ip_address)
    elif ip_address_type == 5:  # Means DNS name - Reconvert to ASCII string
        return "".join([chr(x) for x in ip_address])
    elif ip_address_type == 0:  # Unknown address type - represent as hex string
        return "".join(["%x" % byte for byte in ip_address])


def inventory_cisco_ace_rserver(info):
    for name, ip_address_type, ip_address, descr, _admin_status, _oper_status, _conns in info:
        ip = parse_framework_mib_inet_address(int(ip_address_type), ip_address)
        if name != '':
            item = name
        elif descr != '':
            item = descr
        else:
            item = ip
        yield item, None


def check_cisco_ace_rserver(item, _no_params, info):
    admin_stati = {
        "1": "in service",
        "2": "out of service",
        "3": "in service, standby",
    }
    oper_stati = {
        "1": (2, "out of service"),
        "2": (0, "in service"),
        "3": (2, "failed"),
        "4": (2, "ready to test"),
        "5": (2, "testing"),
        "6": (2, "max connection reached, throttling"),
        "7": (2, "max clients reached, throttling"),
        "8": (2, "dfp throttle"),
        "9": (2, "probe failed"),
        "10": (1, "probe testing"),
        "11": (2, "oper wait"),
        "12": (2, "test wait"),
        "13": (2, "inband probe failed"),
        "14": (2, "return code failed"),
        "15": (2, "arp failed"),
        "16": (1, "standby"),
        "17": (2, "inactive"),
        "18": (2, "max load reached"),
    }

    for name, ip_address_type, ip_address, descr, admin_status, oper_status, conns in info:
        ip_addr = parse_framework_mib_inet_address(ip_address_type, ip_address)
        if name == item or ip_addr == item or descr == item:
            admin_state = admin_stati[admin_status]
            state, state_txt = oper_stati[oper_status]
            if admin_status == "2" and state == 2:
                state = 1  # max state is WARN if real server out of service
            infotext = "Operational State: %s, Administrative State: %s, Current Connections: %s" % \
                                (state_txt, admin_state, conns)
            perfdata = [("connections", int(conns))]

            return state, infotext, perfdata


check_info['cisco_ace_rserver'] = {
    "inventory_function": inventory_cisco_ace_rserver,
    "check_function": check_cisco_ace_rserver,
    "service_description": "ACE RServer %s",
    "snmp_info": (
        ".1.3.6.1.4.1.9.9.470.1.1.1.1",
        [  # cesRserverEntry
            "1",  # cesRserverName
            "3",  # cesRserverIpAddressType
            BINARY("4"),  # cesRserverIpAddress
            "5",  # cesRserverDescription
            "12",  # cesRserverAdminStatus
            "13",  # cesRserverOperStatus
            "19",  # cesRserverCurrConns
        ]),
    "has_perfdata": True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.9.1.824",
}
