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

# Example SNMP data:
# .1.3.6.1.4.1.1588.2.1.1.1.1.6.0 v4.0.1    Firmware
# .1.3.6.1.4.1.1588.2.1.1.1.1.7.0 1         Status


def inventory_brocade_vdx_status(info):
    return [(None, None)]


def check_brocade_vdx_status(_no_item, _no_params, info):
    states = {
        1: "online",
        2: "offline",
        3: "testing",
        4: "faulty",
    }
    firmware = info[0][0]
    state = saveint(info[0][1])
    message = "State: %s, Firmware: %s" % (states[state], firmware)
    if state == 1:
        return 0, message
    if state in [2, 4]:
        return 2, message
    if state == 3:
        return 1, message


def scan_brocade_vdx_status(oid):
    return (
        oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588")
        # Extreme SLX9640; SLX Operating System Version 20.1.X:
        or oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.1916.2.306"  #
    ) and oid(".1.3.6.1.4.1.1588.2.1.1.1.1.6.0") is not None


check_info["brocade_vdx_status"] = {
    "check_function": check_brocade_vdx_status,
    "inventory_function": inventory_brocade_vdx_status,
    "service_description": "Status",
    # It does not seem to work to exclude several OIDs here, there seem
    # to be too many devices which do not have the needed OIDs. We try
    # another approach: check for existance of the first needed OID
    # not oid('.1.3.6.1.2.1.1.2.0').startswith( ".1.3.6.1.4.1.1588.2.1.1.1"),
    "snmp_scan_function": scan_brocade_vdx_status,
    "snmp_info": (".1.3.6.1.4.1.1588.2.1.1.1.1", [6, 7]),  # Firmware  # Status
}
