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

# <<<netapp_api_luns:sep(9)>>>
# lun /vol/iscsi_crm_dblogs/crm_dblogs_lu01   read-only false size 644286182400   vserver ISCSI_CRM   size-used 538924421120  online true volume iscsi_crm_dblogs

factory_settings["netapp_api_luns_default_levels"] = {
    "levels": (80.0, 90.0),  # warn/crit in percent
    "trend_range": 24,
    "trend_perfdata": True,  # do send performance data for trends
    "read_only": False,
}


def inventory_netapp_api_luns(parsed):
    for lun in parsed:
        yield lun, {}


@get_parsed_item_data
def check_netapp_api_luns(item, params, lun):
    if lun.get("online") != "true":
        yield 2, "LUN is offline"

    read_only = lun.get("read-only") == "true"
    if read_only != params.get("read_only"):
        expected = str(params.get("read_only")).lower()
        yield 1, "read-only is %s (expected: %s)" % (lun.get("read-only"), expected)

    size_total_bytes = int(lun["size"])

    if params.get("ignore_levels"):
        yield 0, "Total size: %s, Used space is ignored" % get_bytes_human_readable(
            size_total_bytes
        )
    else:
        # df_check_filesystem_single expects input in Megabytes
        mega = 1024.0 * 1024.0
        size_avail_bytes = size_total_bytes - int(lun["size-used"])
        yield df_check_filesystem_single(
            item,
            size_total_bytes / mega,
            size_avail_bytes / mega,
            0,
            None,
            None,
            params,
        )


def netapp_api_luns_item(name, _values):
    return name.rsplit("/", 1)[-1]


check_info["netapp_api_luns"] = {
    "check_function": check_netapp_api_luns,
    "inventory_function": inventory_netapp_api_luns,
    "parse_function": lambda x: netapp_api_parse_lines(x, item_func=netapp_api_luns_item),
    "service_description": "LUN %s",
    "has_perfdata": True,
    "group": "netapp_luns",
    "default_levels_variable": "netapp_api_luns_default_levels",
}
