编写Shell脚本批量检查Linux主机存储挂载及可访问性
Batch Check Mount Points & Accessibility with pdsh
Alright, let's build a robust shell script using pdsh to batch check mount points and their accessibility across your Linux hosts. I'll walk you through the complete script and break down how each part works so you can tweak it to fit your exact needs.
Complete Script
#!/bin/bash # -------------------------- Configuration Section -------------------------- # Replace with your target hosts: can be a space-separated list or read from a file HOST_LIST="web01 db01 storage01 backup01" # Add all mount points you want to check to this array MOUNT_POINTS=( "/data" "/backup" "/shared_nfs" "/mnt/ssd_storage" ) # --------------------------------------------------------------------------- # Loop through each mount point to run checks across all hosts for mount_point in "${MOUNT_POINTS[@]}"; do echo "=== Starting check for mount point: *$mount_point* ===" # Use pdsh to execute remote check commands pdsh -w "$HOST_LIST" << 'REMOTE_CMD' TARGET_MOUNT="$1" # 1. Check if the mount point is actively mounted (using findmnt for reliability) if ! findmnt -n -o TARGET "$TARGET_MOUNT" > /dev/null 2>&1; then echo "ERROR: Not mounted" exit 1 fi # 2. Check if the mount point is accessible (adjust test condition as needed) # Use test -x for "can enter directory" (basic accessibility) # Use test -w for "can write to directory" if you need to verify write access if ! test -x "$TARGET_MOUNT"; then echo "ERROR: Not accessible (permission denied or invalid path)" exit 1 fi # If all checks pass echo "OK: Mounted and accessible" REMOTE_CMD # Parse pdsh output to highlight errors clearly | awk -v mp="$mount_point" '{ if ($0 ~ /ERROR:/) { # Extract hostname from pdsh's [host]: output format host = substr($1, 2, length($1)-2); # Extract error message sub(/^\[[^]]+\]: /, "", $0); printf "[FAIL] %s | Mount point: %s | Reason: %s\n", host, mp, $0; } }' echo "" # Add blank line between mount point checks for readability done echo "=== All checks completed ==="
Key Features & Explanations
- Reliable Mount Check: Uses
findmntinstead ofmount | grepbecause it's purpose-built for querying mount information, avoiding false positives from partial matches in mount output. - Customizable Accessibility Check: The script uses
test -xto verify you can enter the directory (basic accessibility). Swap this withtest -wif you need to confirm write access for a storage location. - Clear Error Output: Uses
awkto parse pdsh's default[hostname]: outputformat and print human-readable failure messages with the host, mount point, and reason. - Easy Configuration: Just update the
HOST_LISTandMOUNT_POINTSarray at the top to match your environment.
Usage Tips
- Prerequisites: Ensure
pdshis installed on your control machine, and you've set up passwordless SSH access to all target hosts (otherwise you'll be stuck entering passwords for each host). - Host List from File: If your hosts are stored in a text file (one per line), replace the
HOST_LISTline with:HOST_LIST=$(cat /path/to/your/hosts.txt) - Test First: Run a quick test on a single host to validate the check logic before batch running:
pdsh -w web01 "findmnt /data && test -x /data && echo OK"
内容的提问来源于stack exchange,提问作者Dr.Bake
相关产品推荐
相关产品推荐

