#!/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_ibm_storage_ts(info):
    return [(None, None)]


def check_ibm_storage_ts(_no_item, _no_params, info):
    product, vendor, version = info[0][0]
    return 0, "%s %s, Version %s" % (vendor, product, version)


check_info["ibm_storage_ts"] = {
    "check_function": check_ibm_storage_ts,
    "inventory_function": inventory_ibm_storage_ts,
    "service_description": "Info",
    "snmp_info": [
        (
            ".1.3.6.1.4.1.2.6.210.1",
            [1, 3, 4],  # ibm3100IdDisplayName  # ibm3100AgentVendor
        ),  # ibm3100IdAgentVersion
        (".1.3.6.1.4.1.2.6.210.2", [1]),  # ibm3100Status
        (
            ".1.3.6.1.4.1.2.6.210.3.1.1",
            [
                1,  # libraryEntryId
                2,  # libraryState
                10,  # librarySerNum
                11,  # libraryDrvCnt
                22,  # libraryFaultFSC
                23,  # libraryFaultSev
                24,
            ],
        ),  # libraryFaultDescr
        (
            ".1.3.6.1.4.1.2.6.210.3.2.1",
            [
                1,  # driveEntryId
                10,  # driveSerNum
                15,  # driveSoftWrtErrors
                16,  # driveHardWrtErrors
                17,  # driveSoftReadErrors
                18,
            ],
        ),  # driveHardReadErrors
    ],
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.2.6.210",
}


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


ibm_storage_ts_status_name_map = {
    "1": "other",
    "2": "unknown",
    "3": "Ok",
    "4": "non-critical",
    "5": "critical",
    "6": "non-Recoverable",
}

ibm_storage_ts_status_nagios_map = {"1": 1, "2": 1, "3": 0, "4": 1, "5": 2, "6": 2}

ibm_storage_ts_fault_nagios_map = {
    "0": 0,  # no fault (undocumented)
    "1": 0,  # informational
    "2": 1,  # minor
    "3": 2,  # major
    "4": 2,  # critical
}


def check_ibm_storage_ts_status(_no_item, _no_params, info):
    (status,) = info[1][0]

    return (
        ibm_storage_ts_status_nagios_map[status],
        "Device Status: %s" % ibm_storage_ts_status_name_map[status],
    )


check_info["ibm_storage_ts.status"] = {
    "check_function": check_ibm_storage_ts_status,
    "inventory_function": inventory_ibm_storage_ts_status,
    "service_description": "Status",
}


def inventory_ibm_storage_ts_library(info):
    for entry, _status, _serial, _count, _fault, _severity, _descr in info[2]:
        yield entry, None


def check_ibm_storage_ts_library(item, _no_params, info):
    def worst_status(*args):
        order = [0, 1, 3, 2]
        return sorted(args, key=lambda x: order[x], reverse=True)[0]

    for entry, status, serial, count, fault, severity, descr in info[2]:
        if item == entry:
            dev_status = ibm_storage_ts_status_nagios_map[status]
            fault_status = ibm_storage_ts_fault_nagios_map[severity]
            # I have the suspicion that these status are dependent in the device anyway
            # but who knows?
            infotext = "Device %s, Status: %s, Drives: %s" % (
                serial,
                ibm_storage_ts_status_name_map[status],
                count,
            )
            if fault != "0":
                infotext += ", Fault: %s (%s)" % (descr, fault)
            return worst_status(dev_status, fault_status), infotext


check_info["ibm_storage_ts.library"] = {
    "check_function": check_ibm_storage_ts_library,
    "inventory_function": inventory_ibm_storage_ts_library,
    "service_description": "Library %s",
}


def inventory_ibm_storage_ts_drive(info):
    for entry, _serial, _write_warn, _write_err, _read_warn, _read_err in info[3]:
        yield entry, None


def check_ibm_storage_ts_drive(item, params, info):
    for line in info[3]:
        if item == line[0]:
            serial = line[1]
            write_warn, write_err, read_warn, read_err = map(int, line[2:])
            yield 0, "S/N: %s" % serial
            if write_err > 0:
                yield 2, "%d hard write errors" % write_err
            if write_warn > 0:
                yield 1, "%d recovered write errors" % write_warn
            if read_err > 0:
                yield 2, "%d hard read errors" % read_err
            if read_warn > 0:
                yield 1, "%d recovered read errors" % read_warn


check_info["ibm_storage_ts.drive"] = {
    "check_function": check_ibm_storage_ts_drive,
    "inventory_function": inventory_ibm_storage_ts_drive,
    "service_description": "Drive %s",
}
