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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.ibm_tape_library import *  # pylint: disable=wildcard-import,unused-wildcard-import

# .1.3.6.1.4.1.14851.3.1.11.2.1.4.1 Logical_Library: 1 --> SNIA-SML-MIB::changerDevice-ElementName.1
# .1.3.6.1.4.1.14851.3.1.11.2.1.4.2 Logical_Library: LTO6 --> SNIA-SML-MIB::changerDevice-ElementName.2
# .1.3.6.1.4.1.14851.3.1.11.2.1.8.1 3 --> SNIA-SML-MIB::changerDevice-Availability.1
# .1.3.6.1.4.1.14851.3.1.11.2.1.8.2 3 --> SNIA-SML-MIB::changerDevice-Availability.2
# .1.3.6.1.4.1.14851.3.1.11.2.1.9.1 2 --> SNIA-SML-MIB::changerDevice-OperationalStatus.1
# .1.3.6.1.4.1.14851.3.1.11.2.1.9.2 2 --> SNIA-SML-MIB::changerDevice-OperationalStatus.2


def inventory_ibm_tl_changer_devices(info):
    return [(name.replace("Logical_Library:", "").strip(), None) for name, _avail, _status in info]


def check_ibm_tl_changer_devices(item, params, info):
    for name, avail, status in info:
        if item == name.replace("Logical_Library:", "").strip():
            return ibm_tape_library_get_device_state(avail, status)


check_info["ibm_tl_changer_devices"] = {
    "inventory_function": inventory_ibm_tl_changer_devices,
    "check_function": check_ibm_tl_changer_devices,
    "service_description": "Changer device %s",
    "snmp_info": (
        ".1.3.6.1.4.1.14851.3.1.11.2.1",
        [
            "4",  # SNIA-SML-MIB::changerDevice-ElementName
            "8",  # SNIA-SML-MIB::changerDevice-Availability
            "9",  # SNIA-SML-MIB::changerDevice-OperationalStatus
        ],
    ),
    "snmp_scan_function": scan_ibm_tape_library,
}
