#!/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 inventory_hr_fs(checktype, info):
    inventory = []
    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        if hrtype in [ ".1.3.6.1.2.1.25.2.1.4" ] and \
                hrdescr not in inventory_df_exclude_mountpoints and \
                saveint(hrsize) != 0:
            inventory.append((hrdescr, "filesystem_default_levels"))
    return inventory

def check_hr_fs(item, params, info):
    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        if item == hrdescr:
            unit_size = saveint(hrunits)
            size      = saveint(hrsize) * unit_size
            if size == 0:
                return (0, "OK - mounted, but size is zero")
            used      = saveint(hrused) * unit_size
            size_mb   = size / 1048576.0
            size_gb   = size_mb / 1024.0
            used_mb   = used / 1048576.0
            used_perc = 100 * (used_mb / size_mb)

            warn_mb, crit_mb, levelstext = get_filesystem_levels(g_hostname, item, size_gb, params)
            perfdata = [(item, str(used_mb) + 'MB', warn_mb, crit_mb, 0, size_mb)]
            used_txt = "%d%% used (%.2f of %.2f GB)" % (used_perc, used_mb/1024, size_mb/1024)
            if used_mb > crit_mb:
                return (2, "CRIT - %s %s" % (used_txt, levelstext), perfdata)
            elif used_mb >= warn_mb:
                return (1, "WARN - %s %s" % (used_txt, levelstext), perfdata)
            else:
                return (0, "OK - %s %s" % (used_txt, levelstext), perfdata)

    return (3, "UNKNOWN - no such filesystem found in SNMP output")


check_info["hr_fs"] = (check_hr_fs, "fs_%s", 1, inventory_hr_fs)
snmp_info["hr_fs"] = ( ".1.3.6.1.2.1.25.2.3.1", [
    2, # hrStorageType
    3, # hrStorageDescr
    4, # hrStorageAllocationUnits
    5, # hrStorageSize
    6, # hrStorageUsed
] )
snmp_scan_functions["hr_fs"] = lambda oid: \
        not not oid('.1.3.6.1.2.1.25.1.1.0') # HOST-RESOURCES-MIB::hrSystemUptime.0
