#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# emc_isilon_quota_types = {
#     0: "default_user",
#     1: "user",
#     2: "default-group",
#     3: "group",
#     4: "directory",
#     5: "special",
#     6: "max"
# }


def inventory_emc_isilon_quota(info):
    mount_points = []

    for path, _hard_threshold, _soft_threshold_defined, _soft_threshold, \
        _advisory_threshold_defined, _advisory_threshold, _usage in info:
        mount_points.append(path)

    return df_inventory(mount_points)


def check_emc_isilon_quota(item, params, info):
    byte_to_mb = float(1024 * 1024)

    fslist_blocks = []

    for path, hard_threshold, soft_threshold_defined, soft_threshold, \
        advisory_threshold_defined, advisory_threshold, usage in info:

        if "patterns" in params or item == path:
            # note 1: quota type is currently unused
            # note 2: even if quotaHardThresholdDefined is 0 the
            #         quotaHardThreshold is still often set to a reasonable value. Otherwise,
            #         use the "hardest" threshold that isn't 0 for the disk limit
            hard_threshold = int(hard_threshold)  # can't be exceeded
            soft_threshold = int(soft_threshold)  # write-protect after grace period
            advisory_threshold = int(advisory_threshold)  # warning only
            assumed_size = hard_threshold or soft_threshold or advisory_threshold

            # If the users has not configured levels, we use the soft/advisory threshold
            if "levels" not in params and (soft_threshold_defined == '1' or
                                           advisory_threshold_defined == '1'):
                # if a soft threshold is set, it will be used as crit (since the hard_threshold can't be
                # exceeded anyway)
                crit = soft_threshold or (assumed_size * 0.9)
                # if a advisory threshold is set, it will be used as warning level
                warn = advisory_threshold or (assumed_size * 0.8)

                params = params.copy()
                params["levels"] = (warn * 100.0) / assumed_size, (crit * 100.0) / assumed_size

            avail = assumed_size - int(usage)
            fslist_blocks.append((path, assumed_size / byte_to_mb, avail / byte_to_mb, 0))

    return df_check_filesystem_list(item, params, fslist_blocks)


check_info["emc_isilon_quota"] = {
    "check_function": check_emc_isilon_quota,
    "inventory_function": inventory_emc_isilon_quota,
    "service_description": "Quota %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.12124.1.12.1.1",
        [
            5,  # quotaPath
            7,  # quotaHardThreshold
            8,  # quotaSoftThresholdDefined
            9,  # quotaSoftThreshold
            10,  # quotaAdvisoryThresholdDefined
            11,  # quotaAdvisoryThreshold
            13,  # quotaUsage
            # 2,    # quotaType
            # 6,    # quotaHardThresholdDefined
            # 14,   # quotaUsageWithOverhead
            # 16,   # quotaIncludesOverhead
            # 12,   # quotaGracePeriod
            # 15,   # quotaInodeUsage  # apparently doesn't provide the total
            # number of inodes, so this is
            # useless to us
        ]),
    "snmp_scan_function": lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "includes": ["size_trend.include", "df.include"],
    "group": "filesystem",
    "default_levels_variable": "emc_isilon_quota",
}
