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


def check_df_netapp(volume, params, info):
    # info columns: NAME SIZE USED
    infolines = [ l for l in info if l[0] == volume ]
    if len(infolines) == 0:
        return (2, "CRIT - volume %s is missing" % volume )
    elif len(infolines) > 1:
        return (3, "UNKNOWN - Volume listed more than once!")

    size_kb = int(infolines[0][1])
    used_kb = int(infolines[0][2])
    size_mb = float(size_kb) / 1024
    used_mb = float(used_kb) / 1024
    size_gb = float(size_mb) / 1024
    used_gb = float(used_mb) / 1024

    if size_kb == 0:
        return (3, "UNKNOWN - Filesystem has size 0!")

    elif size_kb < 0 or used_kb < 0:
        return (3, "UNKNOWN - negative size or usage (32 bit SNMP counter wrap!)")

    warn_mb, crit_mb, levelstext = get_filesystem_levels(g_hostname, volume, size_gb, params)

    used_perc = int(round(float(used_kb) / float(size_kb) * 100))
    infotext = "%d%% used (%.1fG of %.1fG), %s" % (used_perc, used_gb, size_gb, levelstext)
    perfdata = [(volume, "%.2fMB" % used_mb, warn_mb, crit_mb, 0, size_mb)]
    if used_mb >= crit_mb:
        return (2, "CRIT - " + infotext, perfdata)
    elif used_mb >= warn_mb:
        return (1, "WARNING - " + infotext, perfdata)
    else:
        return (0, "OK - " + infotext, perfdata)

def inventory_df_netapp(checkname, info):
    inventory = []
    for volume, size_kb, used_kb in info:
        if int(size_kb) > 0: # Exclude filesystems with zero size (some snapshots)
            inventory.append( (volume, "%.1fG" % (float(size_kb)/1024/1024), inventory_df_check_params) )
    return inventory


check_info['df_netapp']        = (check_df_netapp, "fs_%s", 1, inventory_df_netapp)
snmp_info['df_netapp']         = ("1.3.6.1.4.1.789.1.5.4.1", [ 2, 29, 30 ] )
precompile_params['df_netapp'] = precompile_filesystem_levels
