#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

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