Monitor application pool IIS recycle if needed

In working with an issue where an application pool was taking an entire website down, it became apparent we needed a programmatic way to monitor the site itself and then take proactive action in an automated way to correct the problem.  In the case/environment this applied to in my case, a problematic application pool periodically takes down an IIS site.  At first iisresets were used to get things back up and running, however, it was found that the application pool could be recycled and the problem would clear as well.  The objective was clear – we needed to monitor application pool IIS recycle if needed.  How can we accomplish this?

Monitor application pool IIS recycle if needed

In any case, if a website is having issues, we should be able to monitor a keyword or content on the site and then perform an action.  In taking a look around the Net concerning the topic of web monitoring, I stumbled on a great post here which shows using powershell and the WebClient .Net to evaluate returned HTML content.  So essentially, you can check for the existence or nonexistence of a keyword and then perform an action based on that result.
This script can be customized as far as you want to take it and OttoHelweg2 shows how to monitor multiple sites with the script, log and alert.  The single site version of the script works for me with just a tad bit of customization as I wanted to perform an action on the problematic application pool (specifically recycle it) if I see problems with the returned web content.  Easily enough, there is a great powershell commandlet which lets you do just that – Restart-WebAppPool.  Using this commandlet we can just feed in the name of the AppPool and have it recycled if you have issues with an IIS site.  So changing the else statement of the script I hyperlinked above: 
$Log = "c:\test\fail.log"
else {
"Fail`t`t" + $startTime.DateTime + "`t`t" + ($endTime – $startTime).TotalSeconds + " seconds" | Out-File $Log -Append ; Restart-WebAppPool MyNiftyApplicationPool; send-mailmessage -from "<myemail@mydomain.com>" -to "John Doe <someone@somewhere.com>" -subject "Application Pool Failure Detected" -body "MyNiftyApplicationPool has been restarted" -smtpserver mymailserver}

The nice thing is that if you need something more profound than a simple restart of an application pool, you can do it by substituting any command you would like here, i.e. iisreset, etc.
As I have logging setup above in the else clause for a failure, we note when failures occur and log these accordingly.  The log that is created looks like the folloing which I simulated an error by taking the site down. 
Fail Tuesday, August 30, 2016 8:04:41 AM 22.6509915 seconds
Fail Tuesday, August 30, 2016 8:06:34 AM 22.4482166 seconds
Fail Tuesday, August 30, 2016 8:27:58 AM 21.2158797 seconds 
Fail Tuesday, August 30, 2016 8:28:58 AM 21.2158174 seconds 
Fail Tuesday, August 30, 2016 8:29:58 AM 21.2314172 seconds 
Fail Tuesday, August 30, 2016 8:30:58 AM 21.2158166 seconds 
Fail Tuesday, August 30, 2016 8:31:58 AM 21.2626212 seconds 
Fail Tuesday, August 30, 2016 8:32:58 AM 21.2158081 seconds
Fail Tuesday, August 30, 2016 9:00:51 AM 12.1054903 seconds 
Fail Tuesday, August 30, 2016 9:10:58 AM 21.2158336 seconds 
Fail Tuesday, August 30, 2016 9:11:00 AM 31.2309338 seconds 
Fail Tuesday, August 30, 2016 9:11:58 AM 21.2158155 seconds 
Fail Tuesday, August 30, 2016 9:12:58 AM 21.2314246 seconds 
Fail Tuesday, August 30, 2016 9:13:58 AM 21.1846326 seconds


Scheduled Task
As mentioned in linked post above, the script is most effective in a scheduled task which can easily be setup with passing the powershell script parameters inside the scheduled task.  Read how to do this with the recently published post:  Powershell script signing the easy way.
You can set the schedule task to run every minute at the lowest setting, but if you utilize multiple tasks, you can overlap these so that it runs every few seconds.
Thoughts
Powershell is a powerful tool even in monitoring website health and can even be used to proactively repair IIS if services need restarted, etc.  Hopefully, with the modified script above adding to the original script created you can Monitor application pool IIS recycle if needed.

Post a Comment

0 Comments