From bfa68743da0c98311b1114710195277074287cd8 Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Thu, 19 Mar 2026 20:34:20 +0700 Subject: [PATCH] should work --- Manifest.toml | 140 +----------------------------------- Project.toml | 2 +- check_and_reboot_state.json | 2 +- check_router_reboot.jl | 46 ++++++++++-- check_router_reboot_log.txt | 48 +++++++++++-- 5 files changed, 86 insertions(+), 152 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 9815147..163e1a5 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,62 +1,14 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.12.1" +julia_version = "1.12.5" manifest_format = "2.0" -project_hash = "324e0aa322681a327b337bcd7a8530af39c981d0" - -[[deps.Artifacts]] -uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" -version = "1.11.0" - -[[deps.Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" -version = "1.11.0" - -[[deps.BitFlags]] -git-tree-sha1 = "0691e34b3bb8be9307330f88d1a3c3f25466c24d" -uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35" -version = "0.1.9" - -[[deps.CodecZlib]] -deps = ["TranscodingStreams", "Zlib_jll"] -git-tree-sha1 = "962834c22b66e32aa10f7611c08c8ca4e20749a9" -uuid = "944b1d66-785c-5afd-91f1-9de20f533193" -version = "0.7.8" - -[[deps.ConcurrentUtilities]] -deps = ["Serialization", "Sockets"] -git-tree-sha1 = "d9d26935a0bcffc87d2613ce14c527c99fc543fd" -uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb" -version = "2.5.0" +project_hash = "9b274fc74610c14aa65af9a9d85650d80d379917" [[deps.Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" version = "1.11.0" -[[deps.ExceptionUnwrapping]] -deps = ["Test"] -git-tree-sha1 = "d36f682e590a83d63d1c7dbd287573764682d12a" -uuid = "460bff9d-24e4-43bc-9d9f-a8973cb893f4" -version = "0.1.11" - -[[deps.HTTP]] -deps = ["Base64", "CodecZlib", "ConcurrentUtilities", "Dates", "ExceptionUnwrapping", "Logging", "LoggingExtras", "MbedTLS", "NetworkOptions", "OpenSSL", "PrecompileTools", "Random", "SimpleBufferStream", "Sockets", "URIs", "UUIDs"] -git-tree-sha1 = "5e6fe50ae7f23d171f44e311c2960294aaa0beb5" -uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" -version = "1.10.19" - -[[deps.InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" -version = "1.11.0" - -[[deps.JLLWrappers]] -deps = ["Artifacts", "Preferences"] -git-tree-sha1 = "0533e564aae234aff59ab625543145446d8b6ec2" -uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" -version = "1.7.1" - [[deps.JSON]] deps = ["Dates", "Logging", "Parsers", "PrecompileTools", "StructUtils", "UUIDs", "Unicode"] git-tree-sha1 = "eb04df293213df64ddd720c86de3c431f5f8ccf1" @@ -69,61 +21,10 @@ version = "1.2.1" [deps.JSON.weakdeps] ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd" -[[deps.JuliaSyntaxHighlighting]] -deps = ["StyledStrings"] -uuid = "ac6e5ff7-fb65-4e79-a425-ec3bc9c03011" -version = "1.12.0" - -[[deps.Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" -version = "1.11.0" - [[deps.Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" version = "1.11.0" -[[deps.LoggingExtras]] -deps = ["Dates", "Logging"] -git-tree-sha1 = "f00544d95982ea270145636c181ceda21c4e2575" -uuid = "e6f89c97-d47a-5376-807f-9c37f3926c36" -version = "1.2.0" - -[[deps.Markdown]] -deps = ["Base64", "JuliaSyntaxHighlighting", "StyledStrings"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" -version = "1.11.0" - -[[deps.MbedTLS]] -deps = ["Dates", "MbedTLS_jll", "MozillaCACerts_jll", "NetworkOptions", "Random", "Sockets"] -git-tree-sha1 = "c067a280ddc25f196b5e7df3877c6b226d390aaf" -uuid = "739be429-bea8-5141-9913-cc70e7f3736d" -version = "1.1.9" - -[[deps.MbedTLS_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "3cce3511ca2c6f87b19c34ffc623417ed2798cbd" -uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" -version = "2.28.10+0" - -[[deps.MozillaCACerts_jll]] -uuid = "14a3606d-f60d-562e-9121-12d972cd8159" -version = "2025.5.20" - -[[deps.NetworkOptions]] -uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" -version = "1.3.0" - -[[deps.OpenSSL]] -deps = ["BitFlags", "Dates", "MozillaCACerts_jll", "NetworkOptions", "OpenSSL_jll", "Sockets"] -git-tree-sha1 = "386b47442468acfb1add94bf2d85365dea10cbab" -uuid = "4d8831e6-92b7-49fb-bdf8-b643e874388c" -version = "1.6.0" - -[[deps.OpenSSL_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95" -version = "3.5.1+0" - [[deps.Parsers]] deps = ["Dates", "PrecompileTools", "UUIDs"] git-tree-sha1 = "7d2f8f21da5db6a806faf7b9b292296da42b2810" @@ -156,19 +57,6 @@ version = "1.11.0" uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" version = "0.7.0" -[[deps.Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" -version = "1.11.0" - -[[deps.SimpleBufferStream]] -git-tree-sha1 = "f305871d2f381d21527c770d4788c06c097c9bc1" -uuid = "777ac1f9-54b0-4bf8-805c-2214025038e7" -version = "1.2.0" - -[[deps.Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" -version = "1.11.0" - [[deps.StructUtils]] deps = ["Dates", "UUIDs"] git-tree-sha1 = "79529b493a44927dd5b13dde1c7ce957c2d049e4" @@ -183,30 +71,11 @@ version = "2.6.0" Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -[[deps.StyledStrings]] -uuid = "f489334b-da3d-4c2e-b8f0-e476e12c162b" -version = "1.11.0" - [[deps.TOML]] deps = ["Dates"] uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" version = "1.0.3" -[[deps.Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -version = "1.11.0" - -[[deps.TranscodingStreams]] -git-tree-sha1 = "0c45878dcfdcfa8480052b6ab162cdd138781742" -uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" -version = "0.11.3" - -[[deps.URIs]] -git-tree-sha1 = "bef26fb046d031353ef97a82e3fdb6afe7f21b1a" -uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" -version = "1.6.1" - [[deps.UUIDs]] deps = ["Random", "SHA"] uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" @@ -215,8 +84,3 @@ version = "1.11.0" [[deps.Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" version = "1.11.0" - -[[deps.Zlib_jll]] -deps = ["Libdl"] -uuid = "83775a58-1f1d-513f-b197-d71354ab007a" -version = "1.3.1+2" diff --git a/Project.toml b/Project.toml index 7978070..dfd10f7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,3 +1,3 @@ [deps] -HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" diff --git a/check_and_reboot_state.json b/check_and_reboot_state.json index 3d2cdaa..47e31a2 100644 --- a/check_and_reboot_state.json +++ b/check_and_reboot_state.json @@ -1 +1 @@ -{"last_reboot_datetime":"2026-01-31T12:56:40.834","consecutive_fails":0} \ No newline at end of file +{"last_reboot_datetime":"2026-03-19T18:27:53.682","consecutive_fails":0} \ No newline at end of file diff --git a/check_router_reboot.jl b/check_router_reboot.jl index 62bd1cc..3889e56 100644 --- a/check_router_reboot.jl +++ b/check_router_reboot.jl @@ -128,7 +128,6 @@ end function save_state(st::State, StateFilePath) obj = Dict("consecutive_fails" => st.consecutive_fails, "last_reboot_datetime" => st.last_reboot_datetime) - @show StateFilePath JSON.json(StateFilePath, obj) end @@ -227,16 +226,18 @@ 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 - logmsg("In cooldown after recent reboot; skipping check. $timepass/$COOLDOWN_AFTER_REBOOT_SECS seconds") - return + in_cooldown = true end end + success = false - last_result = nothing + last_res7ult = nothing for i in 1:ATTEMPTS_PER_CHECK ok, result = check_router_once(ROUTER_IP) last_result = result @@ -246,6 +247,39 @@ function perform_check!(st::State) end sleep(BACKOFF_BETWEEN_ATTEMPTS) end + + 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 + end + + # Outside cooldown - full check with potential reboot if success if st.consecutive_fails > 0 logmsg("$ROUTER_IP is reachable; resetting consecutive failure counter.") @@ -257,7 +291,7 @@ function perform_check!(st::State) return else st.consecutive_fails += 1 - routerresult = isnothing(last_result) ? "no response" : last_result + 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 @@ -265,7 +299,7 @@ function perform_check!(st::State) save_state(st, StateFilePath) ok = do_reboot() if ok - thisFilePath = @__FILE__ + thisFilePath = @__FILE__ broadcast_msg("Broadcasting from file: $thisFilePath") logmsg("Reboot executed (or simulated). Resetting failure counter.") st.consecutive_fails = 0 diff --git a/check_router_reboot_log.txt b/check_router_reboot_log.txt index a7f6eb1..1da62a8 100644 --- a/check_router_reboot_log.txt +++ b/check_router_reboot_log.txt @@ -1,9 +1,3 @@ -[2026-03-11T15:44:25.256] Starting check loop. Checking router 192.168.88.1 every 60 seconds. -[2026-03-11T15:44:26.277] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) -[2026-03-11T15:45:26.318] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) -[2026-03-11T15:49:03.012] Starting check loop. Checking router 192.168.88.1 every 60 seconds. -[2026-03-11T15:49:03.985] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) -[2026-03-11T15:50:04.059] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) [2026-03-11T15:51:04.131] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) [2026-03-11T15:52:04.197] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) [2026-03-11T15:53:04.266] Error during check: SystemError("opening file \"/home/ton/docker-programs/check_and_reboot/check_and_reboot_state.json\"", 13, nothing) @@ -71,3 +65,45 @@ [2026-03-19T15:54:06.554] Reboot attempt failed; will retry after next interval. [2026-03-19T15:58:26.336] Starting check loop. Checking router 192.168.88.1 every 60 seconds. [2026-03-19T15:58:26.462] 192.168.88.1 is reachable; resetting consecutive failure counter. +[2026-03-19T16:27:27.839] Starting check loop. Checking router 192.168.88.1 every 60 seconds. +[2026-03-19T16:34:04.850] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 1/3. +[2026-03-19T16:38:14.247] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 2/3. +[2026-03-19T16:42:23.657] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 3/3. +[2026-03-19T16:42:23.669] Executing reboot command: /bin/systemctl reboot +[2026-03-19T16:42:23.678] Reboot executed (or simulated). Resetting failure counter. +[2026-03-19T16:43:49.992] Starting check loop. Checking router 192.168.88.1 every 60 seconds. +[2026-03-19T16:43:50.104] In cooldown after recent reboot; skipping check. 86/600 seconds +[2026-03-19T16:44:50.144] In cooldown after recent reboot; skipping check. 146/600 seconds +[2026-03-19T16:45:50.206] In cooldown after recent reboot; skipping check. 206/600 seconds +[2026-03-19T16:46:50.222] In cooldown after recent reboot; skipping check. 266/600 seconds +[2026-03-19T16:47:50.283] In cooldown after recent reboot; skipping check. 326/600 seconds +[2026-03-19T16:48:50.344] In cooldown after recent reboot; skipping check. 386/600 seconds +[2026-03-19T16:49:50.351] In cooldown after recent reboot; skipping check. 446/600 seconds +[2026-03-19T16:50:50.412] In cooldown after recent reboot; skipping check. 506/600 seconds +[2026-03-19T16:51:50.465] In cooldown after recent reboot; skipping check. 566/600 seconds +[2026-03-19T16:56:00.122] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 1/3. +[2026-03-19T17:00:09.642] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 2/3. +[2026-03-19T17:04:19.199] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 3/3. +[2026-03-19T17:04:19.213] Executing reboot command: /bin/systemctl reboot +[2026-03-19T17:04:19.221] Reboot executed (or simulated). Resetting failure counter. +[2026-03-19T17:05:36.172] Starting check loop. Checking router 192.168.88.1 every 60 seconds. +[2026-03-19T17:05:36.285] In cooldown after recent reboot; skipping check. 77/600 seconds +[2026-03-19T17:06:36.327] In cooldown after recent reboot; skipping check. 137/600 seconds +[2026-03-19T17:07:36.348] In cooldown after recent reboot; skipping check. 197/600 seconds +[2026-03-19T17:08:36.359] In cooldown after recent reboot; skipping check. 257/600 seconds +[2026-03-19T17:09:36.420] In cooldown after recent reboot; skipping check. 317/600 seconds +[2026-03-19T18:15:58.573] Starting check loop. Checking router 192.168.88.1 every 60 seconds. +[2026-03-19T18:19:34.550] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 1/3. +[2026-03-19T18:23:44.126] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 2/3. +[2026-03-19T18:27:53.662] 192.168.88.1 is unreachable (last result: ProcessFailedException(Base.Process[Process(`ping -c 1 -W 30 192.168.88.1`, ProcessExited(1))])). Consecutive fails: 3/3. +[2026-03-19T18:27:53.673] Executing reboot command: /bin/systemctl reboot +[2026-03-19T18:27:53.682] Reboot executed (or simulated). Resetting failure counter. +[2026-03-19T18:30:25.480] Starting check loop. Checking router 192.168.88.1 every 60 seconds. +[2026-03-19T18:30:25.618] In cooldown after recent reboot; skipping check. 151/600 seconds +[2026-03-19T18:31:25.627] In cooldown after recent reboot; skipping check. 211/600 seconds +[2026-03-19T18:32:25.631] In cooldown after recent reboot; skipping check. 271/600 seconds +[2026-03-19T18:33:25.644] In cooldown after recent reboot; skipping check. 331/600 seconds +[2026-03-19T18:34:25.706] In cooldown after recent reboot; skipping check. 392/600 seconds +[2026-03-19T18:35:25.731] In cooldown after recent reboot; skipping check. 452/600 seconds +[2026-03-19T18:36:25.793] In cooldown after recent reboot; skipping check. 512/600 seconds +[2026-03-19T18:37:25.855] In cooldown after recent reboot; skipping check. 572/600 seconds