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

# <<<prism_storage_pools:sep(9)>>>
# name    usage   capacity
# foobar    2518694292480   38575532134515


factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def inventory_prism_storage_pools(parsed):
    for row in parsed:
        yield row["name"], {}


def _convert_byte_to_mbyte(value):
    return int(value) // 1024**2


def check_prism_storage_pools(item, params, parsed):
    for row in parsed:
        if row["name"] == item:
            # input values are in bytes, convert to mb
            capacity = _convert_byte_to_mbyte(row["capacity"])
            available = capacity - _convert_byte_to_mbyte(row["usage"])
            return df_check_filesystem_single(
                item,
                capacity,
                available,
                0,
                None,
                None,
                params,
            )


check_info["prism_storage_pools"] = {
    "check_function": check_prism_storage_pools,
    "inventory_function": inventory_prism_storage_pools,
    "parse_function": parse_prism,
    "group": "filesystem",
    "has_perfdata": True,
    "service_description": "Storage Pool %s",
    "default_levels_variable": "filesystem_default_levels",
}
