#!/usr/bin/env bash # Wait for two consecutive low-queue samples across the SSH inventory. set -euo pipefail export LC_ALL=C targets=() mappings=() identity="" budget=60 validate_only=false while (($#)); do case "$1" in --validate-only) validate_only=true; shift; continue ;; --target|--devices|--identity-file|--timeout-secs) [[ $# -ge 2 ]] || { echo "missing value for $1" >&2; exit 2; } case "$1" in --target) targets+=("$2") ;; --devices) mappings+=("$2") ;; --identity-file) identity="$2" ;; --timeout-secs) budget="$2" ;; esac shift 2 ;; *) echo "unknown disk queue option: $1" >&2; exit 2 ;; esac done if ! [[ "$budget" =~ ^[1-9][0-9]{0,5}$ ]] || ((budget < 11)); then echo "disk queue timeout must be at least 11 seconds" >&2 exit 2 fi (("${#targets[@]}" > 0 && "${#targets[@]}" == "${#mappings[@]}")) || { echo "disk queue check requires one device mapping per SSH target" >&2; exit 2; } nodes="" hosts="" device_lists=() for target in "${targets[@]}"; do [[ "$target" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*=[A-Za-z0-9][A-Za-z0-9@._:%+-]*$ ]] || { echo "invalid disk queue SSH target" >&2; exit 2; } node="${target%%=*}" host="${target#*=}" [[ "$nodes" != *" $node "* ]] || { echo "duplicate disk queue node: $node" >&2; exit 2; } [[ "$hosts" != *" $host "* ]] || { echo "duplicate disk queue SSH host: $host" >&2; exit 2; } hosts="$hosts $host " nodes="$nodes $node " matches=0 for mapping in "${mappings[@]}"; do [[ "${mapping%%=*}" == "$node" ]] || continue [[ "$mapping" =~ ^[^=]+=([A-Za-z0-9][A-Za-z0-9._-]*)(\+[A-Za-z0-9][A-Za-z0-9._-]*)*$ ]] || { echo "invalid device list for $node" >&2; exit 2; } devices="${mapping#*=}" if ! awk -v devices="$devices" 'BEGIN { n=split(devices,d,"+"); for(i=1;i<=n;i++) if(seen[d[i]]++) exit 1 }'; then echo "duplicate disk queue device for $node" >&2; exit 2 fi device_lists+=("${devices//+/ }") matches=$((matches+1)) done [[ "$matches" == 1 ]] || { echo "expected one device mapping for $node" >&2; exit 2; } done [[ "$validate_only" == false ]] || exit 0 ssh_args=(-o BatchMode=yes -o ConnectTimeout=5) if [[ -n "$identity" ]]; then ssh_args+=(-i "$identity"); fi artifacts=$(mktemp -d) pids=() cleanup() { local pid if ((${#pids[@]})); then kill -TERM "${pids[@]}" 2>/dev/null || true for pid in "${pids[@]}"; do wait "$pid" 2>/dev/null || true; done fi rm -rf -- "$artifacts" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM # Only validated device names are placed in the SSH command. The sampler uses # monotonic uptime and the weighted I/O milliseconds from /proc/diskstats. sample=$(cat <<'REMOTE' set -eu export LC_ALL=C snapshot() { awk -v devices="$devices" ' BEGIN { n=split(devices,d," "); for(i=1;i<=n;i++) want[d[i]]=1 } FNR==NR { t=$1; next } $3 in want { if (NF<14 || $14 !~ /^[0-9]+$/) exit 2 print $3,t,$14 got++ } END { if (got!=n) exit 2 } ' /proc/uptime /proc/diskstats } before=$(snapshot) sleep 5 after=$(snapshot) printf '%s\n--\n%s\n' "$before" "$after" | awk ' $0=="--" { second=1; next } !second { if ($1 in t) bad=1; t[$1]=$2; w[$1]=$3; count++; next } { if (!($1 in t) || seen[$1]++ || $2<=t[$1] || $3peak)peak=q; got++ } END { if (bad||!count||got!=count) exit 2; printf "%.6f %.6f\n",sum/count,peak } ' REMOTE ) end=$((SECONDS+budget)) streak=0 sample_number=0 printf 'sample\tnode\tmean_queue\tpeak_queue\n' while ((end-SECONDS >= 5)); do sample_number=$((sample_number+1)) remaining=$((end-SECONDS)) for i in "${!targets[@]}"; do timeout --foreground --kill-after=1s "${remaining}s" ssh "${ssh_args[@]}" -- "${targets[i]#*=}" \ "devices='${device_lists[i]}'; $sample" >"$artifacts/$i" & pids+=("$!") done valid=true for pid in "${pids[@]}"; do if ! wait "$pid"; then valid=false; fi done pids=() if [[ "$valid" != true ]]; then echo "disk queue sample failed or timed out" >&2 exit 1 fi quiet=true for i in "${!targets[@]}"; do result=$(cat "$artifacts/$i") if [[ ! "$result" =~ ^([0-9]+\.[0-9]+)\ ([0-9]+\.[0-9]+)$ ]]; then echo "invalid disk queue sample from ${targets[i]%%=*}" >&2; exit 1 fi mean="${BASH_REMATCH[1]}" peak="${BASH_REMATCH[2]}" printf '%s\t%s\t%s\t%s\n' "$sample_number" "${targets[i]%%=*}" "$mean" "$peak" if ! awk -v mean="$mean" -v peak="$peak" 'BEGIN {exit !(mean<=0.25 && peak<=0.75)}'; then quiet=false; fi done if [[ "$quiet" == true ]]; then streak=$((streak+1)) if ((streak == 2 && SECONDS <= end)); then exit 0; fi else streak=0 fi done echo "disk queues did not settle within ${budget}s" >&2 exit 1