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

# 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 parse_megaraid_ldisks(info):
    parsed = {}
    adapter = None
    disk = 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])
            item = "%d/%d" % (adapter, disk)
            parsed[item] = {}

        elif item in parsed.keys():

            if line[0].startswith("State"):
                parsed[item]["state"] = l.split(":")[1].strip()
            elif line[0].startswith("Default"):
                if line[1].startswith("Cache"):
                    parsed[item]["default_cache"] = " ".join(line[3:]).replace(': ', '')
                elif line[1].startswith("Write"):
                    parsed[item]["default_write"] = " ".join(line[3:]).replace(': ', '')

            elif line[0].startswith("Current"):
                if line[1].startswith("Cache"):
                    parsed[item]["current_cache"] = " ".join(line[3:]).replace(': ', '')
                elif line[1].startswith("Write"):
                    parsed[item]["current_write"] = " ".join(line[3:]).replace(': ', '')
    return parsed


def inventory_megaraid_ldisks(parsed):
    for item in parsed.keys():
        yield item, None


def check_megaraid_ldisks(item, _no_params, parsed):
    raidstate = parsed[item]["state"]
    if raidstate == "Optimal":
        yield 0, "State is %s" % raidstate
    else:
        yield 2, "State is %s" % raidstate

    current_cache = parsed[item].get("current_cache")
    default_cache = parsed[item].get("default_cache")
    if default_cache:
        if current_cache == default_cache:
            yield 0, "Cache is %s" % current_cache
        else:
            yield 1, "Cache is %s, expected %s" % (current_cache, default_cache)

    current_write = parsed[item].get("current_write")
    default_write = parsed[item].get("default_write")
    if default_write:
        if current_write == default_write:
            yield 0, "Write is %s" % current_write
        else:
            yield 1, "Write is %s, expected %s" % (current_write, default_write)


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