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

# <<<mongodb_flushing>>>
# average_ms 1.28893335892
# last_ms 0
# flushed 36479


def inventory_mongodb_flushing(info):
    # This check has no default parameters
    # The average/last flush time highly depends on the size of the mongodb setup
    return [(None, {})]


def check_mongodb_flushing(_no_item, params, info):
    info_dict = dict(info)

    if not {"last_ms", "average_ms", "flushed"} <= set(info_dict):  # check if keys in dict
        yield 3, "missing data: %s" % (_get_missing_keys(["last_ms", "average_ms", "flushed"],
                                                         info_dict))
        return

    try:
        last_ms = float(info_dict["last_ms"])
        avg_flush_time = float(info_dict["average_ms"]) / 1000.0
        flushed = int(info_dict["flushed"])
    except (ValueError, TypeError):
        yield 3, "Invalid data: last_ms: %s, average_ms: %s, flushed:%s" % (
            info_dict["last_ms"], info_dict["average_ms"], info_dict["flushed"])
        return

    if "average_time" in params:
        warn, crit, avg_interval = params["average_time"]
        avg_ms_compute = get_average("flushes", time.time(), last_ms, avg_interval)
        yield check_levels_backport(
            avg_ms_compute,
            None, (warn, crit),
            unit="ms",
            infoname="Average flush time over %s minutes" % (avg_interval))

    yield check_levels_backport((last_ms / 1000.0),
                       "flush_time",
                       params.get("last_time"),
                       unit="s",
                       infoname="Last flush time")

    yield 0, "Flushes since restart: %s" % flushed, [("flushed", flushed)]
    yield 0, "Average flush time since restart: %s" % get_age_human_readable(avg_flush_time), \
          [("avg_flush_time", avg_flush_time)]


def _get_missing_keys(key_list, info_dict):
    missing_keys = []
    for key in key_list:
        if key not in info_dict:
            missing_keys += [str(key)]
    return " and ".join(sorted(missing_keys))


check_info['mongodb_flushing'] = {
    "inventory_function": inventory_mongodb_flushing,
    "check_function": check_mongodb_flushing,
    "service_description": "MongoDB Flushing",
    "group": "mongodb_flushing",
    "has_perfdata": True,
    "includes": ["backport.include"]
}
