IIS与ApplicationPool重启检测自动化解决方案

IIS与ApplicationPool重启检测自动化解决方案

DA Hotfix Automatic IIS & Application Pool Check-Reset solution

 

1.右键tool,run as admin;需要联系我​:Tianyou.Lan

2.自动检测完成。

aa.png

经历了一段时间的研究与学习,我终于完成了这个为检查打Hotfix之后agent端Application/IIS是否重启过的自动化解决方案,并且通过了几轮针对性的测试。如果大家在使用过程中遇到了问题请联系我lync:tianyou lan

需求说明:

  1. 在打完Hotfix之后,QA经常是需要检查IIS以及Application Pool是否重启过
  2. 检测IIS的重启往往是要通过在Event Viewer中查看system logs,手动的去查找source metadata含有IISCTLS字样的log然后分析时间,与打Hotfix开始的时间对比,去估计IIS是否在打Hotfix之后进行了重启。
  3. 检测Application Pool的重启是无法通过log进行判断,而以往QA是通过对比打Hotfix前后w3wp进程的PID值是否发生了改变,而这也需要手动进行完成前后的PID值记录,然后去人为的对比,进而判断Application pool是否在打Hotfix前后进行了重启。这也是开发所给提供的宝贵意见。

 

难点创新:

  1. 对于之前的自动化解决方案,我的想法是在多台server上开启权限,然后通过脚本判断多台server role,在WFE上检测log以判断IIS reset情况,而对于Application pool的检测则需要在打Hotfix前后分别收集一次数据从而才能判断Application pool是否重启。一共需要运行两次脚本。
  2. 本次的优化,最主要就是体现在对与Application pool的判断上,相对以往判断打Hotfix前后PID值的变化,这一次取而代之的是通过收集打Hotfix时所产生的AvePoint日志从而取到最近一次打Hotfix成功的时间,然后获取w3wp进程并收集它最近一次的StartTime,然后进行判断,若w3wp进程的StartTime晚于最近的Hotfix log的WrittenTime,则说明Application Pool在打Hotfix之后进行了重启。一共只需要运行一次Tool。

 

PowerShell Code:

 

#获取w3wp进程所对应的Application pool(这个函数是在网上搜的)

function global:get-apppools{

    [regex]$pattern="-ap ""(.+)"""

    gwmi win32_process -filter 'name="w3wp.exe"' | % {

        $name=$_.name

        $cmd = $pattern.Match($_.commandline).Groups[1].Value

        $procid = $_.ProcessId

        New-Object psobject | Add-Member -MemberType noteproperty -PassThru Name $name |

            Add-Member -MemberType noteproperty -PassThru AppPoolID $cmd |

            Add-Member -MemberType noteproperty -PassThru PID $procid

    }

}

 

#最新W3Wp进程开启的时间

$global:W3WPSProcesses=Get-Process -Name w3wp -ErrorAction SilentlyContinue

if($global:W3WPSProcesses -is [array]){

#如果有很多的w3wp进程,取最新的那个。

$global:W3WPStartTime=0

for($j=0;$j -lt $global:W3WPSProcesses.length;$j++){

if($global:W3WPSProcesses[$j].StartTime -gt $global:W3WPStartTime){

$global:W3WPStartTime=$global:W3WPSProcesses[$j].StartTime

}

}

}else{

#只有一个w3wp进程时,获取到w3wp进程的starttime。

$global:W3WPStartTime=$global:W3WPSProcesses.StartTime

}

#最近一个Hotfix开始的时间

$global:HotfixLog=Get-EventLog avepoint|Where-Object {$_.Category -like "*Update Manager*"}

if($global:HotfixLog -is [array]){

#0位为最新的Hotfix时间

$global:HotfixTime=$global:HotfixLog[0].TimeWritten

}else{

$global:HotfixTime=$global:HotfixLog.TimeWritten

}

 

function CheckPool{

begin{

$global:NewOrOldProcess=0

$global:Old=0

$global:New=0

}

process{

if($_.starttime -gt $global:HotfixTime){

$global:NewOrOldProcess=$global:NewOrOldProcess+1

$global:Old=$global:Old+1

$global:New=$global:New+1

$global:process=$_

$global:nameofthepool=(global:get-apppools|Where-Object {$_.pid -eq  $global:process.id}).apppoolid

$global:IndexOfEnd=$global:nameofthepool.indexof('"')

$global:shortNameOfThePool=$global:nameofthepool.substring(0,$global:IndexOfEnd)

Write-Host "The application pool [ $global:shortNameOfThePool ] has reset or the w3wp process of it is new created after the 'Hotfix' updated."

Write-Host "-----------------------"

}else{

$global:Old=$global:Old+1

$global:process=$_

$global:nameofthepool=(global:get-apppools|Where-Object {$_.pid -eq  $global:process.id}).apppoolid

$global:IndexOfEnd=$global:nameofthepool.indexof('"')

$global:shortNameOfThePool=$global:nameofthepool.substring(0,$global:IndexOfEnd)

Write-Host "The application pool [ $global:shortNameOfThePool ] has not reset after the 'Hotfix' updated."

Write-Host "-----------------------"

}

}

end{

if($global:NewOrOldProcess -ne 0){

if($global:New -lt $global:Old){

Write-Host "Through the analysis from 'HotCheck':"

Write-Host "Not all the application pool(s) has been reset or new created after the 'Hotfix' updated."

}

if($global:New -eq $global:Old){

Write-Host "Through the analysis from 'HotCheck':"

Write-Host "All the application pool(s) has been reset or new created after the 'Hotfix' updated."

}

}else{

Write-Host "Through the analysis from 'HotCheck':"

Write-Host "No application pool has been reset or new created after the 'Hotfix' updated."

}

}

}

Get-Process -Name w3wp -ErrorAction SilentlyContinue|CheckPool

 

#判断IIS是否在打Hotfix之后重启过

#i和p都是IISReset标记,0代表没有重启过,否则代表重启过。i代表没有Hotfix,p代表有Hotfix。

$global:i=0

$global:p=0

#$global:IISLogs=Get-EventLog system -After ((get-date).addhours(-5))| where-object {$_.source -like "*IIS*"}

$global:IISLogs=Get-EventLog system| where-object {$_.source -like "*IIS*"}

#filter,判断每一条日志,总的结果输出为i或p。

filter IISCK{

if($global:HotfixTime){

if($_){

if($_.TimeWritten -gt $global:HotfixTime){

$global:p=$global:p+1

}

}

}else{

if($_){

$global:i=$global:i+1

}

}

}

#将IISLogs放入过滤器进行判断

$global:IISLogs|IISCK

#判断结果

if($global:i -ne 0){

Write-Host "Since there're no 'Hotfix' updated, we don't need to care about whether the IIS has been reset yet."

}

if($global:p -ne 0){

Write-Host "IIS has been reset after the 'Hotfix' updated."

}

if($global:i -eq 0){

if($global:p -eq 0){

Write-Host "IIS hasn't been reset after the 'Hotfix' updated."

}

}

Read-Host "Press any key to quit"

 

Tool:

Hotfix Automatic Check,简称Hot Check,"火柴"。

经历了:

  1. 在用需要重启IIS的Hotfix进行测试,通过测试;
  2. 在用只需要重启Application Pool的Hotfix测试,通过测试;
  3. 为克服在不支持大部分PowerShell指令,比如get-date和sort-object的机器上能正常运行,重写排序算法代替sort-object指令,通过功能测试。
  4. 在多台打Hotfix的WFE上进行测试,均能正确显示测试结果,通过测试。

下载地址:

 

感谢:

感谢Lardy 哥给予的支持;

感谢过程中得到的萌萌姐,Di Sun,Xue Pan在测试上的帮助;

另:感谢DL的Dora Liu强力打包的四个针对性测试patch和Zhenpeng Liu的各种支持。

posted @ 2015-01-15 10:37  天外归云  阅读(745)  评论(0编辑  收藏  举报