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

#
# Example Output:
# .1.3.6.1.4.1.5951.4.1.1.41.8.1.1.4.47.118.97.114  "/var"
# .1.3.6.1.4.1.5951.4.1.1.41.8.1.1.6.47.102.108.97.115.104  "/flash"
# .1.3.6.1.4.1.5951.4.1.1.41.8.1.2.4.47.118.97.114  96133
# .1.3.6.1.4.1.5951.4.1.1.41.8.1.2.6.47.102.108.97.115.104  7976
# .1.3.6.1.4.1.5951.4.1.1.41.8.1.3.4.47.118.97.114  87418
# .1.3.6.1.4.1.5951.4.1.1.41.8.1.3.6.47.102.108.97.115.104  7256


factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def inventory_df_netscaler(info):
    mplist = []
    for mp, size_mb, _avail_mb in info:
        if int(size_mb) > 0 and mp not in inventory_df_exclude_mountpoints:
            mplist.append(mp)
    return df_discovery(host_extra_conf(host_name(), filesystem_groups), mplist)


def check_df_netscaler(item, params, info):
    fslist = []
    for mp, size_mb, avail_mb in info:
        if "patterns" in params or item == mp:
            fslist.append((mp, int(size_mb), int(avail_mb), 0))
    return df_check_filesystem_list(item, params, fslist)


check_info["df_netscaler"] = {
    "check_function": check_df_netscaler,
    "inventory_function": inventory_df_netscaler,
    "default_levels_variable": "filesystem_default_levels",
    "service_description": "Filesystem %s",
    "has_perfdata": True,
    "group": "filesystem",
    "snmp_info": (
        ".1.3.6.1.4.1.5951.4.1.1.41.8.1",
        [
            1,  # sysHealthDiskName
            2,  # sysHealthDiskSize
            3,  # sysHealthDiskAvail
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.5951.1"),
}
