#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.db2 import *  # pylint: disable=wildcard-import,unused-wildcard-import

# <<<db2_sort_overflow>>>
# [[[test:datenbank1]]]
# Total sorts 100
# Sort overflows 3

factory_settings["db2_sort_overflow_default_levels"] = {"levels_perc": (2.0, 4.0)}


def inventory_db2_sort_overflow(parsed):
    for key in parsed[1]:
        yield key, {}


def check_db2_sort_overflow(item, params, parsed):
    db = parsed[1].get(item)
    if not db:
        raise MKCounterWrapped("Login into database failed")

    total, overflows = tuple(float(x[-1]) for x in db)
    if total > 0:
        overflow_perc = overflows * 100 / total
    else:
        overflow_perc = 0.0
    warn, crit = params.get("levels_perc")
    if overflow_perc >= crit:
        yield 2, "%.1f%% sort overflow (leves at %.1f%%/%.1f%%)" % (overflow_perc, warn, crit)
    elif overflow_perc >= warn:
        yield 1, "%.1f%% sort overflow (leves at %.1f%%/%.1f%%)" % (overflow_perc, warn, crit)
    else:
        yield 0, "%.1f%% sort overflow" % overflow_perc

    yield 0, "Sort overflows: %d" % overflows
    yield 0, "Total sorts: %d" % total, [("sort_overflow", overflow_perc, warn, crit, 0, 100)]


check_info["db2_sort_overflow"] = {
    "parse_function": parse_db2_dbs,
    "service_description": "DB2 Sort Overflow %s",
    "check_function": check_db2_sort_overflow,
    "inventory_function": inventory_db2_sort_overflow,
    "has_perfdata": True,
    "group": "db2_sortoverflow",
    "default_levels_variable": "db2_sort_overflow_default_levels",
}
