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


# .1.3.6.1.4.1.12196.13.1.1.13.1 Exchange HTTPS
# .1.3.6.1.4.1.12196.13.1.1.13.3 Exchange MAPI
# .1.3.6.1.4.1.12196.13.1.1.13.4 Exchange SMTP
# .1.3.6.1.4.1.12196.13.1.1.13.5 Lync Internal WebSvc HTTP
# .1.3.6.1.4.1.12196.13.1.1.13.6 Lync Internal WebSvc HTTPS
# .1.3.6.1.4.1.12196.13.1.1.14.1 1
# .1.3.6.1.4.1.12196.13.1.1.14.3 1
# .1.3.6.1.4.1.12196.13.1.1.14.4 1
# .1.3.6.1.4.1.12196.13.1.1.14.5 1
# .1.3.6.1.4.1.12196.13.1.1.14.6 1
# .1.3.6.1.4.1.12196.13.1.1.21.1 882
# .1.3.6.1.4.1.12196.13.1.1.21.3 6386
# .1.3.6.1.4.1.12196.13.1.1.21.4 3
# .1.3.6.1.4.1.12196.13.1.1.21.5 1
# .1.3.6.1.4.1.12196.13.1.1.21.6 1

# Some devices provide strange, wrong or incomplete data
# and it's not possible to exclude them via std. OIDs
# .1.3.6.1.4.1.12196.13.1.1.13.1 lbwebinterface --> B100-MIB::vSname.1
# .1.3.6.1.4.1.12196.13.1.1.13.2 --> B100-MIB::vSname.2
# .1.3.6.1.4.1.12196.13.1.1.13.3 --> B100-MIB::vSname.3
# .1.3.6.1.4.1.12196.13.1.1.14.1 LB_WI --> B100-MIB::vSstate.1
# .1.3.6.1.4.1.12196.13.1.1.14.2 --> B100-MIB::vSstate.2
# .1.3.6.1.4.1.12196.13.1.1.14.3 CAG_LB --> B100-MIB::vSstate.3


kemp_loadmaster_service_default_levels = ( 1500, 2000 )


def parse_kemp_loadmaster_services(info):
    map_states = {
        "1" : (0, 'in service'),
        "2" : (2, 'out of service'),
        "3" : (2, 'failed'),
        "4" : (3, 'disabled'),
        "5" : (1, 'sorry'),
        "6" : (0, 'redirect'),
        "7" : (2, 'error message'),
    }

    parsed = {}
    for name, status, conns in info:
        if not (name == "" or len(status) > 1):
            parsed.setdefault(name, {"state" : map_states[status]})
            if conns.isdigit():
                parsed[name].update({"conns" : int(conns)})

    return parsed


def inventory_kemp_loadmaster_services(parsed):
    for item, iteminfo in parsed.items():
        if iteminfo["state"] not in ["4", ""]:
            yield item, kemp_loadmaster_service_default_levels


def check_kemp_loadmaster_services(item, _no_params, parsed):
    if item in parsed:
        data = parsed[item]
        state, state_readable = data["state"]
        infotext = "State: %s" % state_readable

        perfdata = []
        if data.get("conns"):
            infotext += ", Active connections: %s" % data["conns"]
            perfdata.append( ('conns', data["conns"]) )

        return state, infotext, perfdata


check_info["kemp_loadmaster_services"] = {
    "parse_function"        : parse_kemp_loadmaster_services,
    "inventory_function"    : inventory_kemp_loadmaster_services,
    "check_function"        : check_kemp_loadmaster_services,
    "service_description"   : "Service %s",
    "has_perfdata"          : True,
    "snmp_info"             : ( ".1.3.6.1.4.1.12196.13.1.1", [
                                    "13", # B100-MIB::vSname
                                    "14", # B100-MIB::vSstate
                                    "21", # B100-MIB::conns
                              ]),
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.12196.250.10" or \
                                          oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.2021.250.10",
}
