#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# +------------------------------------------------------------------+
#
# This file is an addon for Check_MK.
# The official homepage for this check is at http://bitbucket.org/darkfader
#
# 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.


# hddEnclosure01Slots:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.1.1 = 1
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.1.20 = 20
# hddEnclosure01Desc:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.2.1 = "SLOT 01"
# hddEnclosure01Name:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.3.20 = "WDC WD30EFRX-68AX9N0                    "
# hddEnclosure01Serial:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.4.20 = "1234567            "
# hddEnclosure01FirmVer:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.5.20 = "CC1H    "
# hddEnclosure01Capacity:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.6.1 = 0
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.6.20 = 3000600
# hddEnclosure01Type:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.7.1 = 0
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.7.20 = 1
#  hddEnclosure01State:
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.8.1 = "Empty Slot"
#.1.3.6.1.4.1.18928.1.2.3.2.4.1.8.20 = "RaidSet Member"

# How to grok:
#.1.3.6.1.4.1.18928.1.2.3.E.4.1.F.D
#                         |     | |
#                         |     | +---enc disk id
#                         |     +-----field
#                         +-------enclosure



def inventory_areca_hba_pdisks(info):
    inventory = []
    for enclosure_entry in info:
        for slot_entry in enclosure_entry:
            if len(slot_entry) == 8 and slot_entry[-1].split()[0] != "Empty":
                enc, slot = slot_entry[0].split(".")
                diskname = ("%d/%02d" % (int(enc), int(slot)))
                inventory.append((diskname, None))
    return inventory

def check_areca_hba_pdisks(item, _no_params, info):
    for enclosure_entry in info:
        for slot_entry in enclosure_entry:
            enc, slot = slot_entry[0].split(".")
            diskname = ("%d/%02d" % (int(enc), int(slot)))

            # We could do smarter by tracking the serial at inventory.
            # I decided to not get too smart, and so we're not following a disk 
            # around if you put it somewhere else.
            if diskname == item:

                disk_descr = "(%s %s)" % (slot_entry[2], slot_entry[3])

                # What I'm checking here is a field that SEEMS to be the right one.
                # Contact me if this seems wrong. Someone needs to test / adjust it 
                # with some JBOD mode array and pull some disks.
                if slot_entry[7] == "Failed": 
                    return (2, "CRIT - Disk is failed. %s" % disk_descr)
                else:
                    return (0, "OK - Disk is OK. %s" % disk_descr)

    return (3, "Disk not found in agent output")


check_info["areca_hba_pdisks"]  = {
    "check_function"      : check_areca_hba_pdisks,
    "inventory_function"  : inventory_areca_hba_pdisks,
    "has_perfdata"        : False,
    "service_description" : "PDisk Enc/Sl %s",
    # Find Areca SAS MIB
    "snmp_scan_function"  : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.18928.1"),
    "snmp_info"           : [(".1.3.6.1.4.1.18928.1.2.3", 
        # unclear: how does it look in jbod mode?
        # where does i get pony?
        # There's up to 8 enclosures, "1" is hardcoded having 8 slots.
        # probably it's the ext. SAS connector. 
                                  [ "1", "2", "3", "4", "5", "6", "7", "8" ], 
        # Below each enclosure there's the following structure for disk data
                                  [ "4.1.1", # The slot ids
                                    "4.1.2", # The slot descrs
                                    "4.1.3", # The disk model
                                    "4.1.4", # The disk fw
                                    "4.1.5", # The disk size
                       # The MIB seems wrong about the next ones
                                    "4.1.6", #
                                    "4.1.7", # 
                                    "4.1.8", # Textual disk state
                                  ]
                            )],
}

