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

netscaler_vserver_states = {
    "0": ("unknown", 1),
    "1": ("down", 2),
    "2": ("unknown", 1),
    "3": ("busy", 1),
    "4": ("out of service", 1),
    "5": ("transition to out of service", 1),
    "7": ("up", 0),
}

netscaler_vserver_types = {
    "0"    :"http",
    "1"    :"ftp",
    "2"    :"tcp",
    "3"    :"udp",
    "4"    :"sslBridge",
    "5"    :"monitor",
    "6"    :"monitorUdp",
    "7"    :"nntp",
    "8"    :"httpserver",
    "9"    :"httpclient",
    "10"   :"rpcserver",
    "11"   :"rpcclient",
    "12"   :"nat",
    "13"   :"any",
    "14"   :"ssl",
    "15"   :"dns",
    "16"   :"adns",
    "17"   :"snmp",
    "18"   :"ha",
    "19"   :"monitorPing",
    "20"   :"sslOtherTcp",
    "21"   :"aaa",
    "23"   :"secureMonitor",
    "24"   :"sslvpnUdp",
    "25"   :"rip",
    "26"   :"dnsClient",
    "27"   :"rpcServer",
    "28"   :"rpcClient",
    "62"   :"serviceUnknown",
    "69"   :"tftp",
}

netscaler_vserver_entitytypes = {
    "0"    :"unknown",
    "1"    :"loadbalancing",
    "2"    :"loadbalancinggroup",
    "3"    :"sslvpn",
    "4"    :"contentswitching",
    "5"    :"cacheredirection",
}


def inventory_netscaler_vserver(info):
    for line in info:
        if line[0]:
            yield line[0], None

def check_netscaler_vserver(item, _no_params, info):
    for line in info:
        if line[0] == item:
            state = 0
            name, ip, port, svr_type, svr_state, svr_health, svr_entitytype = line

            svr_state_info = netscaler_vserver_states.get(svr_state, ("unknown", 1))
            yield svr_state_info[1], "State: %s" % svr_state_info[0]

            if svr_entitytype in [ "1", "2" ]:
                health_perc = int(svr_health)
                health_state = 0
                if health_perc == 0:
                    health_state = 2
                elif health_perc < 100:
                    health_state = 1
                yield health_state, "Health at: %s%%" % health_perc, [("health_perc", health_perc, None, None, 0, 100)]

            yield 0, "Type: %s" % netscaler_vserver_entitytypes.get(svr_entitytype , "unknown (%s)" % svr_entitytype)
            yield 0, "Protocol: %s" % netscaler_vserver_types.get(svr_type, "serviceUnknown (%s)" % svr_type)
            yield 0, "Socket: %s:%s" % (ip, port)

check_info["netscaler_vserver"] = {
    "check_function"          : check_netscaler_vserver,
    "inventory_function"      : inventory_netscaler_vserver,
    "service_description"     : "VServer %s",
    "snmp_info"               : (".1.3.6.1.4.1.5951.4.1.3.1.1", [ # nsVserverGroup.vserverTable.vserverEntry
                                            1, # vsvrName
                                            2, # vsvrIpAddress
                                            3, # vsvrPort
                                            4, # vsvrType
                                            5, # vsvrState
                                           62, # vsvrHealth
                                           64, # vsvrEntityType
                                        ]),
    "has_perfdata"            : True,
    "snmp_scan_function"      : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.5951.1"),
}
