Commit 6065882a authored by Vitaly Lipatov's avatar Vitaly Lipatov

route-update.sh: add --verbose flag, fix ip rule check with numeric tables

- Add --verbose/-v flag with detailed logging at all decision points: group config, hash comparison, rule/route checks, resolve stats - Use ip -N (numeric) for rule show to avoid name vs number mismatch that could cause false-positive "no changes" when rules were missing Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent ea2f8f05
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Unified route updater: reads directory-based config from routes.d/ and routes6.d/ # Unified route updater: reads directory-based config from routes.d/ and routes6.d/
# Each subdirectory = route group with gateway/table files and .list symlinks # Each subdirectory = route group with gateway/table files and .list symlinks
# #
# Usage: route-update.sh [--force] [--resolve] [--show] [--set-rules] # Usage: route-update.sh [--force] [--resolve] [--show] [--verbose] [--set-rules]
# route-update.sh --add IP|DOMAIN GROUP # route-update.sh --add IP|DOMAIN GROUP
# route-update.sh --del IP|DOMAIN GROUP # route-update.sh --del IP|DOMAIN GROUP
# route-update.sh --flush GROUP # route-update.sh --flush GROUP
...@@ -19,6 +19,7 @@ FORCE= ...@@ -19,6 +19,7 @@ FORCE=
RESOLVE= RESOLVE=
SHOW= SHOW=
SET_RULES= SET_RULES=
VERBOSE=
# Parse arguments # Parse arguments
ACTION= ACTION=
...@@ -32,6 +33,7 @@ while [ -n "$1" ] ; do ...@@ -32,6 +33,7 @@ while [ -n "$1" ] ; do
--resolve) RESOLVE=1 ;; --resolve) RESOLVE=1 ;;
--show) SHOW=1 ;; --show) SHOW=1 ;;
--set-rules) SET_RULES=1 ;; --set-rules) SET_RULES=1 ;;
--verbose|-v) VERBOSE=1 ;;
--flush) FLUSH_GROUP="$2" ; shift ;; --flush) FLUSH_GROUP="$2" ; shift ;;
--add) ACTION=add ; ADD_DEL_TARGET="$2" ; ADD_DEL_GROUP="$3" ; shift 2 ;; --add) ACTION=add ; ADD_DEL_TARGET="$2" ; ADD_DEL_GROUP="$3" ; shift 2 ;;
--del) ACTION=del ; ADD_DEL_TARGET="$2" ; ADD_DEL_GROUP="$3" ; shift 2 ;; --del) ACTION=del ; ADD_DEL_TARGET="$2" ; ADD_DEL_GROUP="$3" ; shift 2 ;;
...@@ -42,6 +44,7 @@ done ...@@ -42,6 +44,7 @@ done
# Helpers # Helpers
log() { echo "$(date '+%H:%M:%S') $*" ; } log() { echo "$(date '+%H:%M:%S') $*" ; }
vlog() { [ -n "$VERBOSE" ] && log " $*" ; }
md5_lists() md5_lists()
{ {
...@@ -192,6 +195,7 @@ process_routes() ...@@ -192,6 +195,7 @@ process_routes()
local state="$routes_dir/$name" local state="$routes_dir/$name"
ensure_state_dir "$state" ensure_state_dir "$state"
vlog "[$name]$label table=$table pref=$pref gw=$gw dir=$gwdir"
# Collect .list files (follow symlinks) # Collect .list files (follow symlinks)
local lists=$(find -L "$gwdir" -maxdepth 1 -name '*.list' -type f 2>/dev/null | sort) local lists=$(find -L "$gwdir" -maxdepth 1 -name '*.list' -type f 2>/dev/null | sort)
...@@ -212,21 +216,29 @@ process_routes() ...@@ -212,21 +216,29 @@ process_routes()
if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/hash" ] ; then if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/hash" ] ; then
local saved_hash local saved_hash
read -r saved_hash < "$STATE_DIR/$state/hash" read -r saved_hash < "$STATE_DIR/$state/hash"
vlog "[$name]$label hash: saved=$saved_hash current=$current_hash"
if [ "$current_hash" = "$saved_hash" ] ; then if [ "$current_hash" = "$saved_hash" ] ; then
# Check if rules/routes were lost (e.g. after network restart) # Check if rules/routes were lost (e.g. after network restart)
local need_reload= local need_reload=
if ! $ipcmd rule show | grep -q "lookup $table" ; then local rule_count=$($ipcmd -N rule show | grep -c "lookup $table")
local route_count=$($ipcmd route show table "$table" 2>/dev/null | wc -l)
vlog "[$name]$label hash match, checking state: rules=$rule_count routes=$route_count"
if [ "$rule_count" = "0" ] ; then
need_reload=1 need_reload=1
elif [ "$($ipcmd route show table "$table" 2>/dev/null | wc -l)" = "0" ] ; then vlog "[$name]$label ip rule for table $table missing"
elif [ "$route_count" = "0" ] ; then
need_reload=1 need_reload=1
vlog "[$name]$label route table $table empty"
fi fi
if [ -n "$need_reload" ] ; then if [ -n "$need_reload" ] ; then
log "[$name]$label Rules/routes lost, forcing reload" log "[$name]$label Rules/routes lost, forcing reload"
else else
[ -n "$SHOW" ] && log "[$name]$label No changes (hash match), skipping" vlog "[$name]$label no changes, skipping"
continue continue
fi fi
fi fi
else
vlog "[$name]$label state: force=$FORCE resolve=$RESOLVE hash_exists=$([ -f "$STATE_DIR/$state/hash" ] && echo yes || echo no)"
fi fi
if [ -n "$RESOLVE" ] ; then if [ -n "$RESOLVE" ] ; then
...@@ -245,6 +257,7 @@ process_routes() ...@@ -245,6 +257,7 @@ process_routes()
ip_lists="$ip_lists $f" ip_lists="$ip_lists $f"
fi fi
done done
vlog "[$name]$label ip_lists:$(echo $ip_lists | wc -w) domain_lists:$(echo $domain_lists | wc -w)"
{ {
for f in $ip_lists ; do for f in $ip_lists ; do
...@@ -264,6 +277,7 @@ process_routes() ...@@ -264,6 +277,7 @@ process_routes()
} | sort -u > "$resolved_new" } | sort -u > "$resolved_new"
# Check if resolved IPs actually changed # Check if resolved IPs actually changed
vlog "[$name]$label resolved $(wc -l < "$resolved_new") IPs, checking against saved state"
if [ -z "$FORCE" ] && [ -z "$need_reload" ] && [ -f "$STATE_DIR/$state/resolved" ] ; then if [ -z "$FORCE" ] && [ -z "$need_reload" ] && [ -f "$STATE_DIR/$state/resolved" ] ; then
if diff -q "$STATE_DIR/$state/resolved" "$resolved_new" >/dev/null 2>&1 ; then if diff -q "$STATE_DIR/$state/resolved" "$resolved_new" >/dev/null 2>&1 ; then
# IPs same, but gateway may have changed # IPs same, but gateway may have changed
...@@ -295,7 +309,8 @@ process_routes() ...@@ -295,7 +309,8 @@ process_routes()
$ipcmd -batch - 2>&1 | grep -v "^$" | head -5 $ipcmd -batch - 2>&1 | grep -v "^$" | head -5
# Ensure ip rule exists # Ensure ip rule exists
if ! $ipcmd rule show | grep -q "lookup $table.*pref $pref" ; then if ! $ipcmd -N rule show | grep -q "lookup $table.*pref $pref" ; then
vlog "[$name]$label adding ip rule: lookup $table pref $pref"
$ipcmd rule add lookup "$table" pref "$pref" 2>/dev/null $ipcmd rule add lookup "$table" pref "$pref" 2>/dev/null
fi fi
...@@ -345,7 +360,7 @@ if [ -n "$SET_RULES" ] ; then ...@@ -345,7 +360,7 @@ if [ -n "$SET_RULES" ] ; then
[ -d "$gwdir" ] || continue [ -d "$gwdir" ] || continue
read_group_config "$gwdir" "$ipcmd" || continue read_group_config "$gwdir" "$ipcmd" || continue
pref=$(rule_pref "$table") pref=$(rule_pref "$table")
if ! $ipcmd rule show | grep -q "lookup $table.*pref $pref" ; then if ! $ipcmd -N rule show | grep -q "lookup $table.*pref $pref" ; then
$ipcmd rule add lookup "$table" pref "$pref" $ipcmd rule add lookup "$table" pref "$pref"
fi fi
done done
...@@ -355,6 +370,7 @@ fi ...@@ -355,6 +370,7 @@ fi
# --- Main --- # --- Main ---
[ -n "$SHOW" ] && log "Dry-run mode (no changes will be made)" [ -n "$SHOW" ] && log "Dry-run mode (no changes will be made)"
[ -n "$VERBOSE" ] && log "Verbose mode, state_dir=$STATE_DIR"
process_routes "$ROUTES_DIR" get_ipv4_list_bulk "ip" "" process_routes "$ROUTES_DIR" get_ipv4_list_bulk "ip" ""
process_routes "$ROUTES6_DIR" get_ipv6_list_bulk "ip -6" " (v6)" process_routes "$ROUTES6_DIR" get_ipv6_list_bulk "ip -6" " (v6)"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment