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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.aws import *  # pylint: disable=wildcard-import,unused-wildcard-import
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.items():
        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.items():
        yield 0, '%s: %s' % (state, len(stores))
    for type_, stores in stores_by_type.items():
        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',
}

#.
#   .--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',
}
