#!/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 from agent:
# <<<vms_diskstat>>>
# $1$DGA1122: TEST_WORK 1171743836 1102431184 0.00
# DSA1: SHAD_1 66048000 58815666 0.00
# DSA2: SHAD_2 66048000 47101824 0.07
# DSA3: SHAD_3 66048000 46137546 1.57
# DSA4: SHAD_4 66048000 36087093 0.00
# DSA5: SHAD_5 66048000 32449914 0.00
# $1$DGA1123: TEST_WORK 2171743836 1102431184 0.00
# $1$DGA1124: TEMP_02 3171743836 102431184 1.10
# $1$DGA1125: DATA_01 1171743836 202431184 0.20


factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def inventory_vms_diskstat_fs(info):
    return df_discovery(host_extra_conf(host_name(), filesystem_groups), [line[1] for line in info])


def check_vms_diskstat_fs(item, params, info):
    for line in info:
        if line[1] == item:
            _device, _label, size, used, _ios = line
            size_mb = int(size) * 512 / (1024.0 * 1024.0)
            avail_mb = int(used) * 512 / (1024.0 * 1024.0)
            return df_check_filesystem_single(item, size_mb, avail_mb, 0, None, None, params)

    return (3, "no such disk")


check_info["vms_diskstat.df"] = {
    "check_function": check_vms_diskstat_fs,
    "inventory_function": inventory_vms_diskstat_fs,
    "service_description": "Filesystem %s",
    "has_perfdata": True,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}
