#!/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_aggr:sep(9)>>>
# aggregation aggr0_root_n1       size-available 940666880        size-total 1793064960
# aggregation aggr0_root_n2       size-available 940556288        size-total 1793064960
# aggregation aggr1_n1    size-available 2915315712       size-total 9437184000
# aggregation aggr1_n2    size-available 2936561664       size-total 9437184000


def inventory_netapp_api_aggr(parsed):
    for name, aggr in parsed.items():
        if "size-total" in aggr and "size-available" in aggr:
            yield name, {}


@get_parsed_item_data
def check_netapp_api_aggr(item, params, aggr):
    if not ("size-total" in aggr and "size-available" in aggr):
        return
    mega = 1024.0 * 1024.0
    size_total = int(aggr.get("size-total")) / mega  # fixed: true-division
    size_avail = int(aggr.get("size-available")) / mega  # fixed: true-division
    return df_check_filesystem_list(item, params, [(item, size_total, size_avail, 0)])


check_info["netapp_api_aggr"] = {
    'check_function': check_netapp_api_aggr,
    'parse_function': netapp_api_parse_lines,
    'inventory_function': inventory_netapp_api_aggr,
    'service_description': 'Aggregation %s',
    'group': 'filesystem',
    'has_perfdata': True,
    'default_levels_variable': 'filesystem_default_levels',
}
