#!/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):
    databases = {}
    for line in info:
        db, coll, what, value = line
        databases.setdefault(db, {}).setdefault(coll, {})[what] = value

    parsed = {}
    for db, collections in databases.items():
        for collection, values in collections.items():
            parsed["%s %s" % (db, collection)] = values
    return parsed

def inventory_mongodb_collections(parsed):
    for item in parsed:
        yield item, {}

def check_mongodb_collections(item, params, parsed):
    fslist_blocks = []
    for coll_item, values in parsed.items():
        used_mb  = float(values["size"]) / 1024.0**2
        total_mb = float(values["storageSize"]) / 1024.0**2
        fslist_blocks.append((coll_item, total_mb, total_mb - used_mb, 0))

    return df_check_filesystem_list(item, params, fslist_blocks, None)


check_info["mongodb_collections"] = {
    "parse_function"          : parse_mongodb_collections,
    "check_function"          : check_mongodb_collections,
    "inventory_function"      : inventory_mongodb_collections,
    "service_description"     : "MongoDB Collection %s",
    "group"                   : "mongodb_collections",
    "default_levels_variable" : "filesystem_default_levels",
    "includes"                : [ "df.include" ],
    "has_perfdata"            : True,
}


