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

AWSEBSStorageTypes = {
    "gp2": "General Purpose SSD",
    "io1": "Provisioned IOPS SSD",
    "st1": "Throughput Optimized HDD",
    "sc1": "Cold HDD",
}


def parse_aws_summary(info):
    parsed = {}
    for row in parse_aws(info):
        inst = parsed.setdefault(row['VolumeId'], row)
        type_ = row['VolumeType']
        inst.setdefault('type_readable', AWSEBSStorageTypes.get(type_, "unknown[%s]" % type_))
    return parsed


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


def check_aws_ebs_summary(item, params, parsed):
    stores_by_state = {}
    stores_by_type = {}
    long_output = []
    for volume_id, row in parsed.iteritems():
        stores_by_state.setdefault(row['State'], []).append(volume_id)
        stores_by_type.setdefault(row['VolumeType'], []).append(volume_id)
        long_output.append(
            "Volume: %s, Status: %s, Type: %s, Encrypted: %s, Creation time: %s"\
            % (volume_id, row['State'], row['VolumeType'], row['Encrypted'], row['CreateTime'])
        )

    yield 0, 'Stores: %s' % len(parsed)
    for state, stores in stores_by_state.iteritems():
        yield 0, '%s: %s' % (state, len(stores))
    for type_, stores in stores_by_type.iteritems():
        yield 0, '%s: %s' % (AWSEBSStorageTypes.get(type_, "unknown[%s]" % type_), len(stores))
    if long_output:
        yield 0, '\n%s' % '\n'.join(long_output)


check_info['aws_ebs_summary'] = {
    'parse_function': parse_aws_summary,
    'inventory_function': discover_single,
    'check_function': check_aws_ebs_summary,
    'service_description': 'AWS/EBS Summary',
    'includes': ['aws.include'],
}

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


@get_parsed_item_data
def check_aws_ebs_summary_health(item, params, ebs_data):
    metrics = ebs_data['VolumeStatus']
    ebs_status = metrics["Status"]
    yield 0 if ebs_status.lower() == 'ok' else 2, "Status: %s" % ebs_status
    for row in metrics['Details']:
        yield 0, "%s: %s" % (row['Name'], row['Status'])


check_info['aws_ebs_summary.health'] = {
    'inventory_function': lambda p:\
        inventory_aws_generic(p, ['VolumeStatus']),
    'check_function': check_aws_ebs_summary_health,
    'service_description': 'AWS/EBS Health %s',
    'includes': ['aws.include'],
}
