#!/bin/bash

# This is a plugin for nagios to parse check nfs server
# Written by AlexGedak@gmx.net


PROGNAME=$(basename $0)
VERSION="0.1"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
TIMEOUT="8"
unset HOSTNAME REGEX VERBOSE

verbose() {
    if [[ -n $VERBOSE ]]; then
        echo "$@"
    fi
}

print_usage() {
    echo "Usage: $PROGNAME -H HOSTNAME [-r REGEX] [-t TIMEOUT]"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
    echo 
    echo "Options:"
    echo "  -t, --timeout"
    echo "    Timeout for TCP check - showmount has it's own."
    echo "  -h, --help"
    echo "    This help"
    echo "  -H, --hostname"
    echo "    Host name argument" 
    echo "  -r, --regex"
    echo "    Regex all exports has to match." 
    echo "  -v, --verbose"
    echo "    Much mor output for debugging"
    echo "  -V, --version"
    echo "    Print out version and exit"
}
        
print_help() {
    print_version
    echo ""
    echo "$PROGNAME:"
    echo ""
    print_usage
    echo ""
    echo "$PROGNAME will check if a tcp port 2049 (nfs) is open, and showmount"
    echo "found one or more exports."
    echo "With the regex you can control all mounts should match the given"
    echo "regex. In the example below all mounts should be exports only for"
    echo "localhost."
    echo ""
    echo "Example: $PROGNAME -H localhost -r \"localhost:/.*\" -t 5"
    echo ""
}

print_version() {
    echo "$PROGNAME Version $VERSION"
}

verbose() {
    if [[ -n $VERBOSE ]]; then
        echo -e "$@"
        echo
    fi
}

# Main Function
check_nfs_server() {
    # First check if the nfs port is open
    # netcat knows a timeout
    if ! $NETCAT -w $TIMEOUT -zv $HOSTNAME 2049 >/dev/null 2>&1; then
        echo "Critical: No NFS Server availabel at $HOSTNAME"
        exit $STATE_CRITICAL
    fi
    verbose "$NETCAT -w $TIMEOUT -zv $HOSTNAME 2049 returned Status \"0\""
    # showmount does not have a timeout
    SHOWMOUNT_OUT="$(showmount -e $HOSTNAME | grep -v "^Export list for ")"
    if [[ -z $SHOWMOUNT_OUT ]]; then
        echo "Critical: No mounts availabe at $HOSTNAME"
        exit $STATE_CRITICAL
    fi
    verbose "$SHOWMOUNT -v $HOSTNAME | grep -v \"^Export list for \""
    verbose "$SHOWMOUNT_OUT"
    # With the regex we could test if all mounts:
    if [[ -n $REGEX ]];then
        verbose "Regex testing enabled"
        verbose "All LINES will be checked with: grep -E \"$REGEX\""
        while read LINE; do
            verbose "Checking line \"$LINE\" against regex \"$REGEX\""
            if ! echo "$LINE" | grep -E "$REGEX" >/dev/null 2>&1; then
                echo "Warning: Mount \"$LINE\" doesn't match regex"
                exit $STATE_WARNING
            fi
        done< <(echo "$SHOWMOUNT_OUT")
    fi
    echo "NFS Ok: $HOSTNAME exports a network filesystem"
}
    
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_version $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        -V)
            print_version $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        -H)
            HOSTNAME=$2
            shift
            ;;
        --hostname)
            HOSTNAME=$2
            shift
            ;;
        --regex)
            REGEX="$2"
            shift
            ;;
        -r)
            REGEX="$2"
            shift
            ;;
        --timeout)
            TIMEOUT=$2
            shift
            ;;
        -t)
            TIMEOUT=$2
            shift
            ;;
        --verbose)
            VERBOSE=1
            ;;
        -v)
            VERBOSE=1
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

if ! SHOWMOUNT="$(which showmount )";then 
    echo "No \"showmount\" found, please install it. \$PATH:$PATH"
    exit $STATE_UNKOWN
fi
if ! NETCAT="$(which netcat 2>/dev/null)";then 
    if ! NETCAT="$(which nc 2>/dev/null)";then
        echo "No \"netcat\" or \"nc\" found, please install one of them. \$PATH:$PATH"
        exit $STATE_UNKOWN
    fi
fi
if [[ -z $HOSTNAME ]]; then
    print_usage
    echo "Error: we need a HOSTNAME"
    exit $STATE_UNKOWN
fi

# Here is where the pepper grows:
check_nfs_server
