fix: improve watchdog reliability
This commit is contained in:
@@ -24,6 +24,7 @@ PING_TARGETS="223.5.5.5 114.114.114.114"
|
||||
FAIL_THRESHOLD=3
|
||||
DHCP_AFTER_FAILURE=1
|
||||
LOG_FILE="/var/log/cups-watchdog/network.log"
|
||||
MAX_LOG_SIZE_KB=256
|
||||
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
# shellcheck disable=SC1090
|
||||
@@ -33,9 +34,29 @@ fi
|
||||
mkdir -p "$STATE_DIR"
|
||||
mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null || true
|
||||
|
||||
acquire_lock() {
|
||||
local lock_dir="$STATE_DIR/network.lock"
|
||||
|
||||
if ! mkdir "$lock_dir" 2>/dev/null; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') [network-watchdog] another run is active"
|
||||
exit 0
|
||||
fi
|
||||
trap 'rmdir "$lock_dir" 2>/dev/null || true' EXIT
|
||||
}
|
||||
|
||||
rotate_log() {
|
||||
local max_bytes=$((MAX_LOG_SIZE_KB * 1024))
|
||||
|
||||
if [ -f "$LOG_FILE" ] && [ "$(wc -c < "$LOG_FILE" 2>/dev/null || echo 0)" -gt "$max_bytes" ]; then
|
||||
mv "$LOG_FILE" "$LOG_FILE.1" 2>/dev/null || true
|
||||
: > "$LOG_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
log_msg() {
|
||||
local msg="$1"
|
||||
local line
|
||||
rotate_log
|
||||
line="$(date '+%Y-%m-%d %H:%M:%S') [network-watchdog] $msg"
|
||||
echo "$line"
|
||||
echo "$line" >> "$LOG_FILE" 2>/dev/null || true
|
||||
@@ -223,8 +244,22 @@ configure_static() {
|
||||
}
|
||||
|
||||
connectivity_ok() {
|
||||
local iface
|
||||
local gateway
|
||||
local target
|
||||
|
||||
iface="$(detect_interface)"
|
||||
if [ -n "$iface" ] && [ -f "/sys/class/net/$iface/carrier" ]; then
|
||||
if [ "$(cat "/sys/class/net/$iface/carrier" 2>/dev/null || echo 0)" != "1" ]; then
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
gateway="$(ip route show default 2>/dev/null | awk '{print $3; exit}')"
|
||||
if [ -n "$gateway" ] && ping -c 1 -W 2 "$gateway" >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
for target in $PING_TARGETS; do
|
||||
[ -n "$target" ] || continue
|
||||
if ping -c 1 -W 2 "$target" >/dev/null 2>&1; then
|
||||
@@ -260,12 +295,15 @@ check_network() {
|
||||
|
||||
case "${1:-check}" in
|
||||
check)
|
||||
acquire_lock
|
||||
check_network
|
||||
;;
|
||||
dhcp)
|
||||
acquire_lock
|
||||
configure_dhcp
|
||||
;;
|
||||
static)
|
||||
acquire_lock
|
||||
configure_static
|
||||
;;
|
||||
*)
|
||||
|
||||
Reference in New Issue
Block a user