#!/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.df import *  # pylint: disable=wildcard-import,unused-wildcard-import

# 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.size_trend import *  # pylint: disable=wildcard-import,unused-wildcard-import

# 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.threepar import *  # pylint: disable=wildcard-import,unused-wildcard-import

factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def inventory_3par_capacity(parsed):
    for key, value in parsed.items():
        if value["totalMiB"] == 0:
            continue
        yield (key.replace("Capacity", ""), {})


def check_3par_capacity(item, params, parsed):
    for key, value in parsed.items():
        if key.replace("Capacity", "") == item:
            total = value["totalMiB"]
            free = value["freeMiB"]
            failed = value["failedCapacityMiB"]

            yield df_check_filesystem_list(item, params, [(item, total, free, failed)])

            if failed == 0:
                return

            failed_percent = float(failed) / float(total) * 100.0
            warn, crit = params.get("failed_capacity_levels", (0.0, 0.0))
            msg = "%s MB / %.1f%% failed (warn/crit at %.1f%%/%.1f%%)" % (
                failed,
                failed_percent,
                warn,
                crit,
            )
            if failed_percent >= crit:
                yield 2, msg
            elif failed_percent >= warn:
                yield 1, msg


check_info["3par_capacity"] = {
    "parse_function": parse_3par,
    "inventory_function": inventory_3par_capacity,
    "check_function": check_3par_capacity,
    "service_description": "Capacity %s",
    "has_perfdata": True,
    "group": "threepar_capacity",
    "default_levels_variable": "filesystem_default_levels",
}
