#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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.

# comNET GmbH, Fabian Binder

# .1.3.6.1.4.1.9148.3.2.1.1.3 Health Score (apSysHealthScore)
# .1.3.6.1.4.1.9148.3.2.1.1.4 Health Status Description (apSysRedundancy)

factory_settings["acme_sbc_snmp_default_levels"] = {
    "levels_lower": (99, 75),
}


def inventory_acme_sbc_snmp(info):
    yield None, {}


def check_acme_sbc_snmp(_no_item, params, info):
    map_states = {
        "0": (3, "unknown"),
        "1": (1, "initial"),
        "2": (0, "active"),
        "3": (0, "standby"),
        "4": (2, "out of service"),
        "5": (2, "unassigned"),
        "6": (1, "active (pending)"),
        "7": (1, "standby (pending)"),
        "8": (1, "out of service (pending)"),
        "9": (1, "recovery"),
    }

    try:
        score, state = info[0]
    except (IndexError, ValueError):
        return
    health_state, health_state_readable = map_states.get(state, (3, "unknown"))
    yield health_state, "Health state: %s" % (health_state_readable)

    try:
        score = int(score)
    except ValueError:
        yield 3, "Unknown score: %s" % score
        return
    warn, crit = params.get("levels_lower", (None, None))
    levels_msg = " (warn/crit at or below %s%%/%s%%)" % (warn, crit)
    score_msg = "Score: %s%%" % score
    if crit is not None and score <= crit:
        yield 2, score_msg + levels_msg
    elif warn is not None and score <= warn:
        yield 1, score_msg + levels_msg
    else:
        yield 0, score_msg


check_info['acme_sbc_snmp'] = {
    'inventory_function': inventory_acme_sbc_snmp,
    'check_function': check_acme_sbc_snmp,
    'service_description': 'ACME SBC health',
    'snmp_info': (
        '.1.3.6.1.4.1.9148.3.2.1.1',
        [
            "3",  # APSYSMGMT-MIB::apSysHealthScore
            "4",  # APSYSMGMT-MIB::apSysRedundancy
        ]),
    'snmp_scan_function': scan_acme,
    'includes': ['acme.include'],
    'group': 'acme_sbc_snmp',
    'default_levels_variable': 'acme_sbc_snmp_default_levels',
}
