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


# Parse the snmp data from the IBM agent.
# must be cautius since a failed disk makes the snmp output
# shift and gives false info for "slot_id"
def parse_ibm_xraid_pdisks(info):
    data = {}
    for line in info:
       slot_id, disk_id, disk_type, disk_state, slot_desc = line
       if "slot" in slot_desc.lower():
           slot_id = int(slot_desc.split(", ")[-1][-1])
           enc_id  = int(slot_desc.split(", ")[-2][-1])
           hba_id  = int(slot_desc.split(", ")[-3][-1])
           disk_path = ("%d/%d/%d") % (hba_id, enc_id, slot_id)
           data.update({ disk_path : ( slot_id, disk_id, disk_type, disk_state, slot_desc )})
    return data


def inventory_ibm_xraid_pdisks(info):
    inventory = []
    for disk_id in parse_ibm_xraid_pdisks(info).keys():
        inventory.append((disk_id, None))
    return inventory


def check_ibm_xraid_pdisks(item, _no_params, info):

    data = parse_ibm_xraid_pdisks(info)
    for disk_path, disk_entry in data.items():
        if disk_path == item:
            slot_label, disk_id, disk_type, disk_state, slot_desc = disk_entry
            if   disk_state == "3":
                return (0, "OK - Disk is active" + " [%s]" % slot_desc)
            elif disk_state == "4":
                return (1, "WARN - Disk is rebuilding" + " [%s]" % slot_desc)
            elif disk_state == "5":
                return (2, "WARN - Disk is dead" + " [%s]" % slot_desc)

    return (2, "CRIT - disk is missing") #  + " [%s]" % data[item][4])


check_info["ibm_xraid_pdisks"] = {
      "check_function"     : check_ibm_xraid_pdisks,
      "service_description": "RAID PDisk %s",
      "has_perfdata"       : False,
      "inventory_function" : inventory_ibm_xraid_pdisks,
      # there is no information about the ext mib in the right place
      # (at least on windows)
      # this means the check has to fetch a specific oid. Limit this
      # effect to relevant systems to lessen useless scanning.
      "snmp_scan_function" : lambda oid: \
            oid(".1.3.6.1.2.1.1.1.0").lower() in ["software: windows", "linux"] \
            and oid(".1.3.6.1.4.1.795.14.1.100.1.0"),
      "snmp_info"          : (".1.3.6.1.4.1.795.14.1", [
         "503.1.1.4",
         "400.1.1.1",
         "400.1.1.5",
         "400.1.1.11",
         "400.1.1.12",
         ])
}
