Commit 99f76df1 authored by Vitaly Lipatov's avatar Vitaly Lipatov

router: derive monitor tag from PTR, fix metric gateway parsing

- Add gw_monitor_tag(): derives InfluxDB tag from gateway PTR record (NAME.egw.etersoft.ru → NAME, with .v6 suffix for IPv6) - find_gw_monitor: fallback to PTR-based tag when no monitor file - Fix find_gw_monitor: use parse_gw_line instead of resolve_gw to handle "IP metric N" format in gateway files - Fix build_route_via and load_list_routes multipath: use parse_gw_line loop instead of resolve_gw with raw gateway lines Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 7f0d2629
......@@ -70,20 +70,38 @@ get_health()
}'
}
# Find monitor tag for a gateway IP by scanning all groups
# Derive InfluxDB monitor tag from gateway IP via PTR record
# PTR format: NAME.egw.etersoft.ru → tag: NAME (+ .v6 suffix for IPv6)
# Usage: gw_monitor_tag GW_IP IPCMD
gw_monitor_tag()
{
local gw_ip="$1" ipcmd="$2"
local ptr=$(dig +short -x "$gw_ip" 2>/dev/null | sed 's/\.$//')
[ -z "$ptr" ] && return
local tag=$(echo "$ptr" | sed 's/\.egw\.etersoft\.ru$//')
[ "$tag" = "$ptr" ] && return # not in egw zone
[ "$ipcmd" = "ip -6" ] && tag="${tag}.v6"
echo "$tag"
}
# Find monitor tag for a gateway IP: first check group monitor files,
# then derive from PTR record
# Usage: find_gw_monitor ROUTES_DIR GW_IP IPCMD
find_gw_monitor()
{
local routes_dir="$1" gw_ip="$2" ipcmd="$3"
local routes_dir="$1" gw_arg="$2" ipcmd="$3"
# Check explicit monitor files in groups
for check_dir in "$routes_dir"/*/ ; do
[ -d "$check_dir" ] || continue
[ -f "$check_dir/monitor" ] || continue
local check_ip=$(resolve_gw "$(read_value "$check_dir/gateway")" "$ipcmd" 2>/dev/null)
if [ "$check_ip" = "$gw_ip" ] ; then
parse_gw_line "$(read_value "$check_dir/gateway")" "$ipcmd"
if [ "$gw_ip" = "$gw_arg" ] ; then
read_value "$check_dir/monitor"
return
fi
done
# Derive from PTR
gw_monitor_tag "$gw_arg" "$ipcmd"
}
# Flush per-list tables for a group
......
......@@ -237,18 +237,19 @@ build_route_via()
fi
[ -z "$table" ] && { echo "[$name] Cannot determine table number" >&2 ; return 1 ; }
# Build route suffix: single vs multipath
# Build route suffix: single gateway or multipath (non-metric only)
local gw_count=$(read_values "$dir/gateway" | wc -l)
if [ "$gw_count" -le 1 ] ; then
route_via="via $gw table $table"
else
route_via="table $table"
local g
for g in $(read_values "$dir/gateway") ; do
g=$(resolve_gw "$g" "$ipcmd")
[ -z "$g" ] && continue
route_via="$route_via nexthop via $g weight 1"
done
while IFS= read -r gw_line ; do
[ -z "$gw_line" ] && continue
echo "$gw_line" | grep -q '^#' && continue
parse_gw_line "$gw_line" "$ipcmd"
[ -z "$gw_ip" ] && continue
route_via="$route_via nexthop via $gw_ip weight 1"
done < "$dir/gateway"
fi
}
......@@ -506,12 +507,13 @@ load_list_routes()
route_via="via $_gw table $_table"
else
route_via="table $_table"
local g
for g in $(read_values "$_gwdir/gateway") ; do
g=$(resolve_gw "$g" "$_ipcmd")
[ -z "$g" ] && continue
route_via="$route_via nexthop via $g weight 1"
done
while IFS= read -r gw_line ; do
[ -z "$gw_line" ] && continue
echo "$gw_line" | grep -q '^#' && continue
parse_gw_line "$gw_line" "$_ipcmd"
[ -z "$gw_ip" ] && continue
route_via="$route_via nexthop via $gw_ip weight 1"
done < "$_gwdir/gateway"
fi
sed "s|^|route replace |; s|$| $route_via|" "$_resolved_new" | \
$_ipcmd -batch - 2>&1 | grep -v "^$" | head -5
......
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