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

# <<<mongodb_locks>>>
# activeClients readers 0
# activeClients total 53
# activeClients writers 0
# currentQueue readers 0
# currentQueue total 32
# currentQueue writers 5


def inventory_mongodb_locks(info):
    return [(None, {})]


def check_mongodb_locks(_no_item, params, info):
    for line in info:
        what, name, count = line
        count = int(count)
        param_name = "clients" if what.startswith("active") else "queue"
        warn, crit = None, None
        state = 0
        if "%s_%s_locks" % (param_name, name) in params:
            warn, crit = params["%s_%s_locks" % (param_name, name)]
            if count >= crit:
                state = 2
            elif count >= warn:
                state = 1
        yield state, "%s-%s: %s" % (param_name.title(), name.title(), count), [
            ("%s_%s_locks" % (param_name, name), count, warn, crit)
        ]


check_info["mongodb_locks"] = {
    "inventory_function": inventory_mongodb_locks,
    "check_function": check_mongodb_locks,
    "service_description": "MongoDB Locks",
    "group": "mongodb_locks",
    "has_perfdata": True,
}
