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

factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def inventory_datapower_fs(info):
    if info:
        # only discover filesystems with a defined total size > 0
        if saveint(info[0][0]) != 0:
            yield "Encrypted", {}
        if saveint(info[0][2]) != 0:
            yield "Unencrypted", {}
        if saveint(info[0][4]) != 0:
            yield "Temporary", {}
        if saveint(info[0][6]) != 0:
            yield "Internal", {}


def check_datapower_fs(item, params, info):
    if item == "Encrypted":
        i = 0
    elif item == "Unencrypted":
        i = 2
    elif item == "Temporary":
        i = 4
    elif item == "Internal":
        i = 6

    avail_mb = float(info[0][i])
    size_mb = float(info[0][i + 1])
    fslist = [(item, size_mb, avail_mb, 0)]

    return df_check_filesystem_list(item, params, fslist)


check_info["datapower_fs"] = {
    "inventory_function": inventory_datapower_fs,
    "check_function": check_datapower_fs,
    "service_description": "Filesystem %s",
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.14685.3.1.29",
        [
            "1",  # dpStatusFilesystemStatusFreeEncrypted
            "2",  # dpStatusFilesystemStatusTotalEncrypted
            "3",  # dpStatusFilesystemStatusFreeUnencrypted
            "4",  # dpStatusFilesystemStatusTotalUnencrypted
            "5",  # dpStatusFilesystemStatusFreeTemporary
            "6",  # dpStatusFilesystemStatusTotalTemporary
            "7",  # dpStatusFilesystemStatusFreeInternal
            "8",  # dpStatusFilesystemStatusTotalInternal
        ],
    ),
    "snmp_scan_function": scan_datapower,
}
