#!/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_collections:sep(9)>>>
# tanserver        tans        count        0
# tanserver        tans        indexDetails        {}
# tanserver        tans        storageSize        8192
# tanserver        tans        ok        1.0
# tanserver        tans        lastExtentSize        8192.0
# tanserver        tans        userFlags        1
# tanserver        tans        totalIndexSize        24528
# tanserver        tans        capped        False
# tanserver        tans        numExtents        1
# tanserver        tans        nindexes        3
# tanserver        tans        ns        tanserver.tans


def parse_mongodb_collections(info):
    required_keys_int = ("size", "storageSize")
    parsed = {}
    for line in info:
        db_name, collection, key, value = line
        data = parsed.setdefault("%s %s" % (db_name, collection), {})
        if key in required_keys_int:
            try:
                data[key] = int(value)
            except ValueError:
                pass
    return parsed


@get_parsed_item_data
def check_mongodb_collections(_no_item, params, data):
    for key, label in (
        ("size", "Uncompressed size in memory"),
        ("storageSize", "Allocated for document storage"),
    ):
        if key not in data:
            continue
        levels = params.get("levels_%s" % key.lower())
        if levels is not None:
            levels = (levels[0] * 1024**2, levels[1] * 1024**2)
        yield check_levels_backport(
            data[key], None, levels, human_readable_func=get_bytes_human_readable, infoname=label)


check_info["mongodb_collections"] = {
    "parse_function": parse_mongodb_collections,
    "inventory_function": discover(),
    "check_function": check_mongodb_collections,
    "service_description": "MongoDB Collection %s",
    "group": "mongodb_collections",
    "has_perfdata": False,
    "includes": ["backport.include"]
}
