#!/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-
# ails.  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_dell_idrac_disks(info):
    inventory = []
    for line in info:
        inventory.append(( line[0], None ))
    return inventory

def check_dell_idrac_disks(item, _no_params, info):
    diskStates  = {
        1 : "unkown",
        2 : "ready",
        3 : "online",
        4 : "foreign",
        5 : "offline",
        6 : "blocked",
        7 : "failed",
        8 : "non-raid",
        9 : "removed",
    }

    componentStates = {
        1 : "Other",
        2 : "Unknown",
        3 : "OK",
        4 : "Non-critical",
        5 : "Critical",
        6 : "Non-recoverable"
    }

    sparestates = {
        1 : "not a Spare",
        2 : "dedicated Hotspare",
        3 : "global Hotspare",
    }
    for diskname, diskState, capacityMB, spareState, componentState, smartAlert, displayName in info:
        if diskname == item:
            state = 0
            infotexts = []

            # Component State
            componentState = int(componentState)
            infotext = "State: " + componentStates[componentState]
            if componentState in [ 5, 6 ]:
                state = 2
                infotexts.append(infotext+"(!!)")
            elif componentState in [ 1, 2, 4 ]:
                state = max(state, 1)
                infotexts.append(infotext+"(!)")

            # Smart Alert
            if smartAlert != '0':
                infotexts.append("Smart Alert on Disk(!!)")

            # Disk State
            diskState = int(diskState)
            infotext = "Disk State: " + diskStates[diskState]
            label = ""
            show = False
            if diskState in [ 1, 5, 6, 7, 9 ]:
                state = 2
                label = "(!!)"
                show = True
            elif diskState in [ 4, 8 ]:
                state = max(state, 1)
                label = "(!)"
                show = True
            if show:
                infotexts.append(infotext+label)

            spareState = int(spareState)
            if spareState != 1:
                infotexts.append('Spare State: ' + sparestates[spareState] )

            # Capacity
            infotexts.append("Size: " + get_bytes_human_readable(int(capacityMB) * 1024 * 1024 ))

            # Display Name
            infotexts.append(displayName)

            return state, ", ".join(infotexts)
    return 3, "Disk not found in SNMP Output"

check_info["dell_idrac_disks"] = {
    "check_function"        : check_dell_idrac_disks,
    "inventory_function"    : inventory_dell_idrac_disks,
    "service_description"   : "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.130.4.1", [
                                                                            2, # physicalDiskName
                                                                            4, # physicalDiskState
                                                                            11, # physicalDiskCapacityInMB
                                                                            22, # physicalDiskSpareState
                                                                            24, # physicalDiskComponentStatus
                                                                            31, # physicalDiskSmartAlertIndication
                                                                            55, # physicalDiskDisplayName
                                                                         ]),
}

