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


# .1.3.6.1.2.1.25.2.3.1.2.1 .1.3.6.1.2.1.25.2.1.2 --> HOST-RESOURCES-MIB::hrStorageType.1
# .1.3.6.1.2.1.25.2.3.1.2.3 .1.3.6.1.2.1.25.2.1.3 --> HOST-RESOURCES-MIB::hrStorageType.3
# .1.3.6.1.2.1.25.2.3.1.3.1 Physical memory --> HOST-RESOURCES-MIB::hrStorageDescr.1
# .1.3.6.1.2.1.25.2.3.1.3.3 Virtual memory --> HOST-RESOURCES-MIB::hrStorageDescr.3
# .1.3.6.1.2.1.25.2.3.1.4.1 1024 --> HOST-RESOURCES-MIB::hrStorageAllocationUnits.1
# .1.3.6.1.2.1.25.2.3.1.4.3 1024 --> HOST-RESOURCES-MIB::hrStorageAllocationUnits.3
# .1.3.6.1.2.1.25.2.3.1.5.1 8122520 --> HOST-RESOURCES-MIB::hrStorageSize.1
# .1.3.6.1.2.1.25.2.3.1.5.3 21230740 --> HOST-RESOURCES-MIB::hrStorageSize.3
# .1.3.6.1.2.1.25.2.3.1.6.1 7749124 --> HOST-RESOURCES-MIB::hrStorageUsed.1
# .1.3.6.1.2.1.25.2.3.1.6.3 7749124 --> HOST-RESOURCES-MIB::hrStorageUsed.3


# Juniper devices put information about the device into the
# field where we expect the mount point. Ugly. Remove that crap.
def fix_hr_fs_mountpoint(mp):
    mp = mp.replace("\\", "/")
    if "mounted on:" in mp:
        return mp.rsplit(":",1)[-1].strip()
    elif "Label:" in mp:
        pos = mp.find("Label:")
        return mp[:pos].rstrip()
    else:
        return mp


def inventory_hr_fs(info):
    mplist = []
    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        hrdescr = fix_hr_fs_mountpoint(hrdescr)
        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:
            mplist.append(hrdescr)
    return df_inventory(mplist)


def check_hr_fs(item, params, info):
    fslist = []

    if "Label:" in item or "\\" in item:
        return 3, "check had an incompatible change, please re-discover services on this host"

    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        hrdescr = fix_hr_fs_mountpoint(hrdescr)
        if "patterns" in params or item == hrdescr:
            unit_size = saveint(hrunits)
            hrsize = saveint(hrsize)
            if hrsize < 0:
                hrsize = hrsize+2**32
            size      = hrsize * unit_size
            hrused = saveint(hrused)
            if hrused < 0:
                hrused = hrused+2**32
            used      = hrused * unit_size
            size_mb   = size / 1048576.0
            used_mb   = used / 1048576.0
            avail_mb  = size_mb - used_mb
            fslist.append((hrdescr, size_mb, avail_mb, 0))

    return df_check_filesystem_list(item, params, fslist)


check_info["hr_fs"] = {
    'check_function':          check_hr_fs,
    'inventory_function':      inventory_hr_fs,
    'service_description':     'Filesystem %s',
    'has_perfdata':            True,
    'snmp_info':               ('.1.3.6.1.2.1.25.2.3.1', [
                                    2, # hrStorageType
                                    3, # hrStorageDescr
                                    4, # hrStorageAllocationUnits
                                    5, # hrStorageSize
                                    6, # hrStorageUsed
                               ]),
    # HOST-RESOURCES-MIB::hrSystemUptime.0
    'snmp_scan_function':      lambda oid: \
         not not oid('.1.3.6.1.2.1.25.1.1.0'),
    'group':                   'filesystem',
    'default_levels_variable': 'filesystem_default_levels',
    'includes':                [ "df.include" ],
}
