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


# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.2.1 System --> IDRAC-MIB::virtualDiskName.1
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.2.2 Oracle --> IDRAC-MIB::virtualDiskName.2
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.2.3 Backup --> IDRAC-MIB::virtualDiskName.3
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.4.1 2 --> IDRAC-MIB::virtualDiskState.1
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.4.2 2 --> IDRAC-MIB::virtualDiskState.2
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.4.3 2 --> IDRAC-MIB::virtualDiskState.3
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.20.1 3 --> IDRAC-MIB::virtualDiskComponentStatus.1
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.20.2 3 --> IDRAC-MIB::virtualDiskComponentStatus.2
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.20.3 3 --> IDRAC-MIB::virtualDiskComponentStatus.3
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.34.1 1 --> IDRAC-MIB::virtualDiskRemainingRedundancy.1
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.34.2 1 --> IDRAC-MIB::virtualDiskRemainingRedundancy.2
# .1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1.34.3 1 --> IDRAC-MIB::virtualDiskRemainingRedundancy.3


def inventory_dell_idrac_virtdisks(info):
    return [ (line[0], None) for line in info ]


def check_dell_idrac_virtdisks(item, _no_params, info):
    map_states = {
        "disk" : {
            "1" : (1, "unknown"),
            "2" : (0, "online"),
            "3" : (2, "failed"),
            "4" : (2, "degraded"),
        },
        "component" : {
            "1" : (0, "other"),
            "2" : (1, "unknown"),
            "3" : (0, "OK"),
            "4" : (1, "non-critical"),
            "5" : (2, "critical"),
            "6" : (2, "non-recoverable"),
        },
    }
    for name, disk_state, component_state, redundancy in info:
        if item == name:
            for what, what_key in [
                (disk_state, "Disk"),
                (component_state, "Component")]:
                state, state_readable = map_states[what_key.lower()][what]
                yield state, "%s status: %s" % (what_key, state_readable)

            yield 0, "Remaining redundancy: %s physical disk(s)" % redundancy


check_info["dell_idrac_virtdisks"] = {
    "check_function"        : check_dell_idrac_virtdisks,
    "inventory_function"    : inventory_dell_idrac_virtdisks,
    "service_description"   : "Virtual Disk %s",
    "snmp_scan_function"    : lambda oid: oid('.1.3.6.1.2.1.1.2.0') == ".1.3.6.1.4.1.674.10892.5",
    "snmp_info"             : (".1.3.6.1.4.1.674.10892.5.5.1.20.140.1.1", [
                                    "2",    # virtualDiskName
                                    "4",    # virtualDiskState
                                    "20",   # virtualComponentStatus
                                    "34",   # virtualDiskRemainingRedundancy
                              ]),
}

