#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             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_datapower_pdrive(info):
    for controller, device, _ldrive, _position, status, _progress, _vendor, _product, _fail in info:
        if status != "12":
            item = "%s-%s" % (controller, device)
            yield item, None


def check_datapower_pdrive(item, _no_params, info):
    datapower_pdrive_status = {
        "1": (0, "Unconfigured/Good"),
        "2": (0, "Unconfigured/Good/Foreign"),
        "3": (1, "Unconfigured/Bad"),
        "4": (1, "Unconfigured/Bad/Foreign"),
        "5": (0, "Hot spare"),
        "6": (1, "Offline"),
        "7": (2, "Failed"),
        "8": (1, "Rebuilding"),
        "9": (0, "Online"),
        "10": (1, "Copyback"),
        "11": (1, "System"),
        "12": (1, "Undefined"),
    }
    datapower_pdrive_fail = {
        "1": (2, "disk reports failure"),
        "2": (0, "disk reports no failure"),
    }
    datapower_pdrive_position = {
        "1": "HDD 0",
        "2": "HDD 1",
        "3": "HDD 2",
        "4": "HDD 3",
        "5": "undefined",
    }
    for controller, device, ldrive, position, status, progress, vendor, product, fail in info:
        if item == "%s-%s" % (controller, device):
            member_of_ldrive = "%s-%s" % (controller, ldrive)
            state, state_txt = datapower_pdrive_status[status]
            position_txt = datapower_pdrive_position[position]
            if int(progress) != 0:
                progress_txt = " - Progress: %s%%" % progress
            else:
                progress_txt = ""
            infotext = "%s%s, Position: %s, Logical Drive: %s, Product: %s %s"\
                    % (state_txt, progress_txt, position_txt, member_of_ldrive, vendor, product)
            yield state, infotext

            if fail:
                yield datapower_pdrive_fail[fail]


check_info['datapower_pdrive'] = {
    "inventory_function": inventory_datapower_pdrive,
    "check_function": check_datapower_pdrive,
    "service_description": "Physical Drive %s",
    "snmp_info": (
        ".1.3.6.1.4.1.14685.3.1.260.1",
        [
            "1",  # dpStatusRaidPhysicaldrivetatusControllerID
            "2",  # dpStatusRaidPhysicaldrivetatusDeviceID
            "4",  # dpStatusRaidPhysicaldrivetatusLogicalDriveID
            "6",  # dpStatusRaidPhysicaldrivetatusPosition
            "7",  # dpStatusRaidPhysicaldrivetatusState
            "8",  # dpStatusRaidPhysicaldrivetatusProgressPercent
            "14",  # dpStatusRaidPhysicaldrivetatusVendorID
            "15",  # dpStatusRaidPhysicaldrivetatusProductID
            "18",  # dpStatusRaidPhysicaldrivetatusFailure
        ]),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") in
                          [".1.3.6.1.4.1.14685.1.7", ".1.3.6.1.4.1.14685.1.3"],
}
