#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# 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
    item = None
    for line in info:
        l = " ".join(line)
        if line[0] == "Adapter" and not l.endswith('No Virtual Drive Configured.'):
            adapter = line[1]
        elif megaraid_ldisks_is_new_drive(l) and adapter is not None:
            disk = l.split(': ')[1].split(' ')[0]
            item = "%d/%d" % (int(adapter), int(disk))
            parsed[item] = {}

        elif item is not None and item in parsed:

            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:
        yield item, None


def check_megaraid_ldisks(item, _no_params, parsed):
    if item not in parsed:
        return

    data = parsed[item]
    raidstate = data.get("state")
    if raidstate is None:
        return

    if raidstate == "Optimal":
        yield 0, "State is %s" % raidstate
    else:
        yield 2, "State is %s" % raidstate

    current_cache = data.get("current_cache")
    default_cache = data.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 = data.get("current_write")
    default_write = data.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',
}
