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

AWSNoExceptionsText = "No exceptions"


def parse_aws_exceptions(info):
    parsed = {}
    for line in info:
        parsed.setdefault(line[0], set()).add(" ".join(line[1:]))
    return parsed


def check_aws_exceptions(item, params, parsed):
    for title, messages in parsed.items():
        errors = [message for message in messages if message != AWSNoExceptionsText]
        if errors:
            yield 2, "%s %s" % (title, ", ".join(errors))
        else:
            yield 0, "%s %s" % (title, AWSNoExceptionsText)


check_info["aws_exceptions"] = {
    "parse_function": parse_aws_exceptions,
    "inventory_function": discover_single,
    "check_function": check_aws_exceptions,
    "service_description": "AWS Exceptions",
}
