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

# <<<scaleio_system:sep(9)>>>
# SYSTEM 5914d6b47d479d5a:
#        ID                                                 5914d6b47d479d5a
#        NAME                                               N/A
#        CAPACITY_ALERT_HIGH_THRESHOLD                      80%
#        CAPACITY_ALERT_CRITICAL_THRESHOLD                  90%
#        MAX_CAPACITY_IN_KB                                 65.5 TB (67059 GB)
#        UNUSED_CAPACITY_IN_KB                              17.2 TB (17635 GB)
#


def inventory_scaleio_system(parsed):
    for entry in parsed:
        yield entry, {}


def check_scaleio_system(item, params, parsed):
    data = get_scaleio_data(item, parsed)
    if not data:
        return

    if "levels" not in params:
        params["levels"] = (
            float(data["CAPACITY_ALERT_HIGH_THRESHOLD"][0].strip("%")),
            float(data["CAPACITY_ALERT_CRITICAL_THRESHOLD"][0].strip("%")),
        )
    total = int(data["MAX_CAPACITY_IN_KB"][2].strip("(")) * 1024
    free = int(data["UNUSED_CAPACITY_IN_KB"][2].strip("(")) * 1024

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


check_info["scaleio_system"] = {
    "parse_function": lambda info: parse_scaleio(info, "SYSTEM"),
    "inventory_function": inventory_scaleio_system,
    "check_function": check_scaleio_system,
    "service_description": "ScaleIO System %s",
    "group": "filesystem",
    "has_perfdata": True,
}
