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


def parse_aws_ebs(info):
    return _extract_aws_metrics_by_labels(
        [
            "VolumeReadOps",
            "VolumeWriteOps",
            "VolumeReadBytes",
            "VolumeWriteBytes",
            "VolumeQueueLength",
            "BurstBalance",
            #"VolumeThroughputPercentage",
            #"VolumeConsumedReadWriteOps",
            #"VolumeTotalReadTime",
            #"VolumeTotalWriteTime",
            #"VolumeIdleTime",
        ],
        parse_aws(info))


#   .--Disk IO-------------------------------------------------------------.
#   |                     ____  _     _      ___ ___                       |
#   |                    |  _ \(_)___| | __ |_ _/ _ \                      |
#   |                    | | | | / __| |/ /  | | | | |                     |
#   |                    | |_| | \__ \   <   | | |_| |                     |
#   |                    |____/|_|___/_|\_\ |___\___/                      |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def check_aws_ebs(item, params, parsed):
    now = time.time()
    disks = {}
    for disk_name, metrics in parsed.iteritems():
        disks.setdefault(
            disk_name, {
                "read_ios": get_rate("aws_ebs_disk_io_read_ios.%s" % item, now,
                                     metrics['VolumeReadOps']),
                "write_ios": get_rate("aws_ebs_disk_io_write_ios.%s" % item, now,
                                      metrics['VolumeWriteOps']),
                "read_throughput": get_rate("aws_ebs_disk_io_read_throughput.%s" % item, now,
                                            metrics['VolumeReadBytes']),
                "write_throughput": get_rate("aws_ebs_disk_io_write_throughput.%s" % item, now,
                                             metrics['VolumeWriteBytes']),
                "queue_length": get_rate("aws_ebs_disk_io_queue_len.%s" % item, now,
                                         metrics['VolumeQueueLength']),
            })
    return check_diskstat_dict(item, params, disks)


check_info['aws_ebs'] = {
    'parse_function': parse_aws_ebs,
    'inventory_function': lambda p:\
        inventory_aws_generic(p, ['VolumeReadOps', 'VolumeWriteOps', 'VolumeReadBytes', 'VolumeWriteBytes', 'VolumeQueueLength']),
    'check_function': check_aws_ebs,
    'service_description': 'AWS/EBS Disk IO %s',
    'includes': ['diskstat.include', 'aws.include'],
    'group': 'diskstat',
    'has_perfdata': True,
}

#.
#   .--burst balance-------------------------------------------------------.
#   |    _                    _     _           _                          |
#   |   | |__  _   _ _ __ ___| |_  | |__   __ _| | __ _ _ __   ___ ___     |
#   |   | '_ \| | | | '__/ __| __| | '_ \ / _` | |/ _` | '_ \ / __/ _ \    |
#   |   | |_) | |_| | |  \__ \ |_  | |_) | (_| | | (_| | | | | (_|  __/    |
#   |   |_.__/ \__,_|_|  |___/\__| |_.__/ \__,_|_|\__,_|_| |_|\___\___|    |
#   |                                                                      |
#   '----------------------------------------------------------------------'


@get_parsed_item_data
def check_aws_ebs_burst_balance(item, params, metrics):
    warn, crit = params.get("burst_balance_levels_lower", (None, None))
    yield check_levels_backport(
        metrics['BurstBalance'],
        "aws_burst_balance", (None, None, warn, crit),
        human_readable_func=get_percent_human_readable,
        infoname='Balance')


check_info['aws_ebs.burst_balance'] = {
    'inventory_function': lambda p:\
        inventory_aws_generic(p, ['BurstBalance']),
    'check_function': check_aws_ebs_burst_balance,
    'service_description': 'AWS/EBS Burst Balance %s',
    'includes': ['aws.include', 'backport.include'],
    'group': 'aws_ebs_burst_balance',
    'has_perfdata': True
}
