#!/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-
# 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.

# Example output from agent:
# Adapter 0 -- Virtual Drive Information:
# Virtual Disk: 0 (Target Id: 0)
# Size:139488MB
# State: Optimal
# Stripe Size: 64kB
# Number Of Drives:2
# Adapter 1: No Virtual Drive Configured.

def megaraid_ldisks_is_new_drive(l):
    return l.startswith('Virtual Disk:') or l.startswith('Virtual Drive:') \
           or l.startswith('CacheCade Virtual Drive:')

def inventory_megaraid_ldisks(info):
    inventory = []
    adapter = None
    for line in info:
        l = ' '.join(line)
        if line[0] == "Adapter" and not l.endswith('No Virtual Drive Configured.'):
            adapter = int(line[1])
        elif megaraid_ldisks_is_new_drive(l):
            disk = int(l.split(': ')[1].split(' ')[0])
            inventory.append( ("%d/%d" % (adapter, disk), "", None) )
    return inventory

def check_megaraid_ldisks(item, _no_params, info):
    adapter = None
    cache   = None
    write   = None
    found   = False
    result  = 0
    infotext = ''
    for line in info:
        l = ' '.join(line)
        if line[0] == "Adapter" and not l.endswith('No Virtual Drive Configured.'):
            adapter = int(line[1])
        elif megaraid_ldisks_is_new_drive(l):
            if found:
                break
            disk = int(l.split(': ')[1].split(' ')[0])
            found = "%d/%d" % (adapter, disk) == item
        elif found:
            if line[0].startswith("State"):
                state = " ".join(line[1:]).replace(': ', '')
                infotext += "state is %s" % state
                if state != "Optimal":
                    result = max(result, 2)
            elif line[0].startswith("Default") and line[1].startswith("Cache"):
                cache = " ".join(line[3:]).replace(': ', '')
            elif line[0].startswith("Current") and line[1].startswith("Cache"):
                state = " ".join(line[3:]).replace(': ', '')
                if cache != state:
                    infotext += ", cache is %s, expected %s" % (state, cache)
                    result = max(result, 1)
            elif line[0].startswith("Default") and line[1].startswith("Write"):
                write = " ".join(line[3:]).replace(': ', '')
            elif line[0].startswith("Current") and line[1].startswith("Write"):
                state = " ".join(line[3:]).replace(': ', '')
                if write != state:
                    infotext += ", write is %s, expected %s" % (write, cache)
                    result = max(result, 1)
    if found:
        return (result, infotext)
    return (3, "no such adapter/logical disk found")


check_info["megaraid_ldisks"] = {
    'check_function':          check_megaraid_ldisks,
    'inventory_function':      inventory_megaraid_ldisks,
    'service_description':     'RAID Adapter/LDisk %s',
    'has_perfdata':            True,
}
