Last active
March 10, 2026 06:31
-
-
Save bachmanity1/78db850b7ed640ff622b1f2dd8351d9a 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 --format json) | |
| failed=() | |
| for host in "${HOSTS[@]}"; do | |
| weight=$(echo "$crush_tree" | jq --arg h "$host" ' | |
| [.. | objects | select(.type == "host" and .name == $h) | .. | objects | select(.type == "osd") | .crush_weight] | add // 0 | |
| ') | |
| if awk "BEGIN{exit !($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