No encontré nada que se ajuste a mis necesidades, así que aprendí un poco de scripts de Powershell y obtuve una solución que también debería ser útil para otros. Asumiendo una plataforma de Windows (de lo contrario utiliza monit!), Powershell es realmente potente y fácil.
script de ejemplo-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
}
}
La única lógica en este guión es que sólo se tratará de reiniciar el servicio una vez después de un fallo inicial. Esto es para evitar una situación en la que un servicio tarda un rato en reiniciarse, y mientras se reinicia, el monitor sigue viendo la falla y se reinicia de nuevo (bucle infinito malo). De lo contrario, puede hacer casi cualquier cosa, como agregar notificaciones por correo electrónico, o hacer más que simplemente reiniciar un servicio.
Este script se ejecutará una vez, lo que significa que tendrá que controlar su repetición externamente. Podría ponerlo en un bucle infinito en el guión, pero eso parece un poco escamoso. Utilicé el Programador de tareas de Windows, ejecutándolo de esta forma: Programa: Powershell.exe argumentos: -comando "C: \ projects \ foo \ scripts \ monitor.ps1" -noprofile Inicio en: C: \ proyectos \ foo \ scripts
También podría utilizar un programador más robusto como VisualCron, conectarlo a un servicio de Windows o mediante un programador de servidor de aplicaciones como Quart.NET. En mi caso, el programador de tareas funciona bien.
Ja! - una pregunta y respuesta de 7 años puesta en espera. –