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


def inventory_mongodb_counters(parsed):
    yield "Operations", None
    if "opcountersRepl" in parsed:
        yield "Replica Operations", None


def check_mongodb_counters(item, _no_params, parsed):
    item_map = {"Operations": "opcounters", "Replica Operations": "opcountersRepl"}
    real_item_name = item_map.get(item)
    data = parsed.get(real_item_name)
    if not data:
        return

    now = time.time()
    for what, value in data.items():
        what_rate = get_rate(what, now, value)
        yield 0, "%s: %.2f/s" % (what.title(), what_rate), [("%s_ops" % what, what_rate)]


check_info["mongodb_counters"] = {
    "inventory_function": inventory_mongodb_counters,
    "check_function": check_mongodb_counters,
    "service_description": "MongoDB Counters %s",
    "has_perfdata": True,
}
