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

factory_settings["cisco_stack"] = {
    "waiting": 0,
    "progressing": 0,
    "added": 0,
    "ready": 0,
    "sdmMismatch": 1,
    "verMismatch": 1,
    "featureMismatch": 1,
    "newMasterInit": 0,
    "provisioned": 0,
    "invalid": 2,
    "removed": 2,
}


def parse_cisco_stack(info):

    switch_state_names = {
        "1": "waiting",
        "2": "progressing",
        "3": "added",
        "4": "ready",
        "5": "sdmMismatch",
        "6": "verMismatch",
        "7": "featureMismatch",
        "8": "newMasterInit",
        "9": "provisioned",
        "10": "invalid",
        "11": "removed",
    }

    switch_role_names = {
        "1": "master",
        "2": "member",
        "3": "notMember",
    }

    parsed = {}
    for line in info:
        parsed[line[0]] = {
            "switch_role": switch_role_names.get(line[1], "unknown"),
            "switch_state": switch_state_names.get(line[2], "unknown"),
        }
    return parsed


def inventory_cisco_stack(parsed):
    for item in parsed:
        yield item, {}


def check_cisco_stack(item, params, parsed):

    switch_state_descriptions = {
        "waiting": "Waiting for other switches to come online",
        "progressing": "Master election or mismatch checks in progress",
        "added": "Added to stack",
        "ready": "Ready",
        "sdmMismatch": "SDM template mismatch",
        "verMismatch": "OS version mismatch",
        "featureMismatch": "Configured feature mismatch",
        "newMasterInit": "Waiting for new master initialization",
        "provisioned": "Not an active member of the stack",
        "invalid": "State machine in invalid state",
        "removed": "Removed from stack",
    }

    data = parsed.get(item)
    if data is None:
        return

    switch_state = data["switch_state"]
    switch_role = data["switch_role"]

    status = params.get(data["switch_state"], 3)
    infotext = "Switch state: %s (%s), switch role: %s" % (switch_state_descriptions.get(
        switch_state, "Unknown"), switch_state, switch_role)
    return status, infotext


check_info['cisco_stack'] = {
    'default_levels_variable': 'cisco_stack',
    'parse_function': parse_cisco_stack,
    'inventory_function': inventory_cisco_stack,
    'check_function': check_cisco_stack,
    'service_description': 'Switch stack status %s',
    'snmp_info': (
        '.1.3.6.1.4.1.9.9.500.1.2.1.1',
        [
            "1",  # cswSwitchNumCurrent
            "3",  # cswSwitchRole
            "6",  # cswSwitchState
        ]),
    'snmp_scan_function': lambda oid: (oid(".1.3.6.1.2.1.1.2.0").startswith(
        ".1.3.6.1.4.1.9.1.1208") or oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.9.1.1745")),
    'group': 'cisco_stack',
}
