IIS状态监测(如果状态错误则重启IIS)

步骤:

1:建立健康监测文件。文件内容随意,这里以healthcheck.aspx命名,内容是<span>hellow word</span>

2:利用vbs语言执行IIS重启

文件名称:AppPoolRecycle.vbs

注意:自行修改进程池的名称这里是“DefaultAppPool”

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
        & strComputer & "\root\microsoftiisv2")
'回收名为DefaultAppPool的应用程序池
Set colItems = objWMIService.ExecQuery _
    ("Select * From IIsApplicationPool Where Name = " & _
        "'W3SVC/AppPools/DefaultAppPool'")
For Each objItem in colItems
    objItem.Recycle
Next

2:利用VBS调用curl进行探测,利用grep对探测结果进行筛选

文件名称:IISchk.vbs

注意:

A:文件路径中不能有空格,自行修改探测的文件地址这里是http://localhost/healthcheck.aspx

B:本代码位置是C:\healthcheck

C:附件清单(文件夹curl里面有32位64位两个版本外部的curl.exe是32位;grep文件夹里是该软件的安装程序;AppPoolRecycle.vbs 重启脚本;IISchk.vbs检查脚本;curl.txtcurl结果文件;grep.txt grep结果文件;)

d:C:\ProgramFiles\GnuWin32\bin\grep.exe 这个是我的grep安装目录,路径不能有空格

E:建立计划任务周期执行IISchk.vbs脚本就可以了

Set objShell = CreateObject("Wscript.Shell")
'利用curl获取目标网页的http头信息
strcmd1 = "%comspec% /c C:\healthcheck\curl -I http://localhost/healthcheck.aspx -o c:\healthcheck\curl.txt"
'利用grep筛选出curl获取的关键行HTTP/1.1
strcmd2 = "%comspec% /c C:\ProgramFiles\GnuWin32\bin\grep.exe -i HTTP/1.1 c:\healthcheck\curl.txt > c:\healthcheck\grep.txt"
objShell.Run(strcmd1)
'等待500毫秒让strcmd1的文件写完,因为strcmd2需要这个文件
WScript.Sleep 500
objShell.Run(strcmd2)
'打开经过筛选后的grep.txt文本
Const ForReading=1
Dim strLine,strnewline
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\healthcheck\grep.txt", ForReading)
strLine = objTextFile.ReadLine
'去掉头尾的空格
strnewline  = Trim(strLine)
'判断行是否等于HTTP/1.1 200 OK,如果不等于则运行c:\healthcheck\AppPoolRecycle.vbs
If not strnewline = "HTTP/1.1 200 OK" then
objShell.Run("c:\healthcheck\AppPoolRecycle.vbs")
End if

F:用到的文件

posted @ 2013-06-26 13:37  李占卫  阅读(2190)  评论(0编辑  收藏  举报