use flags for check_nfs_shares

This commit is contained in:
Cyberes 2024-06-22 20:02:36 -06:00
parent 1a4f3f5ffd
commit cc880e8514
1 changed files with 21 additions and 20 deletions

View File

@ -1,40 +1,42 @@
#!/bin/bash
# Function to print help
print_help() {
echo "Usage: $0 <host> <shares>"
echo "Usage: $0 -h <host> -s <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 "-h The IP address or hostname of the NFS server."
echo "-s 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
if [ "$#" -ne 4 ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
print_help
fi
# Assign arguments to variables
host=$1
shares=$2
while getopts "h:s:" opt; do
case ${opt} in
h )
host=$OPTARG
;;
s )
shares=$OPTARG
;;
\? )
print_help
;;
esac
done
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)
IFS=',' read -r -a share_array <<< "$shares"
# Check if the share is found in the output
for share in "${share_array[@]}"; do
output=$(showmount -e $host | grep $share)
if [[ $output != *"$share"* ]]; then
echo "CRITICAL - $share is not exported on $host"
exit 2
@ -42,7 +44,6 @@ for share in "${share_array[@]}"; do
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