icinga2-checks/check_nfs_shares.sh

51 lines
1.4 KiB
Bash
Raw Normal View History

#!/bin/bash
# Function to print help
print_help() {
echo "Usage: $0 <host> <shares>"
echo "Checks if the specified NFS shares are exported on the given host."
echo ""
echo "<host> The IP address or hostname of the NFS server."
echo "<shares> A comma-separated list of NFS shares to check. For example: '/mnt/test,/mnt/export'"
echo ""
echo "You can list the exported shares on an NFS server using the command 'showmount -e <host>'"
echo "This script assumes that 'showmount' is installed on the system where it is run."
exit 1
}
# Check if help is requested or the correct number of arguments is provided
if [ "$#" -ne 2 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
print_help
fi
# Assign arguments to variables
host=$1
shares=$2
share_count=0
# Convert comma-separated shares into an array
IFS=',' read -r -a share_array <<< "$shares"
# Flag to track if all shares are exported
all_ok=true
# Iterate over each share
for share in "${share_array[@]}"; do
# Use showmount to check if the share is exported
output=$(showmount -e $host | grep $share)
# Check if the share is found in the output
if [[ $output != *"$share"* ]]; then
echo "CRITICAL - $share is not exported on $host"
exit 2
fi
share_count=$((share_count+1))
done
# Print final status
if $all_ok; then
echo "OK - All specified shares are exported on $host | share_count=$share_count"
fi
exit 0