Commit 851165a0 authored by Vitaly Lipatov's avatar Vitaly Lipatov

route-health.sh: use shared helpers from functions

Remove duplicated log/has_option, use read_value, ipcmd_for, table_by_name, resolve_default_gw from shared functions. Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent f6be4b15
...@@ -20,8 +20,6 @@ DOWN_THRESHOLD=2 ...@@ -20,8 +20,6 @@ DOWN_THRESHOLD=2
SHOW= SHOW=
[ "$1" = "--show" ] && SHOW=1 [ "$1" = "--show" ] && SHOW=1
log() { echo "$(date '+%H:%M:%S') $*" ; }
# --- Query InfluxDB for packet loss --- # --- Query InfluxDB for packet loss ---
HEALTH_DATA=$(mktemp) HEALTH_DATA=$(mktemp)
trap 'rm -f "$HEALTH_DATA"' EXIT trap 'rm -f "$HEALTH_DATA"' EXIT
...@@ -75,12 +73,6 @@ get_health() ...@@ -75,12 +73,6 @@ get_health()
}' }'
} }
# Check if option is set in group's options file
has_option()
{
[ -f "$1/options" ] && grep -q "^$2$" "$1/options" 2>/dev/null
}
# --- Evaluate health for all monitored groups --- # --- Evaluate health for all monitored groups ---
healthy_count=0 healthy_count=0
dead_count=0 dead_count=0
...@@ -94,7 +86,7 @@ for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do ...@@ -94,7 +86,7 @@ for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do
name=$(basename "$gwdir") name=$(basename "$gwdir")
[ -f "$gwdir/monitor" ] || continue [ -f "$gwdir/monitor" ] || continue
tag=$(grep -v '^#' "$gwdir/monitor" | grep -m1 .) tag=$(read_value "$gwdir/monitor")
[ -n "$tag" ] || continue [ -n "$tag" ] || continue
status=$(get_health "$tag") status=$(get_health "$tag")
...@@ -127,8 +119,7 @@ done ...@@ -127,8 +119,7 @@ done
# --- Manage default route for groups with set-default --- # --- Manage default route for groups with set-default ---
for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do
[ -d "$routes_dir" ] || continue [ -d "$routes_dir" ] || continue
ipcmd="ip" ipcmd=$(ipcmd_for "$routes_dir")
[ "$routes_dir" = "$ROUTES6_DIR" ] && ipcmd="ip -6"
for gwdir in "$routes_dir"/*/ ; do for gwdir in "$routes_dir"/*/ ; do
[ -d "$gwdir" ] || continue [ -d "$gwdir" ] || continue
...@@ -151,10 +142,9 @@ for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do ...@@ -151,10 +142,9 @@ for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do
for check_dir in "$routes_dir"/*/ ; do for check_dir in "$routes_dir"/*/ ; do
[ -d "$check_dir" ] || continue [ -d "$check_dir" ] || continue
[ -f "$check_dir/monitor" ] || continue [ -f "$check_dir/monitor" ] || continue
check_gw=$(grep -v '^#' "$check_dir/gateway" 2>/dev/null | grep -m1 .) check_ip=$(resolve_gw "$(read_value "$check_dir/gateway")" "$ipcmd" 2>/dev/null)
check_ip=$(resolve_gw "$check_gw" "$ipcmd" 2>/dev/null)
if [ "$check_ip" = "$gw_ip" ] ; then if [ "$check_ip" = "$gw_ip" ] ; then
gw_tag=$(grep -v '^#' "$check_dir/monitor" | grep -m1 .) gw_tag=$(read_value "$check_dir/monitor")
break break
fi fi
done done
...@@ -180,7 +170,7 @@ for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do ...@@ -180,7 +170,7 @@ for routes_dir in "$ROUTES_DIR" "$ROUTES6_DIR" ; do
continue continue
fi fi
cur_default=$($ipcmd route show default | awk '/default/ {print $3; exit}') cur_default=$(resolve_default_gw "$ipcmd")
if [ "$cur_default" != "$best_gw" ] ; then if [ "$cur_default" != "$best_gw" ] ; then
log "[$name] Switching default: $cur_default -> $best_gw" log "[$name] Switching default: $cur_default -> $best_gw"
if [ -z "$SHOW" ] ; then if [ -z "$SHOW" ] ; then
...@@ -202,9 +192,9 @@ for state_path in $dead_groups ; do ...@@ -202,9 +192,9 @@ for state_path in $dead_groups ; do
# Read table number from group config or rt_tables # Read table number from group config or rt_tables
table="" table=""
if [ -f "$state_path/table" ] ; then if [ -f "$state_path/table" ] ; then
table=$(grep -v '^#' "$state_path/table" | grep -m1 .) table=$(read_value "$state_path/table")
else else
table=$(awk -v n="$name" '$2 == n { print $1; exit }' /etc/iproute2/rt_tables 2>/dev/null) table=$(table_by_name "$name")
fi fi
# Flapping protection: require DOWN_THRESHOLD consecutive checks # Flapping protection: require DOWN_THRESHOLD consecutive checks
...@@ -231,9 +221,7 @@ for state_path in $dead_groups ; do ...@@ -231,9 +221,7 @@ for state_path in $dead_groups ; do
continue continue
fi fi
# Flush the routing table ipcmd=$(ipcmd_for "$routes_dir")
ipcmd="ip"
[ "$routes_dir" = "$ROUTES6_DIR" ] && ipcmd="ip -6"
log "[$name] Dead (${down_count}x confirmed), flushing table $table" log "[$name] Dead (${down_count}x confirmed), flushing table $table"
if [ -z "$SHOW" ] && [ -n "$table" ] ; then if [ -z "$SHOW" ] && [ -n "$table" ] ; then
......
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