#!/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_qtree_quota:sep(9)>>>
# quota user01    quota-type user disk-limit 12288000 quota-users AD\aolov  volume vol_silber2_group_cifs   disk-used 0
# quota user01    quota-type user disk-limit 12288000 quota-users AD\bva    volume vol_silber2_group_cifs   disk-used 0
# quota user01    quota-type user disk-limit 12288000 quota-users AD\cclze    volume vol_silber2_group_cifs   disk-used 0
# quota fdo01   quota-type tree disk-limit 4294967296   volume vol_bronze1_fdo1 disk-used 3544121572
# quota fdo03   quota-type tree disk-limit 2684354560   volume vol_bronze1_fdo2 disk-used 788905236


def inventory_netapp_api_qtree_quota(parsed):
    for qtree, attrs in parsed.items():
        if attrs.get("quota-type") == "tree" and attrs.get("disk-limit").isdigit():
            yield qtree, {}


@get_parsed_item_data
def check_netapp_api_qtree_quota(item, params, qtree):
    disk_limit = qtree.get("disk-limit")
    if not disk_limit.isdigit():
        return 3, "Qtree has no disk limit set"

    size_total = int(disk_limit) / 1024.0
    size_avail = size_total - int(qtree.get("disk-used")) / 1024.0
    if qtree.get("files-used", "").isdigit() and qtree.get("file-limit", "").isdigit():
        inodes_total = int(qtree.get("file-limit"))
        inodes_avail = inodes_total - int(qtree.get("files-used"))
    else:
        inodes_total = None
        inodes_avail = None

    return df_check_filesystem_single(item, size_total, size_avail, 0, inodes_total, inodes_avail,
                                      params)


check_info["netapp_api_qtree_quota"] = {
    'check_function': check_netapp_api_qtree_quota,
    'inventory_function': inventory_netapp_api_qtree_quota,
    'parse_function': lambda info: netapp_api_parse_lines(info,
                                                          custom_keys=["quota", "quota-users"]),
    'service_description': 'Qtree %s',
    'has_perfdata': True,
    'group': "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}
