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


def parse_aws_rds_summary(info):
    try:
        return {instance['DBInstanceIdentifier']: instance for instance in parse_aws(info)}
    except IndexError:
        return {}


#   .--summary-------------------------------------------------------------.
#   |                                                                      |
#   |           ___ _   _ _ __ ___  _ __ ___   __ _ _ __ _   _             |
#   |          / __| | | | '_ ` _ \| '_ ` _ \ / _` | '__| | | |            |
#   |          \__ \ |_| | | | | | | | | | | | (_| | |  | |_| |            |
#   |          |___/\__,_|_| |_| |_|_| |_| |_|\__,_|_|   \__, |            |
#   |                                                    |___/             |
#   '----------------------------------------------------------------------'


def check_aws_rds_summary(item, params, parsed):
    instances_by_classes = {}
    for instance in parsed.itervalues():
        instance_class = instance['DBInstanceClass']
        instances_by_classes.setdefault(instance_class, []).append(instance)

    class_infos = []
    for instance_class, instances in instances_by_classes.iteritems():
        class_infos.append("%s: %s" % (instance_class, len(instances)))
    yield 0, ", ".join(class_infos)


check_info['aws_rds_summary'] = {
    'parse_function': parse_aws_rds_summary,
    'inventory_function': discover_single,
    'check_function': check_aws_rds_summary,
    'service_description': 'AWS/RDS Summary',
    'includes': ['aws.include'],
}

#.
#   .--DB status-----------------------------------------------------------.
#   |              ____  ____        _        _                            |
#   |             |  _ \| __ )   ___| |_ __ _| |_ _   _ ___                |
#   |             | | | |  _ \  / __| __/ _` | __| | | / __|               |
#   |             | |_| | |_) | \__ \ || (_| | |_| |_| \__ \               |
#   |             |____/|____/  |___/\__\__,_|\__|\__,_|___/               |
#   |                                                                      |
#   '----------------------------------------------------------------------'


@get_parsed_item_data
def check_aws_rds_summary_db(item, params, data):
    db_name = data.get('DBName')
    pre_info = ""
    if db_name is not None:
        pre_info = "[%s] " % db_name
    yield 0, '%sStatus: %s' % (pre_info, data['DBInstanceStatus'])

    multi_az = data.get('MultiAZ')
    if multi_az is not None:
        if multi_az:
            multi_az_readable = "yes"
        else:
            multi_az_readable = "no"
        yield 0, 'Multi AZ: %s' % multi_az_readable

    zone = data.get('AvailabilityZone')
    if zone is not None:
        region = zone[:-1]
        zone_info = zone[-1]
        yield 0, 'Availability zone: %s (%s)' % (AWSRegions[region], zone_info)


check_info['aws_rds_summary.db_status'] = {
    'inventory_function': lambda p: inventory_aws_generic(p, ['DBInstanceStatus']),
    'check_function': check_aws_rds_summary_db,
    'service_description': 'AWS/RDS %s Info',
    'includes': ['aws.include'],
}
