| 1995 | 1 local Luan = require "luan:Luan.luan" | 
|  | 2 local error = Luan.error | 
|  | 3 local Io = require "luan:Io.luan" | 
|  | 4 local uri = Io.uri or error() | 
|  | 5 local String = require "luan:String.luan" | 
|  | 6 local trim = String.trim or error() | 
|  | 7 local Time = require "luan:Time.luan" | 
|  | 8 local Thread = require "luan:Thread.luan" | 
|  | 9 local Http = require "luan:http/Http.luan" | 
|  | 10 local Config = require "site:/private/Config.luan" | 
|  | 11 local Utils = require "site:/private/lib/Utils.luan" | 
|  | 12 local Logging = require "luan:logging/Logging.luan" | 
|  | 13 local logger = Logging.logger "monitor" | 
|  | 14 | 
|  | 15 | 
|  | 16 local who_monitors_who = { | 
|  | 17 	-- ["admin.me.luan.software"] = "admin.me.luan.software" | 
|  | 18 	["admin.s1.luan.software"] = "admin.s2.luan.software" | 
|  | 19 	["admin.s2.luan.software"] = "admin.s1.luan.software" | 
|  | 20 } | 
|  | 21 local frequency = Time.period{minutes=2} | 
|  | 22 | 
|  | 23 --[[  -- for development | 
|  | 24 --who_monitors_who["admin.me.luan.software"] = "admin.me.luan.software" | 
|  | 25 frequency = Time.period{seconds=20} | 
|  | 26 Config.email_to = "fschmidt@gmail.com" | 
|  | 27 --]] | 
|  | 28 | 
|  | 29 | 
|  | 30 | 
|  | 31 local domain = who_monitors_who[Utils.domain] | 
|  | 32 | 
|  | 33 if domain == nil then | 
|  | 34 	logger.info "nothing to monitor" | 
|  | 35 	return true | 
|  | 36 end | 
|  | 37 | 
|  | 38 local url = "https://"..domain.."/hi" | 
|  | 39 local options = { time_out = Time.period{seconds=20} } | 
|  | 40 | 
|  | 41 local function init_check() | 
|  | 42 	local fails = 0 | 
|  | 43 | 
|  | 44 	return function() | 
|  | 45 		try | 
|  | 46 			local page = uri(url,options).read_text() | 
|  | 47 			fails = 0 | 
|  | 48 			logger.info(domain.." is okay") | 
|  | 49 		catch e | 
|  | 50 			logger.error("Error connecting to "..domain..": "..e.get_message()) | 
|  | 51 			fails = fails + 1 | 
|  | 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 | 
|  | 75 	end | 
|  | 76 end | 
|  | 77 | 
|  | 78 Thread.schedule_closure(init_check,{repeating_delay=frequency}) | 
|  | 79 | 
|  | 80 logger.info("monitoring "..domain) | 
|  | 81 | 
|  | 82 return true |