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

# <<<elasticsearch_indices>>>
# filebeat-6.5.4 11492614 801366929

ElasticIndex = collections.namedtuple("ElasticIndex", [
    "count",
    "size",
])


def parse_elasticsearch_indices(info):
    parsed = {}

    for line in info:
        try:
            index, count, size = line
            if parsed.get(index):
                # sum up count/size for index names which already appeared
                parsed[index] = parsed[index]._replace(count=parsed[index].count + float(count))
                parsed[index] = parsed[index]._replace(size=parsed[index].size + float(size))
            else:
                parsed.setdefault(index, ElasticIndex(
                    float(count),
                    float(size),
                ))
        except (IndexError, ValueError):
            pass

    return parsed


@get_parsed_item_data
def check_elasticsearch_indices(item, params, item_data):
    for value, name, infotext, hr_func, unit in [
        (item_data.count, "elasticsearch_count", "count", int, "docs"),
        (item_data.size, "elasticsearch_size", "size", get_bytes_human_readable, ""),
    ]:

        if params.get("%s_rate" % name):
            avg = params.get("%s_rate" % name)[2]
        else:
            avg = 30

        yield check_levels(value,
                           name, (None, None),
                           human_readable_func=hr_func,
                           unit=unit,
                           infoname="Total %s" % infotext)

        this_time = time.time()
        rate = get_rate("elasticsearch_indices.%s.%s" % (name, item), this_time, value) * 60

        avg_rate = get_average("elasticsearch_indices.%s.%s.avg" % (name, item), this_time, rate,
                               avg)

        if params.get("%s_rate" % name):
            warn, crit, avg = params["%s_rate" % name]
            avg_rate_warn = (avg_rate * (warn / 100.0 + 1))
            avg_rate_crit = (avg_rate * (crit / 100.0 + 1))
            params_avg = (avg_rate_warn, avg_rate_crit)
        else:
            params_avg = (None, None)

        yield check_levels(rate,
                           "%s_rate" % name,
                           params_avg,
                           human_readable_func=hr_func,
                           unit="%s per Minute" % unit,
                           infoname="Average %s" % infotext)


check_info["elasticsearch_indices"] = {
    "check_function": check_elasticsearch_indices,
    "parse_function": parse_elasticsearch_indices,
    "inventory_function": discover(),
    "service_description": "Elasticsearch Indice %s",
    "has_perfdata": True,
    "group": "elasticsearch_indices",
}
