#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             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.


# <<<ucs_bladecenter_faultinst:sep(9)>>>
# faultInst   Dn sys/chassis-2/bl...ault-F1256 Descr Local disk 2 missing on server 2/3    Severity info
# faultInst   Dn sys/chassis-2/bl...ault-F1256 Descr Local disk 1 missing on server 2/3    Severity info
# faultInst   Dn sys/chassis-1/bl...ault-F1256 Descr Local disk 2 missing on server 1/3    Severity info

def inventory_ucs_bladecenter_faultinst(parsed):
    yield None, None

def check_ucs_bladecenter_faultinst(item, _no_params, parsed):
    severity_map = {
        "critical"  : 2,
        "major"     : 1,
        "warning"   : 1,
        "minor"     : 1,
        "info"      : 0,
        "condition" : 0,
        "cleared"   : 0,
    }
    severities = {}
    for key, values in parsed.get("faultInst", {}).items():
        entry_sev = values.get("Severity").lower()
        severities.setdefault(entry_sev, [])
        severities[entry_sev].append(values)

    state = 0
    for sev, instances in severities.items():
        sev_state = severity_map.get(sev, 3)
        state = max(state, sev_state)

        # Right now, OK instances are also reported in detail
        # If required we can increase the state level here, so that only WARN+ messages are shown
        if sev_state >= 0:
            extra_info = []
            for instance in instances:
                extra_info.append("%s" % instance["Descr"])
            extra_info = ": " + ", ".join(extra_info)
        else:
            extra_info = ""

        yield sev_state, "%d %s Instances%s" % (len(instances), sev.upper(), extra_info)

check_info["ucs_bladecenter_faultinst"] = {
    'parse_function':          ucs_bladecenter_convert_info,
    'inventory_function':      inventory_ucs_bladecenter_faultinst,
    'check_function':          check_ucs_bladecenter_faultinst,
    'service_description':     'Fault Instances',
    'includes':                [ 'ucs_bladecenter.include' ],
}

