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

# No used space check for Tablsspaces with CONTENTS in ('TEMPORARY','UNDO')
# It is impossible to check the used space in UNDO and TEMPORARY Tablespaces
# These Types of Tablespaces are ignored in this plugin.
# This restriction is only working with newer agents, because we need an
# additional parameter at end if each datafile

# <<<db2_tablespaces>>>
# [[[db2taddm:CMDBS1]]]
# SYSCATSPACE DMS NORMAL 786304 786432 704224 82080
# USERSPACE1 DMS NORMAL 16874496 16875520 7010176 9809920
# SYSTOOLSPACE DMS NORMAL 32640 32768 3600 29040
# LARGESPACE2 DMS NORMAL 80896 81920 3072 77824
# TEMPSPACE1 SMS NORMAL 32 32 32 959659392
# USERSPACE2 SMS NORMAL 1327488 1327488 1327488 119957424
# LARGETEMP2 SMS NORMAL 32 32 32 119957424
# USERSPACE3 SMS NORMAL 1626712 1626712 1626712 119957424
# MYTMPSPACE SMS NORMAL 64 64 64 959659392
# SYSTOOLSTMPSPACE SMS NORMAL 32 32 32 959659392

factory_settings["db2_tablespaces_default_levels"] = {
    "levels": (10.0, 5.0),
    "magic_normsize": 1000,
}


def inventory_db2_tablespaces(parsed):
    for instance, values in parsed[1].items():
        for table in values[1:]:
            yield "%s.%s" % (instance, table[0]), {}


def check_db2_tablespaces(item, params, parsed):
    try:
        instance, tbsname = item.split('.')
    except ValueError:
        yield 3, 'Invalid check item given (must be <instance>.<tablespace>)'
        return

    db = parsed[1].get(instance)
    if not db:
        raise MKCounterWrapped("Login into database failed")

    db_tables = {x[0]: x[1:] for x in db[1:]}
    tablespace = db_tables.get(tbsname)
    if not tablespace:
        return

    headers = db[0]
    tablespace_dict = dict(zip(headers[1:], tablespace))

    tbsp_type = tablespace_dict["TBSP_TYPE"]
    tbsp_state = tablespace_dict["TBSP_STATE"]
    usable = float(tablespace_dict["TBSP_USABLE_SIZE_KB"]) * 1024
    total = float(tablespace_dict["TBSP_TOTAL_SIZE_KB"]) * 1024
    used = float(tablespace_dict["TBSP_USED_SIZE_KB"]) * 1024
    free = float(tablespace_dict["TBSP_FREE_SIZE_KB"]) * 1024

    if tbsp_type == "SMS":
        usable = free  # for SMS free size is the amount of disk space available to the db file

    warn, crit, levels_text, as_perc = db_get_tablespace_levels_in_bytes(usable, params)

    infotext = "%s of %s used" % (get_bytes_human_readable(used), get_bytes_human_readable(usable))
    perfdata = [("tablespace_size", usable, max(0, total - (warn or 0)), max(
        0, total - (crit or 0))), ("tablespace_used", used), ("tablespace_max_size", total)]
    yield 0, infotext, perfdata

    perc_free = 100.0 - used / usable * 100.0
    abs_free = usable - used

    state = 0
    infotext = "%s free" % get_percent_human_readable(perc_free)
    if crit is not None and abs_free <= crit:
        state = 2
    elif warn is not None and abs_free <= warn:
        state = 1
    if state:
        if as_perc:
            value_str = get_percent_human_readable(perc_free)
        else:
            value_str = get_bytes_human_readable(abs_free)
        infotext = "only %s left %s" % (value_str, levels_text)
    yield state, infotext

    yield tbsp_state.lower() != "normal" and 1 or 0, "State: %s" % tbsp_state
    yield 0, "Type: %s" % tbsp_type


check_info['db2_tablespaces'] = {
    "parse_function": parse_db2_dbs,
    "service_description": "DB2 Tablespace %s",
    "check_function": check_db2_tablespaces,
    "inventory_function": inventory_db2_tablespaces,
    "has_perfdata": True,
    "group": "db2_tablespaces",
    "default_levels_variable": "db2_tablespaces_default_levels",
    "includes": ["db.include", "db2.include"]
}
