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

时间:2009-03-19 19:52:54

标签: system-monitoring

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

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

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

4 个答案:

答案 0 :(得分:6)

我没有找到任何符合我需求的东西,所以我学到了一些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
    }
}

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

此脚本将执行一次,这意味着您需要在外部控制其重复。你可以把它放在脚本中的无限循环中,但这看起来有点不稳定。我使用Windows任务计划程序,执行它如下: 程序:Powershell.exe 参数:-command“C:\ projects \ foo \ scripts \ monitor.ps1”-noprofile 开始于:C:\ projects \ foo \ scripts

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

答案 1 :(得分:1)

当他无法连接时,我调整了一点Dan Tanner脚本,显示错误并且没有重新启动服务

$webClient = new-object System.Net.WebClient

###################################################
# BEGIN USER-EDITABLE VARIABLES

# the URL to ping
$HeartbeatUrl = "http://localhost:8080/"

# the response string to look for that indicates things are working ok
$SuccessResponseString = "Apache"

# 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\log.log"

# used to indicate that the service has failed since the last time we checked.
$FailureLogFile = "c:\temp\log2.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
try {
    $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
        }
    }
    }catch [Net.WebException] {
        New-Item $FailureLogFile -type file
        "Initial failure:" + $startTime.DateTime + $_.Exception.ToString() >> $FailureLogFile
        Restart-Service $ServiceName
}

答案 2 :(得分:0)

我正在使用RGE公司的ipsentry(http://www.ipsentry.com/)。

已经使用了好几年,多次救了我。

与他们没有任何关系,这不是广告,只是来自满意客户的信息。

答案 3 :(得分:0)

这可以使用Windows附带的服务控制管理器至少部分完成。它监视服务应用程序并可以在启动时自动启动它们,在崩溃时重新启动它们等。将应用程序编写为服务是一种选择,但如果您不能将应用程序编写为服务,那么您可以尝试包装该过程在Windows资源工具包中使用srvany.exe

有关撰写服务的更多信息:https://support.microsoft.com/en-us/kb/137890

至于实际监控功能,我不完全确定可用的内容,或SCM功能的扩展。