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

DEVICE_TYPE_MAP = {
    "1": "Power",
    "2": "Cooling",
    "3": "Control",
    "4": "Connectivity",
    "5": "Robotics",
    "6": "Media",
    "7": "Drive",
    "8": "Operator action request",
}

RAS_STATUS_MAP = {
    "1": (0, "good"),
    "2": (2, "failed"),
    "3": (2, "degraded"),
    "4": (1, "warning"),
    "5": (0, "informational"),
    "6": (3, "unknown"),
    "7": (3, "invalid"),
}

OPNEED_STATUS_MAP = {
    "0": (0, "no"),
    "1": (2, "yes"),
    "2": (0, "no"),
}


def parse_quantum_libsmall_status(info):
    parsed = []
    for line in info:
        for oidend, dev_state in line:
            dev_type = DEVICE_TYPE_MAP.get(oidend.split('.')[0])
            if not (dev_type or dev_state):
                continue
            parsed.append((dev_type, dev_state))
    return parsed


def inventory_quantum_libsmall_status(parsed):
    if parsed:
        return [(None, None)]


def check_quantum_libsmall_status(_no_item, _no_params, parsed):
    for dev_type, dev_state in parsed:
        if dev_type == "Operator action request":
            state, state_readable = OPNEED_STATUS_MAP.get(dev_state, (3, 'unknown[%s]' % dev_state))
        else:
            state, state_readable = RAS_STATUS_MAP.get(dev_state, (3, 'unknown[%s]' % dev_state))
        yield state, "%s: %s" % (dev_type, state_readable)


check_info['quantum_libsmall_status'] = {
    "parse_function"          : parse_quantum_libsmall_status,
    "check_function"          : check_quantum_libsmall_status,
    "inventory_function"      : inventory_quantum_libsmall_status,
    "service_description"     : "Tape library status",
    "snmp_info"               : [(".1.3.6.1.4.1.3697.1.10.10.1.15", [
                                    OID_END,
                                    "10",
                                 ]),
                                 (".1.3.6.1.4.1.3764.1.10.10", [
                                    OID_END,
                                    "12",
                                ])],
    "snmp_scan_function"      : lambda oid: "linux" in oid(".1.3.6.1.2.1.1.1.0", "").lower() \
                                      and "library" in oid(".1.3.6.1.2.1.1.6.0", "").lower(),
}
