This commit is contained in:
2026-03-20 09:06:21 +07:00
parent 6f77993798
commit bd12ee9e49
4 changed files with 179 additions and 168 deletions

View File

@@ -16,11 +16,11 @@ using Dates, Printf, JSON
# Configuration
const ROUTER_IP = "192.168.88.1"
const TIMEOUT_SECS = 30 # request timeout
const ATTEMPTS_PER_CHECK = 3 # number of ping attempts per check
const BACKOFF_BETWEEN_ATTEMPTS = 60 # seconds between attempts
const ATTEMPTS_PER_CHECK = 1 # number of ping attempts per check
const BACKOFF_BETWEEN_ATTEMPTS = 1 # seconds between ping attempts
const FAILS_TO_REBOOT = 3 # consecutive failed checks required to trigger reboot
const COOLDOWN_AFTER_REBOOT_SECS = 600 # do not reboot again within this many seconds
const DRY_RUN = true # set false to actually reboot
const DRY_RUN = false # set false to actually reboot
const CHECK_INTERVAL_SECS = 60 # run a check every CHECK_INTERVAL_SECS seconds
const thisFolderPath = @__DIR__
@@ -226,77 +226,45 @@ end
# Single check iteration
function perform_check!(st::State)
# Check if we're in cooldown period
in_cooldown = false
if st.last_reboot_datetime !== nothing
timepass = ((Dates.now() - st.last_reboot_datetime).value / 1000) |> floor |> Int
if timepass < COOLDOWN_AFTER_REBOOT_SECS
in_cooldown = true
success = false
last_result = nothing
for i in 1:ATTEMPTS_PER_CHECK
ok, result = check_router_once(ROUTER_IP)
# ok, result = values(JSON.parsefile("test_ping_result.json")) # for testing without actual ping
if ok
success = true
break
end
end
sleep(BACKOFF_BETWEEN_ATTEMPTS)
end
routerresult = isnothing(last_result) ? "no response" : last_result
success = false
last_result = nothing
for i in 1:ATTEMPTS_PER_CHECK
ok, result = check_router_once(ROUTER_IP)
last_result = result
if ok
success = true
break
end
sleep(BACKOFF_BETWEEN_ATTEMPTS)
end
# Check if we're in cooldown period
in_cooldown = false
if st.last_reboot_datetime !== nothing
timepass = ((Dates.now() - st.last_reboot_datetime).value / 1000) |> floor |> Int
if in_cooldown
# During cooldown, track failures but don't trigger reboot yet
if success
logmsg("$ROUTER_IP is reachable during cooldown. Router is back online! Resetting state.")
st.consecutive_fails = 0
save_state(st, StateFilePath)
else
st.consecutive_fails += 1
routerresult = isnothing(last_result) ? "no response" : last_result
logmsg("$ROUTER_IP is unreachable during cooldown. Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
save_state(st, StateFilePath)
# Check if we've reached threshold by now
if st.consecutive_fails >= FAILS_TO_REBOOT
logmsg("Cooldown has expired and router is still unreachable. Triggering reboot.")
ok = do_reboot()
if ok
thisFilePath = @__FILE__
broadcast_msg("Broadcasting from file: $thisFilePath")
logmsg("Reboot executed (or simulated). Resetting failure counter.")
st.consecutive_fails = 0
st.last_reboot_datetime = Dates.now()
save_state(st, StateFilePath)
else
logmsg("Reboot attempt failed; will retry after next interval.")
end
end
end
return
if timepass < COOLDOWN_AFTER_REBOOT_SECS
in_cooldown = true
end
end
# Outside cooldown - full check with potential reboot
if in_cooldown
# During cooldown, track failures but don't trigger reboot yet
if success
if st.consecutive_fails > 0
logmsg("$ROUTER_IP is reachable; resetting consecutive failure counter.")
else
# logmsg("$ROUTER_IP is reachable.")
end
st.consecutive_fails = 0
save_state(st, StateFilePath)
return
logmsg("$ROUTER_IP is reachable during cooldown. Router is back online! Resetting state.")
st.consecutive_fails = 0
save_state(st, StateFilePath)
else
st.consecutive_fails += 1
routerresult = isnothing(last_result) ? "no response" : last_result
logmsg("$ROUTER_IP is unreachable (last result: $routerresult). Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
save_state(st, StateFilePath)
end
if st.consecutive_fails >= FAILS_TO_REBOOT
save_state(st, StateFilePath)
st.consecutive_fails += 1
logmsg("$ROUTER_IP is unreachable during cooldown. Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
save_state(st, StateFilePath)
# Check if we've reached threshold by now
if st.consecutive_fails >= FAILS_TO_REBOOT
logmsg("Cooldown has expired and router is still unreachable. Triggering reboot.")
ok = do_reboot()
if ok
thisFilePath = @__FILE__
@@ -308,7 +276,40 @@ function perform_check!(st::State)
else
logmsg("Reboot attempt failed; will retry after next interval.")
end
end
end
return
end
# Outside cooldown - full check with potential reboot
if success
if st.consecutive_fails > 0
logmsg("$ROUTER_IP is reachable; resetting consecutive failure counter.")
else
# logmsg("$ROUTER_IP is reachable.")
end
st.consecutive_fails = 0
save_state(st, StateFilePath)
return
else
st.consecutive_fails += 1
logmsg("$ROUTER_IP is unreachable (last result: $routerresult). Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
save_state(st, StateFilePath)
end
if st.consecutive_fails >= FAILS_TO_REBOOT
save_state(st, StateFilePath)
ok = do_reboot()
if ok
thisFilePath = @__FILE__
broadcast_msg("Broadcasting from file: $thisFilePath")
logmsg("Reboot executed (or simulated). Resetting failure counter.")
st.consecutive_fails = 0
st.last_reboot_datetime = Dates.now()
save_state(st, StateFilePath)
else
logmsg("Reboot attempt failed; will retry after next interval.")
end
end
end