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

# .1.3.6.1.4.1.14851.3.1.6.2.1.2.1 3 --> SNIA-SML-MIB::mediaAccessDeviceObjectType.1
# .1.3.6.1.4.1.14851.3.1.6.2.1.2.2 3 --> SNIA-SML-MIB::mediaAccessDeviceObjectType.2
# .1.3.6.1.4.1.14851.3.1.6.2.1.3.1 IBM     ULT3580-TD6     00078B5F0F --> SNIA-SML-MIB::mediaAccessDevice-Name.1
# .1.3.6.1.4.1.14851.3.1.6.2.1.3.2 IBM     ULT3580-TD6     00078B5FCF --> SNIA-SML-MIB::mediaAccessDevice-Name.2
# .1.3.6.1.4.1.14851.3.1.6.2.1.6.1 2 --> SNIA-SML-MIB::mediaAccessDevice-NeedsCleaning.1
# .1.3.6.1.4.1.14851.3.1.6.2.1.6.2 2 --> SNIA-SML-MIB::mediaAccessDevice-NeedsCleaning.2

# .1.3.6.1.4.1.14851.3.1.12.2.1.3.1 IBM     ULT3580-TD6     00078B5F0F --> SNIA-SML-MIB::scsiProtocolController-ElementName.1
# .1.3.6.1.4.1.14851.3.1.12.2.1.3.2 IBM     ULT3580-TD6     00078B5FCF --> SNIA-SML-MIB::scsiProtocolController-ElementName.2
# .1.3.6.1.4.1.14851.3.1.12.2.1.4.1 2 --> SNIA-SML-MIB::scsiProtocolController-OperationalStatus.1
# .1.3.6.1.4.1.14851.3.1.12.2.1.4.2 2 --> SNIA-SML-MIB::scsiProtocolController-OperationalStatus.2
# .1.3.6.1.4.1.14851.3.1.12.2.1.6.1 3 --> SNIA-SML-MIB::scsiProtocolController-Availability.1
# .1.3.6.1.4.1.14851.3.1.12.2.1.6.2 3 --> SNIA-SML-MIB::scsiProtocolController-Availability.2


def parse_ibm_tl_media_access_devices(info):
    parsed = {}
    media_access_info, controller_info = info
    for ty, name, clean in media_access_info:
        parsed.setdefault(
            ibm_tape_library_parse_device_name(name), {
                "type": {
                    "0": "unknown",
                    "1": "worm drive",
                    "2": "magneto optical drive",
                    "3": "tape drive",
                    "4": "dvd drive",
                    "5": "cdrom drive",
                }[ty],
                "clean": {
                    "0": "unknown",
                    "1": "true",
                    "2": "false",
                }[clean],
            })

    for ctrl_name, ctrl_avail, ctrl_status in controller_info:
        ctrl_name = ibm_tape_library_parse_device_name(ctrl_name)
        if ctrl_name in parsed:
            parsed[ctrl_name]["ctrl_avail"] = ctrl_avail
            parsed[ctrl_name]["ctrl_status"] = ctrl_status

    return parsed


def inventory_ibm_tl_media_access_devices(parsed):
    for device in parsed:
        yield device, None


def check_ibm_tl_media_access_devices(item, params, parsed):
    if item in parsed:
        data = parsed[item]
        if data.get("ctrl_avail") and data.get("ctrl_status"):
            for res in ibm_tape_library_get_device_state(data["ctrl_avail"], data["ctrl_status"]):
                yield res
        yield 0, "Type: %s, Needs cleaning: %s" % (data["type"], data["clean"])


check_info['ibm_tl_media_access_devices'] = {
    'parse_function': parse_ibm_tl_media_access_devices,
    'inventory_function': inventory_ibm_tl_media_access_devices,
    'check_function': check_ibm_tl_media_access_devices,
    'service_description': 'Media access device %s',
    'snmp_info': [
        (
            '.1.3.6.1.4.1.14851.3.1.6.2.1',
            [
                "2",  # SNIA-SML-MIB::mediaAccessDeviceObjectType
                "3",  # SNIA-SML-MIB::mediaAccessDevice-Name
                "6",  # SNIA-SML-MIB::mediaAccessDevice-NeedsCleaning
            ]),
        (
            '.1.3.6.1.4.1.14851.3.1.12.2.1',
            [
                "3",  # SNIA-SML-MIB::scsiProtocolController-ElementName
                "6",  # SNIA-SML-MIB::scsiProtocolController-Availability
                "4",  # SNIA-SML-MIB::scsiProtocolController-OperationalStatus
            ])
    ],
    'snmp_scan_function': scan_ibm_tape_library,
    'includes': ['ibm_tape_library.include'],
}
