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

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',
    'has_perfdata'        : False,
    'snmp_info'           : [
        ('.1.3.6.1.4.1.2.6.210.1',     [1,           # ibm3100IdDisplayName
                                        3,           # ibm3100AgentVendor
                                        4]),         # 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_monitoring_state(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):
    def worst_status(*args):
        order = [0,1,3,2]
        return sorted(args, key=lambda x: order[x], reverse=True)[0]

    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',
}

