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

# See https://utcc.utoronto.ca/~cks/space/blog/linux/NFSStaleUnmounting
# Output changes from
# knvmsapprd:/transreorg/sap/trans /transreorg/sap/trans nfs4 rw,relatime,vers=4.0,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.24.98.63,local_lock=none,addr=172.24.98.57 0 0
# to
# knvmsapprd:/transreorg/sap/trans /transreorg/sap/trans\040(deleted) nfs4 rw,relatime,vers=4.0,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.24.98.63,local_lock=none,addr=172.24.98.57 0 0


def inventory_mounts(info):
    inventory = []
    devices = []
    for dev, mp, fstype, options, dump, fsck in info:
        if fstype not in [ 'tmpfs' ] and dev not in devices:
            devices.append(dev)
            opts = options.split(",")
            opts.sort()
            inventory.append( (mp.replace("\\040(deleted)", ""), opts) )
    return inventory


def check_mounts(item, targetopts, info):
    # Ignore options that are allowed to change
    def should_ignore_option(option):
        for ignored_option in [ "commit=", "localalloc=", "subvol=", "subvolid=" ]:
            if option.startswith(ignored_option):
                return True
        return False

    for dev, mp, fstype, options, dump, fsck in info:
        if item == mp.replace("\\040(deleted)", ""):
            if mp.endswith("\\040(deleted)"):
                return 1, "Mount point detected as stale"

            opts = options.split(",")
            # Now compute the exact difference.

            exceeding = []
            for o in opts:
                if o not in targetopts and not should_ignore_option(o):
                    exceeding.append(o)

            missing = []
            for o in targetopts:
                if o not in opts and not should_ignore_option(o):
                    missing.append(o)

            if not missing and not exceeding:
                return 0, "Mount options exactly as expected"

            infos = []
            if missing:
                infos.append("Missing: %s" % ",".join(missing))
            if exceeding:
                infos.append("Exceeding: %s" % ",".join(exceeding))
            infotext = ", ".join(infos)

            if "ro" in exceeding:
                return 2, "Filesystem has switched to read-only and is probably corrupted(!!), %s" % infotext

            # Just warn in other cases
            return 1, infotext

    return 3, "Filesystem not mounted"


check_info["mounts"] = {
    'check_function':          check_mounts,
    'inventory_function':      inventory_mounts,
    'service_description':     'Mount options of %s',
    'group':                   'fs_mount_options',
}
