#!/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" : (1, "unknown"),
    "1" : (2, "down"),
    "2" : (1, "unknown"),
    "3" : (1, "busy"),
    "4" : (1, "out of service"),
    "5" : (1, "transition to out of service"),
    "7" : (0, "up"),
}

netscaler_vserver_types = {
    "0"  : "http",
    "1"  : "ftp",
    "2"  : "tcp",
    "3"  : "udp",
    "4"  : "ssl bridge",
    "5"  : "monitor",
    "6"  : "monitor udp",
    "7"  : "nntp",
    "8"  : "http server",
    "9"  : "http client",
    "10" : "rpc server",
    "11" : "rpc client",
    "12" : "nat",
    "13" : "any",
    "14" : "ssl",
    "15" : "dns",
    "16" : "adns",
    "17" : "snmp",
    "18" : "ha",
    "19" : "monitor ping",
    "20" : "sslOther tcp",
    "21" : "aaa",
    "23" : "secure monitor",
    "24" : "ssl vpn udp",
    "25" : "rip",
    "26" : "dns client",
    "27" : "rpc server",
    "28" : "rpc client",
    "62" : "service unknown",
    "69" : "tftp",
}

netscaler_vserver_entitytypes = {
    "0" : "unknown",
    "1" : "loadbalancing",
    "2" : "loadbalancing group",
    "3" : "ssl vpn",
    "4" : "content switching",
    "5" : "cache redirection",
}


factory_settings["netscaler_vserver_default_levels"] = {
    "health_levels" : (100.0, 0.1),
}


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


def check_netscaler_vserver(item, params, info):
    if params is None:
        params = {}

    for name, ip, port, svr_type, svr_state,\
        svr_health, svr_entitytype in info:
        if name == item:
            svr_state, svr_state_readable = \
                netscaler_vserver_states.get(svr_state, ("unknown", 1))
            yield svr_state, "Status: %s" % svr_state_readable

            if svr_entitytype in [ "1", "2" ]:
                health_perc  = float(svr_health)
                health_state = 0
                infotext     = "Health: %s" % get_percent_human_readable(health_perc)
                warn, crit   = params["health_levels"]

                if health_perc < crit:
                    health_state = 2
                elif health_perc < warn:
                    health_state = 1

                if health_state > 0:
                    infotext += " (warn/crit below %s/%s)" % \
                                (get_percent_human_readable(warn),
                                 get_percent_human_readable(crit))

                yield health_state, infotext, \
                      [("health_perc", health_perc, None, None, 0, 100)]

            yield 0, "Type: %s, Protocol: %s, Socket: %s:%s" % (
                  netscaler_vserver_entitytypes.get(svr_entitytype , "unknown (%s)" % svr_entitytype),
                  netscaler_vserver_types.get(svr_type, "service unknown (%s)" % svr_type),
                  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"),
    "group"                   : "netscaler_vserver",
    "default_levels_variable" : "netscaler_vserver_default_levels",
}
