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

inventory_df_rules = []
inventory_df_exclude_fs = ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660']


def _filter_by_exclude_rule(inv_rule_excludes, iterable):
    """exclude these mount points (/tmp, /proc, whatever user wants)"""
    for df_block in iterable:
        if df_block[0] not in inv_rule_excludes:
            yield df_block


def _filter_by_type(volume_info, ignore_fs_types, never_ignore_mountpoints, iterable):

    for df_block in iterable:
        if volume_info[df_block[0]]["fs_type"] not in ignore_fs_types:
            yield df_block
            continue

        # Filesystem is not ignored, so check against mount point patterns
        for p in never_ignore_mountpoints:
            if p[0] == "~" and regex(p[1:]).match(df_block[0]):
                yield df_block
                continue

            if df_block[0] == p:
                yield df_block


def _format_with_description(description, mountpoint):
    """
    TODO: maybe do this only if description != mountpoint
    """
    return "%s %s" % (description, mountpoint)


def _get_item_list_from_blocks(df_blocks):
    """return the mountpoint"""  # TODO: fix this. Should include optional description
    return [df_block[0] for df_block in df_blocks]


def _is_grouped_item(params):
    return "patterns" in params


def inventory_df(parsed):
    inventory_options = host_extra_conf_merged(host_name(), inventory_df_rules)
    include_volume_name_settings = inventory_options.get("include_volume_name", False)

    if include_volume_name_settings is False:
        include_volume_name = False
        grouping_behaviour = "mountpoint"
    else:
        include_volume_name = True
        if include_volume_name_settings is True:
            # Legacy configuration without explicit grouping behaviour
            grouping_behaviour = "mountpoint"
        else:
            #  ( {'include_volume_name': (True, 'volume_name_and_mountpoint')}, [], ALL_HOSTS, {} ),
            grouping_behaviour = include_volume_name_settings[1]

    ignore_fs_types = inventory_options.get("ignore_fs_types", inventory_df_exclude_fs)
    never_ignore_mountpoints = inventory_options.get("never_ignore_mountpoints", [])

    (df_blocks, volume_info), df_inodes = parsed

    filtered_blocks = _filter_by_exclude_rule(inventory_df_exclude_mountpoints, df_blocks)
    filtered_blocks = _filter_by_type(volume_info, ignore_fs_types, never_ignore_mountpoints,
                                      filtered_blocks)

    #Always exclude filesystems below dockers local storage area
    #and also exclude docker mounts in containers which are reported
    #by the agent when the agent is executed in the container context
    filtered_blocks = (mp for mp in filtered_blocks if not mp[0].startswith("/var/lib/docker/"))

    mplist = _get_item_list_from_blocks(filtered_blocks)

    # Modify the df_blocks and df_inodes to include the volume name
    # and hand the data over to the inventory function, this enables grouping based on volume and mountpoint
    if include_volume_name and grouping_behaviour == "volume_name_and_mountpoint":
        df_blocks, df_inodes = _add_volume_name(df_blocks, df_inodes, volume_info, mplist)
        mplist = [x[0] for x in df_blocks]

    for mountpoint, params in df_inventory(mplist):
        if "patterns" in params:
            # Add the grouping_behaviour info to the discovered parameters of this service. With this information
            # the check can easily reconstruct the discovered grouping.
            params["grouping_behaviour"] = grouping_behaviour
        elif include_volume_name and grouping_behaviour == "mountpoint":
            # Somehow the user wanted to see the volume name in the service description,
            # but the grouping itself is based on the mountpoint only
            # => The df_inventory returns a list of mountpoints and mountpoint groups
            # Add the volume name as prefix for single mountpoints
            mountpoint = "%s %s" % (volume_info[mountpoint]["volume_name"], mountpoint)

        params["include_volume_name"] = include_volume_name
        yield mountpoint, params


# Legacy params
def _get_mountpoint_from_item(item, params, volume_info):
    volume_and_mp_to_mp = {
        ("%s %s" % (volume_info[mp]["volume_name"], mp)): mp for mp in volume_info
    }
    if "patterns" in params or item in volume_info:
        mountpoint = item
    elif item in volume_and_mp_to_mp:
        mountpoint = volume_and_mp_to_mp[item]
    else:
        mountpoint = item
    return mountpoint


def _add_volume_name(df_blocks, df_inodes, volume_info, only_mp=None):
    # Modify the df_blocks and df_inodes mount points to include the volume name
    df_blocks_volume = []
    df_inodes_volume = []
    for entry in df_blocks:
        if only_mp and entry[0] not in only_mp:
            continue
        # The parse function explictly creates a correspondin volume_info entry for each df_block
        mountpoint_with_volume = "%s %s" % (volume_info[entry[0]]["volume_name"], entry[0])
        df_blocks_volume.append((mountpoint_with_volume,) + entry[1:])
    for entry in df_inodes:
        if only_mp and entry[0] not in only_mp:
            continue
        # There is no guarantee that the inode mountpoint is in volume_info, check parse func
        if entry[0] not in volume_info:
            continue
        mountpoint_with_volume = "%s %s" % (volume_info[entry[0]]["volume_name"], entry[0])
        df_inodes_volume.append((mountpoint_with_volume,) + entry[1:])

    return df_blocks_volume, df_inodes_volume


def check_df(item, params, parsed):
    (df_blocks, volume_info), df_inodes = parsed

    if "include_volume_name" not in params:
        item = _get_mountpoint_from_item(item, params, volume_info)

    # Check if the item is not the actual mountpoint and modify the data accordingly
    elif params.get("include_volume_name"):
        if "patterns" not in params:
            # Single mountpoints with prefixed volume name
            item = _get_mountpoint_from_item(item, params, volume_info)
        elif params["grouping_behaviour"] == "volume_name_and_mountpoint":
            # Filesystem group
            # There is no need to modify the item name, since is a custom text  and not used in for pattern matching.
            # The "volume_name_and_mount" grouping option however, requires the modification of the df_blocks
            # and df_inodes variables. After the modification these variables also include the volume name
            # which will be used during for group matching
            df_blocks, df_inodes = _add_volume_name(df_blocks, df_inodes, volume_info)

    return df_check_filesystem_list_coroutine(item, params, df_blocks, df_inodes)


check_info['df'] = {
    "inventory_function": inventory_df,
    "check_function": check_df,
    "service_description": "Filesystem %s",
    "has_perfdata": True,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}
