Created
March 10, 2026 06:43
-
-
Save bachmanity1/535b98258609fa3bc4dabbfd7f6ca675 to your computer and use it in GitHub Desktop.
Check if Ceph CRUSH weight is zero for hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| HOST_FILE="" | |
| HOSTS=() | |
| usage() { | |
| echo "Usage: $0 [--file <hosts-file>] [hostname ...]" | |
| echo "" | |
| echo "Checks if CRUSH weight is zero for each specified host." | |
| exit 1 | |
| } | |
| [[ $# -lt 1 ]] && usage | |
| [[ "$1" == "--help" || "$1" == "-h" ]] && usage | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| --file|-f) HOST_FILE="$2"; shift 2 ;; | |
| --help|-h) usage ;; | |
| *) HOSTS+=("$1"); shift ;; | |
| esac | |
| done | |
| if [[ -n "$HOST_FILE" ]]; then | |
| if [[ ! -f "$HOST_FILE" ]]; then | |
| echo "ERROR: file not found: $HOST_FILE" | |
| exit 1 | |
| fi | |
| while IFS= read -r line; do | |
| line="${line%%#*}" | |
| line="$(echo "$line" | xargs)" | |
| [[ -n "$line" ]] && HOSTS+=("$line") | |
| done < "$HOST_FILE" | |
| fi | |
| if [[ ${#HOSTS[@]} -eq 0 ]]; then | |
| echo "ERROR: no hosts specified" | |
| usage | |
| fi | |
| crush_tree=$(ceph osd crush tree) | |
| failed=() | |
| for host in "${HOSTS[@]}"; do | |
| weight=$(echo "$crush_tree" | awk -v h="$host" '$0 ~ "host " h {print $2}') | |
| if [[ -z "$weight" ]]; then | |
| echo "FAIL: $host — not found in crush tree" | |
| failed+=("$host") | |
| elif [[ "$weight" != "0" ]]; then | |
| echo "FAIL: $host — crush weight $weight" | |
| failed+=("$host") | |
| else | |
| echo "OK: $host — crush weight 0" | |
| fi | |
| done | |
| if [[ ${#failed[@]} -gt 0 ]]; then | |
| echo "" | |
| echo "Hosts with non-zero weight: ${failed[*]}" | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment