#!/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):
    parsed = {}
    for name, status, conns in info:
        if name == "" or len(status) > 1:
            continue

        inst = parsed.setdefault(name, {"device_state": status})
        try:
            inst.update({"conns": int(conns)})
        except ValueError:
            pass

    return parsed


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


def check_kemp_loadmaster_services(item, _no_params, parsed):
    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'),
    }

    if item in parsed:
        data = parsed[item]
        dev_state = data["device_state"]
        state, state_readable = map_states.get(dev_state, (3, 'unknown[%s]' % dev_state))
        yield state, "Status: %s" % state_readable
        conns = data.get("conns")
        if conns is not None:
            yield 0, "Active connections: %s" % conns, [('conns', conns)]


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",
}
