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

import collections
import cmk.base.plugins.agent_based.utils.ucs_bladecenter as ucs_bladecenter


def check_ucs_c_rack_server_faultinst(
    _item: None,
    _params: Dict,
    parsed: Dict[str, List[str]],
) -> Iterable[Tuple[int, str]]:

    if not parsed:
        yield 0, "No fault instances found"
        return

    states = [
        ucs_bladecenter.UCS_FAULTINST_SEVERITY_TO_STATE.get(severity, 3)
        for severity in parsed['Severity']
    ]
    if 2 in states:
        overall_state = 2
    else:
        overall_state = max(states)

    # report overall state and summary of fault instances
    severity_counter = collections.Counter(parsed['Severity'])
    yield overall_state, "Found faults: " + ", ".join([
        "%s with severity '%s'" % (severity_counter[severity], severity)
        for severity in sorted(severity_counter.keys())
    ])

    # report individual faults sorted by monitoring state
    start_str = "\n\nIndividual faults:\n"
    for index in sorted(range(len(states)), key=lambda idx: states[idx]):
        yield states[index], start_str + ', '.join([
            '%s: %s' % (key, parsed[key][index])
            for key in ['Severity', 'Description', 'Cause', 'Code', 'Affected DN']
        ])
        start_str = ""


check_info['ucs_c_rack_server_faultinst'] = {
    'inventory_function': lambda p: [(None, {})],
    'check_function': check_ucs_c_rack_server_faultinst,
    'service_description': 'Fault Instances',
}
