代码之家  ›  专栏  ›  技术社区  ›  Dan Tanner

windows操作系统有类似Monit的等价物吗?[闭门]

  •  11
  • Dan Tanner  · 技术社区  · 17 年前

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

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

    4 回复  |  直到 17 年前
        1
  •  7
  •   Dan Tanner    17 年前

    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 开始于:C:\projects\foo\scripts

    您还可以使用更强大的调度器(如VisualCron),将其插入windows服务,或通过应用程序服务器调度器(如Quart.NET)进行操作。在我的例子中,任务调度器工作得很好。

        2
  •  1
  •   mendel    11 年前

    我使用的是RGE公司的ipsentry( http://www.ipsentry.com/

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

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

        3
  •  1
  •   Felipe Mejía    9 年前

    $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
    }
    
        4
  •  0
  •   Ben Collins    10 年前

    srvany.exe 在Windows资源工具包中。

    有关编写服务的详细信息: https://support.microsoft.com/en-us/kb/137890

    推荐文章