#!/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.hitachi_hnas 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
from cmk.base.plugins.agent_based.utils.df import df_discovery

factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def inventory_hitachi_hnas_span(info):
    mplist = []
    for id_, label, _total_upper, _total_lower, _used_upper, _used_lower in info:
        mplist.append(id_ + " " + label)
    return df_discovery(host_extra_conf(host_name(), filesystem_groups), mplist)


def check_hitachi_hnas_span(item, params, info):
    fslist = []
    for id_, label, total_upper, total_lower, used_upper, used_lower in info:
        if id_ + " " + label == item:
            size_mb = (int(total_upper) * 2**32 + int(total_lower)) / 1048576.0
            used_mb = (int(used_upper) * 2**32 + int(used_lower)) / 1048576.0
            avail_mb = size_mb - used_mb
            fslist.append((item, size_mb, avail_mb, 0))

            return df_check_filesystem_list(item, params, fslist)

    return 3, "Span %s not found" % item


check_info["hitachi_hnas_span"] = {
    "check_function": check_hitachi_hnas_span,
    "inventory_function": inventory_hitachi_hnas_span,
    "service_description": "Span %s",
    "has_perfdata": True,
    "snmp_info": (".1.3.6.1.4.1.11096.6.1.1.6.4.2.1", [1, 2, 3, 4, 5, 6]),
    # spanStatsSpanId, spanLabel, spanCapacityTotalUpper, spanCapacityTotalLower,
    # spanCapacityUsedUpper, spanCapacityUsedLower
    "snmp_scan_function": hitachin_hnas_scan_function,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}
