Mercurial Hosting > luan
comparison host/admin/src/private/lib/monitor.luan @ 2021:b8e5d53c4fc9
admin monitors scheduler
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 20 Oct 2025 14:45:27 -0600 |
| parents | 301a6561fb6b |
| children | 969291201e12 |
comparison
equal
deleted
inserted
replaced
| 2020:163f88f7ee00 | 2021:b8e5d53c4fc9 |
|---|---|
| 12 local Logging = require "luan:logging/Logging.luan" | 12 local Logging = require "luan:logging/Logging.luan" |
| 13 local logger = Logging.logger "monitor" | 13 local logger = Logging.logger "monitor" |
| 14 | 14 |
| 15 | 15 |
| 16 local who_monitors_who = { | 16 local who_monitors_who = { |
| 17 -- ["admin.me.luan.software"] = "admin.me.luan.software" | |
| 18 ["admin.s1.luan.software"] = "admin.s2.luan.software" | 17 ["admin.s1.luan.software"] = "admin.s2.luan.software" |
| 19 ["admin.s2.luan.software"] = "admin.s1.luan.software" | 18 ["admin.s2.luan.software"] = "admin.s1.luan.software" |
| 20 } | 19 } |
| 21 local frequency = Time.period{minutes=2} | 20 local frequency = Time.period{minutes=2} |
| 21 local url | |
| 22 | 22 |
| 23 --[[ -- for development | 23 --[[ -- for development |
| 24 --who_monitors_who["admin.me.luan.software"] = "admin.me.luan.software" | 24 who_monitors_who["admin.me.luan.software"] = "admin.me.luan.software" |
| 25 frequency = Time.period{seconds=20} | 25 url = "http://admin.me.luan.software:8080/hi.txt" |
| 26 frequency = Time.period{seconds=30} | |
| 26 Config.email_to = "fschmidt@gmail.com" | 27 Config.email_to = "fschmidt@gmail.com" |
| 27 --]] | 28 --]] |
| 28 | 29 |
| 29 | 30 |
| 30 | 31 |
| 33 if domain == nil then | 34 if domain == nil then |
| 34 logger.info "nothing to monitor" | 35 logger.info "nothing to monitor" |
| 35 return true | 36 return true |
| 36 end | 37 end |
| 37 | 38 |
| 38 local url = "https://"..domain.."/hi" | 39 url = url or "https://"..domain.."/hi.txt" |
| 39 local options = { time_out = Time.period{seconds=20} } | 40 local options = { time_out = Time.period{seconds=20} } |
| 40 | 41 |
| 41 local function init_check() | 42 local function init_check() |
| 42 local fails = 0 | 43 local fails = 0 |
| 44 local last_page | |
| 45 | |
| 46 local function fail() | |
| 47 fails = fails + 1 | |
| 48 if fails < 2 then return end | |
| 49 try | |
| 50 local s = Utils.ssh(domain,"/Users/administrator/luan/host/restart.sh monitoring") | |
| 51 if trim(s) == "stopped with stop script" then | |
| 52 logger.info("stopped with stop script") | |
| 53 else | |
| 54 logger.error("restart successful\n"..s) | |
| 55 Utils.send_mail { | |
| 56 Subject = domain.." restarted" | |
| 57 body = s | |
| 58 } | |
| 59 end | |
| 60 fails = 0 | |
| 61 catch e | |
| 62 logger.error("restart failed: "..e.get_message()) | |
| 63 if fails < 5 then | |
| 64 Utils.send_mail { | |
| 65 Subject = domain.." restart failed" | |
| 66 body = e.get_message() | |
| 67 } | |
| 68 end | |
| 69 end | |
| 70 end | |
| 43 | 71 |
| 44 return function() | 72 return function() |
| 45 try | 73 try |
| 46 local page = uri(url,options).read_text() | 74 local page = uri(url,options).read_text() |
| 75 if page == last_page then | |
| 76 logger.error("Scheduler not working on "..domain) | |
| 77 fail() | |
| 78 return | |
| 79 end | |
| 80 last_page = page | |
| 47 fails = 0 | 81 fails = 0 |
| 48 logger.info(domain.." is okay") | 82 logger.info(domain.." is okay") |
| 49 catch e | 83 catch e |
| 50 logger.error("Error connecting to "..domain..": "..e.get_message()) | 84 logger.error("Error connecting to "..domain..": "..e.get_message()) |
| 51 fails = fails + 1 | 85 fail() |
| 52 if fails < 2 then return end | |
| 53 try | |
| 54 local s = Utils.ssh(domain,"/Users/administrator/luan/host/restart.sh monitoring") | |
| 55 if trim(s) == "stopped with stop script" then | |
| 56 logger.info("stopped with stop script") | |
| 57 else | |
| 58 logger.error("restart successful\n"..s) | |
| 59 Utils.send_mail { | |
| 60 Subject = domain.." restarted" | |
| 61 body = s | |
| 62 } | |
| 63 end | |
| 64 fails = 0 | |
| 65 catch e | |
| 66 logger.error("restart failed: "..e.get_message()) | |
| 67 if fails < 5 then | |
| 68 Utils.send_mail { | |
| 69 Subject = domain.." restart failed" | |
| 70 body = e.get_message() | |
| 71 } | |
| 72 end | |
| 73 end | |
| 74 end | 86 end |
| 75 end | 87 end |
| 76 end | 88 end |
| 77 | 89 |
| 78 Thread.schedule_closure(init_check,{repeating_delay=frequency}) | 90 Thread.schedule_closure(init_check,{repeating_delay=frequency}) |
