任何Monit都喜欢Windows操作系统的等价物吗?

Dan*_*ner 11 system-monitoring

我已经看到问题"你能在Windows上运行Monit吗?",除非你想使用虚拟机,否则答案似乎是否定的.

那么......对于Windows操作系统来说,是否存在任何小型的类似monit的应用程序?我正在寻找的不仅是监控(其中有数百个应用程序),还有执行脚本或重新启动服务的能力.例如,监视一个网页,如果该页面没有响应,则重新启动Tomcat(不能只监视该服务,因为该服务仍在运行但没有正确响应).

这适用于小型应用程序,而不是大型应用程序,因此不需要重量级/昂贵的解决方案.

Dan*_*ner 7

我没有找到任何符合我需求的东西,所以我学到了一些Powershell脚本并推出了一个对其他人也有用的解决方案.假设一个Windows平台(否则使用monit!),Powershell非常强大而且简单.

sample-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
    }
}
Run Code Online (Sandbox Code Playgroud)

此脚本中唯一的逻辑是它只会在初始失败后尝试重新启动一次服务.这是为了防止服务需要一段时间才能重新启动,并且在重新启动时,监视器会一直看到故障并再次重新启动(无限循环错误).否则,您可以执行任何操作,例如添加电子邮件通知,或者不仅仅是重新启动服务.

该脚本将执行一次,这意味着您需要从外部控制其重复.你可以把它放在脚本中的无限循环中,但这看起来有点不稳定.我使用了Windows Task Scheduler,执行它如下:Program:Powershell.exe参数:-command"C:\ projects\foo\scripts\monitor.ps1"-noprofile Start In:C:\ projects\foo\scripts

您还可以使用更强大的调度程序,如VisualCron,将其插入Windows服务,或通过Quart.NET等应用程序服务器调度程序.在我的情况下,任务调度程序工作正常.