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


def inventory_blade_bx_blades(info):
    for id_, status, _serial, _name in info:
        if status != "3":  # blade not present
            yield id_, None


def check_blade_bx_blades(item, _no_params, info):
    status_codes = {
        "1": (3, "unknown"),
        "2": (0, "OK"),
        "3": (3, "not present"),
        "4": (2, "error"),
        "5": (2, "critical"),
        "6": (0, "standby"),
    }

    for id_, status, serial, name in info:
        if id_ == item:
            state, state_readable = status_codes[status]
            if name:
                name_info = "[%s, Serial: %s]" % (name, serial)
            else:
                name_info = "[Serial: %s]" % serial
            return state, "%s Status: %s" % (name_info, state_readable)


check_info["blade_bx_blades"] = {
    "inventory_function": inventory_blade_bx_blades,
    "check_function": check_blade_bx_blades,
    "service_description": "Blade %s",
    "snmp_info": (
        ".1.3.6.1.4.1.7244.1.1.1.4.2.1.1",
        [
            1,  # bladeId
            2,  # bladestatus
            5,  # Serialnumber
            21,  # Name
        ],
    ),
    "snmp_scan_function": lambda oid: "BX600" in oid(".1.3.6.1.2.1.1.1.0")
    or oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.7244.1.1.1",
}
