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