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

# emc_isilon_quota_types = {
#     0: "default_user",
#     1: "user",
#     2: "default-group",
#     3: "group",
#     4: "directory",
#     5: "special",
#     6: "max"
# }
from cmk.base.plugins.agent_based.utils.df import df_discovery


def inventory_emc_isilon_quota(info):
    mount_points = []

    for (
        path,
        _hard_threshold,
        _soft_threshold_defined,
        _soft_threshold,
        _advisory_threshold_defined,
        _advisory_threshold,
        _usage,
    ) in info:
        mount_points.append(path)

    return df_discovery(host_extra_conf(host_name(), filesystem_groups), mount_points)


def check_emc_isilon_quota(item, params, info):
    byte_to_mb = float(1024 * 1024)

    fslist_blocks = []

    for (
        path,
        hard_threshold,
        soft_threshold_defined,
        soft_threshold,
        advisory_threshold_defined,
        advisory_threshold,
        usage,
    ) in info:

        if "patterns" in params or item == path:
            # note 1: quota type is currently unused
            # note 2: even if quotaHardThresholdDefined is 0 the
            #         quotaHardThreshold is still often set to a reasonable value. Otherwise,
            #         use the "hardest" threshold that isn't 0 for the disk limit
            hard_threshold = int(hard_threshold)  # can't be exceeded
            soft_threshold = int(soft_threshold)  # write-protect after grace period
            advisory_threshold = int(advisory_threshold)  # warning only
            assumed_size = hard_threshold or soft_threshold or advisory_threshold

            # If the users has not configured levels, we use the soft/advisory threshold
            if "levels" not in params and (
                soft_threshold_defined == "1" or advisory_threshold_defined == "1"
            ):
                # if a soft threshold is set, it will be used as crit (since the hard_threshold can't be
                # exceeded anyway)
                crit = soft_threshold or (assumed_size * 0.9)
                # if a advisory threshold is set, it will be used as warning level
                warn = advisory_threshold or (assumed_size * 0.8)

                params = params.copy()
                params["levels"] = (warn * 100.0) / assumed_size, (crit * 100.0) / assumed_size

            avail = assumed_size - int(usage)
            fslist_blocks.append(
                (path, assumed_size / byte_to_mb, avail / byte_to_mb, 0)
            )  # fixed: true-division

    return df_check_filesystem_list(item, params, fslist_blocks)


check_info["emc_isilon_quota"] = {
    "check_function": check_emc_isilon_quota,
    "inventory_function": inventory_emc_isilon_quota,
    "service_description": "Quota %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.12124.1.12.1.1",
        [
            5,  # quotaPath
            7,  # quotaHardThreshold
            8,  # quotaSoftThresholdDefined
            9,  # quotaSoftThreshold
            10,  # quotaAdvisoryThresholdDefined
            11,  # quotaAdvisoryThreshold
            13,  # quotaUsage
            # 2,    # quotaType
            # 6,    # quotaHardThresholdDefined
            # 14,   # quotaUsageWithOverhead
            # 16,   # quotaIncludesOverhead
            # 12,   # quotaGracePeriod
            # 15,   # quotaInodeUsage  # apparently doesn't provide the total
            # number of inodes, so this is
            # useless to us
        ],
    ),
    "snmp_scan_function": lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "group": "filesystem",
    "default_levels_variable": "emc_isilon_quota",
}
