#!/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 parse_hp_proliant_raid(info):
    parsed = {}
    for number, name, status, size_str, rebuild in info:
        if name in parsed:
            itemname = "%s %s" % (name, number)
        else:
            itemname = name

        parsed.setdefault(itemname.strip(), {
            "status"       : status,
            "size"         : int(size_str) * 1024 * 1024,
            "rebuild_perc" : rebuild,
        })

    return parsed


def inventory_hp_proliant_raid(parsed):
    for raid in parsed:
        yield raid, None


def check_hp_proliant_raid(item, _no_params, parsed):
    map_states = {
        "1"  : (3, "other"),
        "2"  : (0, "OK"),
        "3"  : (2, "failed"),
        "4"  : (1, "unconfigured"),
        "5"  : (1, "recovering"),
        "6"  : (1, "ready for rebuild"),
        "7"  : (1, "rebuilding"),
        "8"  : (2, "wrong drive"),
        "9"  : (2, "bad connect"),
        "10" : (2, "overheating"),
        "11" : (1, "shutdown"),
        "12" : (1, "automatic data expansion"),
        "13" : (2, "not available"),
        "14" : (1, "queued for expansion"),
        "15" : (1, "multi-path access degraded"),
        "16" : (1, "erasing"),
    }

    if item in parsed:
        data       = parsed[item]
        dev_status = data["status"]
        state, state_readable = map_states[dev_status]
        infotext   = "Status: %s, Logical volume size: %s" % \
                     (state_readable, get_bytes_human_readable(data["size"]))

        # From CPQIDA-MIB:
        # This value is the percent complete of the rebuild.
        # This value is only valid if the Logical Drive Status is
        # rebuilding (7) or expanding (12).
        # If the value cannot be determined or a rebuild is not active,
        # the value is set to 4,294,967,295.
        if dev_status in [ "7", "12" ]:
            infotext += "Rebuild: %s%%" % data["rebuild_perc"]

        return state, infotext


check_info["hp_proliant_raid"] = {
    'parse_function'        : parse_hp_proliant_raid,
    'check_function'        : check_hp_proliant_raid,
    'inventory_function'    : inventory_hp_proliant_raid,
    'service_description'   : 'Logical Device %s',
    'snmp_info'             : (".1.3.6.1.4.1.232.3.2.3.1.1", [
                                "2",    # CPQIDA-MIB::cpqDaLogDrvIndex
                                "14",   # CPQIDA-MIB::cpqDaLogDrvOsName
                                "4",    # CPQIDA-MIB::cpqDaLogDrvStatus
                                "9",    # CPQIDA-MIB::cpqDaLogDrvSize
                                "12",   # CPQIDA-MIB::cpqDaLogDrvPercentRebuild
                               ]),
    'snmp_scan_function'    : \
         lambda oid: "proliant" in oid(".1.3.6.1.4.1.232.2.2.4.2.0", "").lower(),
}
