#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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.

# <<<netapp_api_luns:sep(9)>>>
# lun /vol/iscsi_crm_dblogs/crm_dblogs_lu01   read-only false size 644286182400   vserver ISCSI_CRM   size-used 538924421120  online true volume iscsi_crm_dblogs

factory_settings["netapp_api_luns_default_levels"] = {
    "levels"          : (80.0, 90.0), # warn/crit in percent
    "trend_range"     : 24,
    "trend_perfdata"  : True,    # do send performance data for trends
}

def inventory_netapp_api_luns(parsed):
    for lun in parsed.keys():
        yield lun, {}

def check_netapp_api_luns(item, params, parsed):
    lun = parsed.get(item)

    if not lun:
        yield 3, "LUN not found in agent output"
        return

    if lun.get("online") != "true":
        yield 2, "LUN is offline"

    if lun.get("read-only") != "false":
        yield 1, "read-only is %s" % lun.get("read-only")

    mega = 1024.0 * 1024.0
    size_total = int(lun.get("size")) / mega
    size_avail = size_total - (int(lun.get("size-used")) / mega)

    if params.get("ignore_levels"):
        yield 0, "Total size: %s, Used space is ignored" % get_bytes_human_readable(size_total)
    else:
        yield df_check_filesystem_single(g_hostname, item, size_total, size_avail, 0, None, None, params)

def netapp_api_luns_item(name):
    return name.rsplit("/", 1)[-1]

check_info["netapp_api_luns"] = {
    'check_function'          : check_netapp_api_luns,
    'inventory_function'      : inventory_netapp_api_luns,
    'parse_function'          : lambda x: netapp_api_parse_lines(x,item_func = netapp_api_luns_item),
    'service_description'     : 'LUN %s',
    'has_perfdata'            : True,
    'group'                   : "netapp_luns",
    'includes'                : [ "df.include", "netapp_api.include" ],
    "default_levels_variable" : "netapp_api_luns_default_levels",
}
