65.9K
CodeProject 正在变化。 阅读更多。
Home

网站监控器

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (3投票s)

2010年4月21日

CPOL
viewsIcon

30054

downloadIcon

590

这个程序是一个VBS脚本,它会每分钟检查您的网站,并在网站离线时向您发出警告。

引言

这个程序是一个VBS脚本。该脚本会定期检查您的网站是否在线。当您的网站离线时,您将收到一个弹出窗口,该窗口将在15秒后消失。如果您点击“确定”,弹出窗口按钮将会在Internet Explorer中打开测试页面。该程序还会将错误记录到日志文件(WebsiteMonitor.vbs.log)中,该文件与脚本位于同一文件夹中。

部署

部署此程序

  1. Test.asp复制到您的根文件夹(C:\Inetpub\wwwroot)。
  2. 修改WebsiteMonitor.vbs文件的第一行,使其指向您网站上的Test.asp
  3. 该脚本需要使用Windows计划任务注册。转到控制面板 > 计划任务 > 添加计划任务。该脚本将运行大约24小时,因此重要的是将其安排每天运行。

Using the Code

这是VBS脚本

sURL = "http://www.MySite.com/test.asp"

For i = 1 to (24*59) 'minutes in day
    
    sData = GetUrlData(sURL)
    
    If sData <> "Good" Then
        Set oShell = CreateObject("WScript.Shell")
        iButton = oShell.Popup ("Cannot connect to " & sURL, 15)
        if iButton <> -1 Then
            OpenIE sURL
        End If
        
        Log Now() & vbTab & sData
    End If

    WScript.Sleep(1000*60) '1 minute
Next

Function GetUrlData(sUrl)
    on error resume next
    Dim oHttp
    'Set oHttp = CreateObject("Microsoft.XMLHTTP")
    Set oHttp = CreateObject("MSXML2.ServerXMLHTTP")
    oHttp.setTimeouts 0, 0, 0, 0
    
    oHttp.Open "GET", sUrl, False
    oHttp.send
    
    If oHttp.responseText = "" Then
      GetUrlData = "Error Occurred : " & oHttp.Status & " - " & oHttp.statusText
    Else
      GetUrlData = oHttp.responseText
    End If
    
    Set oHttp = Nothing
End Function

Sub Log(sLine)
    Const ForAppending = 8
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oLogFile: Set oLogFile = fso.OpenTextFile(WScript.ScriptFullName & _
                                 ".log", ForAppending, True)
    oLogFile.WriteLine sLine
    oLogFile.Close
End Sub

Sub OpenIE(sURL)
    On Error Resume Next
    
    Set oShell = CreateObject("Shell.Application")
    Set oIE = oShell.Windows.Item

    oIE.Navigate2 sURL
    
    If Err.number <> 0 Then
        Set oIE = wscript.CreateObject("internetexplorer.application")
        oIE.Visible = True
        oIE.Navigate2 sURL
    End If
    
    Set oIE = Nothing
    Set oShell = Nothing
End Sub

关注点

我也希望这个脚本能够发出蜂鸣声或发送电子邮件。但我一直没有时间去做。

© . All rights reserved.