私は自分のニーズに合ったものは見つけられませんでしたので、Powershellの小さなスクリプトを学び、他の人にも役立つはずのソリューションを紹介しました。 Windowsプラットフォーム(それ以外の場合はmonit!を使用します)を仮定すると、Powershellは本当に強力で簡単です。
サンプル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
}
}
このスクリプトでは唯一のロジックは、それが初期不良のみの後に一度サービスを再起動しようとするということです。これは、サービスの再起動に時間がかかり、再起動している間、モニターは失敗を見続けてから再開します(悪い無限ループ)。それ以外の場合は、電子メール通知の追加やサービスの再起動だけでは何もできません。
このスクリプトは一度実行されます。つまり、外部で繰り返しを制御する必要があります。あなたはスクリプトの中で無限ループに入れることができますが、それはちょっと薄れているようです。 プログラム:Powershell.exe 引数:-command "C:¥projects¥foo¥scripts¥monitor.pop1" -noprofile 開始場所:C:\ projects \ foo \ scripts以下のようにしてWindowsタスクスケジューラを使用します。
VisualCronのようなより堅牢なスケジューラを使用して、Windowsサービスにプラグインするか、Quart.NETのようなアプリケーションサーバースケジューラを使用することもできます。私の場合、タスクスケジューラは正常に動作します。
ほらを再起動しませんでした! - 7歳の質問と回答が保留になりました。 –