#!/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_hp_proliant_raid(info):
    if len(info) > 0:
        inventory = []
        for line in info:
            inventory.append((line[0], None))
        return inventory

def check_hp_proliant_raid(item, _no_params, info):
    hp_proliant_raid_statusmap = {
        3   : "More physical drives have failed than the fault tolerance mode can handle.",
        4   : "Logical drive is not configured.",
        5   : "Recovery for at least one physical drive hasfailed. No data loss currently.",
        6   : "Logical drive is ready for recovery but is still operating in Interim Recovery Mode.",
        8   : "The wrong physical drive was replaced after failure.",
        9   : "A physical drive is not responding.",
        10  : "Enclosue is overheated but drives still functioning and should be shutdown.",
        11  : "Enclosure is overheated and drives are shutdown.",
        12  : "Logical drive is currently doing Automatic Data Expansion.",
        13  : "Logical drive is currently unavailable.",
        14  : "Logical drive is in the queue for expansion.",

    }
    for line in info:
        if line[0] == item:
            state, size_mb, percent_rebuild = map(saveint, line[1:])
            drive_size = "Logical Volume Size: %s" % get_bytes_human_readable(size_mb * 1024 * 1024 )
            if state == 7:
                return 1, "Rebuild: %d%% finished. %s" % ( percent_rebuild, drive_size )
            if state == 2:
                return 0, "In normal operation mode. " + drive_size
            if state in [ 4, 5, 6, 12, 14 ]:
                return 1, hp_proliant_raid_statusmap[state] + drive_size
            if state in [ 3, 8, 9, 10, 12, 13 ]:
                return 2, hp_proliant_raid_statusmap[state] + drive_size
    return (3, "Drive not found or Uknown state")

check_info["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", [
                                14, # Drive Name
                                4,  # Logical Drive status
                                9,  # Drive Size
                                12, # Percent rebuild
        ]
    ),
    'snmp_scan_function': \
         lambda oid: "proliant" in oid(".1.3.6.1.4.1.232.2.2.4.2.0", "").lower(),
}
