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

db2_mem_default_levels = ( 10.0, 5.0 )

def inventory_db2_mem(info):
    return [ (x[1], db2_mem_default_levels ) for x in info if x[0] == "Instance"]

def check_db2_mem(item, params, info):
    if not info:
        raise MKCounterWrapped("Login into database failed")

    warn, crit = params

    in_block = False
    limit, usage = None, None
    for line in info:
        if line[1] == item:
            in_block = True
        elif in_block == True:
            if line[-1].lower() == "kb":
                value = int(line[-2]) * 1024
            elif line[-1].lower() == "mb":
                value = int(line[-2]) * 1024 * 1024
            else:
                value = int(line[-2])

            if limit == None:
                limit = value
            else:
                usage = value
                break

    if limit == None or usage == None:
        return

    left = limit - usage
    perc_level = ( 100.0 / limit ) * left
    label = " (Warn/Crit %d%%/%d%%)" % (warn, crit)

    if perc_level <= crit:
        state = 2
    elif perc_level <= warn:
        state = 1
    else:
        label = ""
        state = 0

    message = "Max: %s, Used: %s (%.2d%% Free%s) " % \
                    (get_bytes_human_readable(limit),
                     get_bytes_human_readable(usage),
                     perc_level, label)
    perf = [("mem", usage, 0, 0, 0, limit )]

    return state, message, perf


check_info['db2_mem'] = {
    "check_function"          : check_db2_mem,
    "inventory_function"      : inventory_db2_mem,
    "service_description"     : "Mem of %s",
    "has_perfdata"            : True,
    "group"                   : "db2_mem"
}

