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


def inventory_cisco_ace_rserver(info):
    for name, ip, descr, admin_status, oper_status, conns in info:
        if name != '':
            item = name
        elif descr != '':
            item = descr
        else:
            item = socket.inet_ntoa(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, descr, admin_status, oper_status, conns in info:
        ip_addr = socket.inet_ntoa(ip)
	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
                              "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",
}
