Non ho trovato nulla che corrispondesse alle mie esigenze, quindi ho imparato un po 'di scripting di Powershell e ho trovato una soluzione che dovrebbe essere utile anche per gli altri. Supponendo una piattaforma Windows (altrimenti usa monit!), Powershell è davvero potente e facile.
script di esempio-monitor.ps1:
$webClient = new-object System.Net.WebClient
###################################################
# BEGIN USER-EDITABLE VARIABLES
# the URL to ping
$HeartbeatUrl = "http://someplace.com/somepage/"
# the response string to look for that indicates things are working ok
$SuccessResponseString = "Some Text"
# the name of the windows service to restart (the service name, not the display name)
$ServiceName = "Tomcat6"
# the log file used for monitoring output
$LogFile = "c:\temp\heartbeat.log"
# used to indicate that the service has failed since the last time we checked.
$FailureLogFile = "c:\temp\failure.log"
# END USER-EDITABLE VARIABLES
###################################################
# create the log file if it doesn't already exist.
if (!(Test-Path $LogFile)) {
New-Item $LogFile -type file
}
$startTime = get-date
$output = $webClient.DownloadString($HeartbeatUrl)
$endTime = get-date
if ($output -like "*" + $SuccessResponseString + "*") {
# uncomment the below line if you want positive confirmation
#"Success`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds" >> $LogFile
# remove the FailureLog if it exists to indicate we're in good shape.
if (Test-Path $FailureLogFile) {
Remove-Item $FailureLogFile
}
}
else {
"Fail`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds" >> $LogFile
# restart the service if this is the first time it's failed since the last successful check.
if (!(Test-Path $FailureLogFile)) {
New-Item $FailureLogFile -type file
"Initial failure:" + $startTime.DateTime >> $FailureLogFile
Restart-Service $ServiceName
}
}
L'unica logica in questo script è che sarà solo cercare di riavviare il servizio una volta dopo un errore iniziale. Questo serve a prevenire una situazione in cui un servizio richiede un po 'di tempo per riavviarsi, e mentre si sta riavviando, il monitor continua a vedere l'errore e si riavvia di nuovo (ciclo infinito negativo). Altrimenti puoi fare qualsiasi cosa, come aggiungere notifiche via email o fare di più che riavviare un servizio.
Questo script verrà eseguito una volta, il che significa che sarà necessario controllarne la ripetizione esternamente. Potresti inserirlo in un ciclo infinito proprio nella sceneggiatura, ma sembra un po 'squilibrato. Ho usato Windows Task Scheduler, eseguendolo in questo modo: Programma: Powershell.exe argomenti: -comando "C: \ projects \ foo \ scripts \ monitor.ps1" -noprofile Inizio: C: \ projects \ foo \ scripts
È anche possibile utilizzare uno scheduler più robusto come VisualCron, collegarlo a un servizio Windows o tramite uno scheduler del server delle applicazioni come Quart.NET. Nel mio caso l'utilità di pianificazione funziona correttamente.
fonte
2009-06-19 02:37:13
Hah! - una domanda e una risposta di 7 anni sono state messe in attesa. –