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

def inventory_dell_om_disks(info):
    return [ ("%s:%s:%s" % (x[3], x[4], x[5]) , None) for x in info ]

def check_dell_om_disks(item, _no_params, info):
    #State definitions. Found in check_openmange from Trond H. Amundsen
    spare_state = {
         1 : 'VD member',    # disk is a member of a virtual disk
         2 : 'DG member',    # disk is a member of a disk group
         3 : 'Global HS',    # disk is a global hot spare
         4 : 'Dedicated HS', # disk is a dedicated hot spare
         5 : 'no',           # not a spare
    }

    media_type = {
         1 : 'unknown',
         2 : 'HDD',
         3 : 'SSD',
    }

    bus_type = {
         1 : 'SCSI',
         2 : 'IDE',
         3 : 'Fibre Channel',
         4 : 'SSA',
         6 : 'USB',
         7 : 'SATA',
         8 : 'SAS',
    }

    pdisk_state = {
         0  : 'Unknown',
         1  : 'Ready',
         2  : 'Failed',
         3  : 'Online',
         4  : 'Offline',
         6  : 'Degraded',
         7  : 'Recovering',
         11 : 'Removed',
         13 : 'non-raid',
         15 : 'Resynching',
         22 : 'Replacing', # FIXME: this one is not defined in the OMSA MIBs
         24 : 'Rebuilding',
         25 : 'No Media',
         26 : 'Formatting',
         28 : 'Diagnostics',
         34 : 'Predictive failure',
         35 : 'Initializing',
         41 : 'Unsupported',
         53 : 'Incompatible',
         39 : 'Foreign',
         40 : 'Clear',
    }

    for name, dstate, pid, eid, cid, tid, sizeMB, btype, sstate, smart, mt  in info:
        ditem = "%s:%s:%s" % ( eid, cid, tid )
        if ditem == item:
            state = 0
            dstate = saveint(dstate)
            btype = saveint(btype)
            sstate = saveint(sstate)
            smart = saveint(smart)
            mt = saveint(mt)
            size = saveint(sizeMB)*1024*1024
            msg = ["%s (%s, %s)" % ( name, pid, get_bytes_human_readable(size) ) ]
            label = ""
            if smart == 2:
                dstate = 34
            if dstate in [ 40, 35, 34, 26, 7, 4]:
                state = 1
                label = "(!)"
            elif dstate not in [ 3, 1, 13 ]:
                state = 2
                label = "(!!)"

            # handle hot spares as OK
            if sstate in [ 3, 4 ] and dstate == 1:
                state = 0
                label = ""

            msg.append("state %s%s" % ( pdisk_state.get(dstate, 'ukn (%s)' % dstate ), label))
            msg.append("Bus Type: %s" % bus_type.get(btype,'unk (%s)' % btype) )

            if sstate != 5:
                msg.append("Spare State: %s" % spare_state.get(sstate, 'ukn (%s)' %sstate ))
            if mt != 0:
                msg.append("Media Type: %s" % media_type.get(mt,'ukn (%s)' % mt ))

            return state, ", ".join(msg)
    return 3, "Device not found in SNMP tree"

check_info["dell_om_disks"] = {
    "check_function"        : check_dell_om_disks,
    "inventory_function"    : inventory_dell_om_disks,
    "service_description"   : "Physical Disk %s",
    "has_perfdata"          : False,
    # There is no other way to find out that openmanage is present.
    "snmp_scan_function"    : scan_dell_om,
    "snmp_info"             : ( ".1.3.6.1.4.1.674.10893.1.20.130.4.1", [
                                            2, # arrayDiskName
                                            4, # arrayDiskState
                                            6, # arrayDiskProductID
                                            9, # arrayDiskEnclosureID
                                            10, # arrayDiskChannel
                                            15, # arrayDiskTargetID
                                            11, # arrayDiskLengthInMB
                                            21, # arrayDiskBusType
                                            22, # arrayDiskSpareState
                                            #24, #arrayDiskComponentStatus
                                            31, #arrayDiskSmartAlertIndication
                                            35, # arrayDiskMediaType
                              ]),
    "includes"              : [ "dell_om.include" ],
}

