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

# <<<mysql_capacity>>>
# greendb 163840  1428160512
# hirn    16384   238026752
# information_schema  9216    0
# mysql   650067  0
# performance_schema  0   0
# wa-confluence   15499264    13805551616

def inventory_mysql_size(info):
    inventory = []
    for dbname, used, avail in info:
        if dbname not in [ "information_schema", "mysql", "performance_schema" ]:
            inventory.append((dbname, None))
    return inventory

def check_mysql_size(item, params, info):
    for dbname, size, avail in info:
        if item == dbname:
            size = int(size)
            infotext = " - Size is %s" % get_bytes_human_readable(size)
            if params:
                state = 0
                warn, crit = params # in MB
                warn_b = warn * 1048576
                crit_b = crit * 1048576
                perfdata = [("size", size, warn, crit)]
		if size > crit:
                   state = 2
                   infotext += " (critical at %s)" % get_bytes_human_readable(crit)
                elif size > warn:
                   state = 1
                   infotext += " (warning at %s)" % get_bytes_human_readable(crit)
            else:
                state = 0
                perfdata = [("size", size)]
            return (state, nagios_state_names[state] + infotext, perfdata)
    return (3, "UNKNOWN - Database not found in Agent output")


check_info['mysql_capacity'] = {
    "check_function"          : check_mysql_size,
    "inventory_function"      : inventory_mysql_size,
    "service_description"     : "MySQL DB %s Size",
    "has_perfdata"            : True,
    "group"                   : "dbsize",
#    "default_levels_variable" : "filesystem_default_levels",
#    "includes"                : [ "dbsize.include" ],
}
