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

# Example output from agent:
# [zmucvm99-lds]
# accessible  True
# capacity    578478407680
# freeSpace   388398841856
# type    VMFS
# uncommitted 51973812224
# url /vmfs/volumes/513df1e9-12fd7366-ac5a-e41f13e69eaa


factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


def parse_esx_vsphere_datastores(info):
    stores = {}
    for line in info:
        if line[0].startswith("["):
            name = line[0][1:-1]
            store = {}
            stores[name] = store
        else:
            # Seems that the url attribute can have an empty value
            if len(line) == 1:
                key = line[0].strip()
                value = None
            else:
                key, value = line

            if key == "accessible":
                value = value.lower() == "true"
            elif key in ["capacity", "freeSpace", "uncommitted"]:
                value = int(value)
            store[key] = value
    return stores


@get_parsed_item_data
def check_esx_vsphere_datastores(item, params, data):
    if not data["accessible"]:
        yield 2, "inaccessible"

    mib = 1024.0**2
    size_bytes = data.get("capacity")
    avail_bytes = data.get("freeSpace")
    if size_bytes is None or size_bytes == 0 or avail_bytes is None:
        return

    yield df_check_filesystem_single(  # pylint: disable=undefined-variable
        item, size_bytes / mib, avail_bytes / mib, 0, None, None, params
    )  # fixed: true-division

    uncommitted_bytes = data.get("uncommitted")
    if uncommitted_bytes is None:
        return
    text_uncommitted = "Uncommitted: %s" % get_bytes_human_readable(uncommitted_bytes)
    yield 0, text_uncommitted, [("uncommitted", uncommitted_bytes / mib)]  # fixed: true-division

    used_bytes = size_bytes - avail_bytes
    prov_bytes = used_bytes + uncommitted_bytes
    prov_percent = (prov_bytes * 100.0) / size_bytes if size_bytes != 0 else 0

    warn, crit = params.get("provisioning_levels", (None, None))
    yield check_levels(
        prov_percent,
        None,
        (warn, crit),
        human_readable_func=get_percent_human_readable,
        infoname="Provisioning",
    )

    if prov_bytes > size_bytes:
        prov_used = used_bytes / prov_bytes * 100.0
        yield 0, f"{get_percent_human_readable(prov_used)} provisioned space used"

    if warn is not None:
        # convert percent to abs MiB
        scale = (size_bytes / mib) / 100.0  # fixed: true-division
        yield 0, "", [
            ("overprovisioned", prov_bytes / mib, scale * warn, scale * crit)
        ]  # fixed: true-division
    else:
        yield 0, "", [("overprovisioned", prov_bytes / mib)]  # fixed: true-division


check_info["esx_vsphere_datastores"] = {
    "parse_function": parse_esx_vsphere_datastores,
    "inventory_function": discover(),
    "check_function": check_esx_vsphere_datastores,
    "service_description": "Filesystem %s",
    "has_perfdata": True,
    "group": "esx_vsphere_datastores",
    "default_levels_variable": "filesystem_default_levels",
}

# .
