Monitoring an IIS Application Pool

by stevenmurawski at 2012-10-01 11:29:15

I currently have an application in a load balanced IIS cluster (NLB) on Server 2008 R2. This application leverages some legacy database access code which occasionally fails enough to disable the app pool.

I would like put a WMI event watcher in place to monitor the change in the status of the app pool (other suggestions welcome), that would restart the app pool if it goes into a stopped state.

This is for a temporary workaround until new code can be pushed, but that has to go through a Dev/QA/UAT cycle.

There is a WMI class for the ApplicationPool in the root\webadministration namespace, however to get the status, you have to call the GetState method on that object, so I’m not sure how to watch that with a WMI event.
by JasonHelmick at 2012-10-01 12:05:05
Hey Steve! How are ya?

Hey, is using PowerShell Remoting an option? So, you could;

Invoke-Command -Computername <yourserver> {Get-Item –Path IIS:\appPools\defaultappPool | Select-Object -Property state}

Then you could use Start-WebappPool or Restart-WebAppPool

If not, if WMI is your only choice, I’ll dig out the line after class today.

Cheers!

Jason
by JasonHelmick at 2012-10-01 12:18:57
Oh, and I ust ran this…here is the WMI – I used the default app pool as the example. You can then use the methods Recycle(), Start(), and Stop() the same way.

PS C:&gt; gwmi -Namespace root\webAdministration -class ApplicationPool -filter "Name=‘DefaultAppPool’" | Foreach{$_.getstate()}

Cheers!

Jason
by stevenmurawski at 2012-10-01 12:27:08
Thanks Jason.

I had a job using PowerShell remoting doing the watching, until WinRM ran into issues… so then I’m looking at monitoring WinRM to then monitor IIS…

Thanks for the oneliner… I can set up a scheduled task for to poll for that. I was mainly wondering if there was a WMI query that could get the state directly, so I could use an event subscriber to watch for it.

In the meantime, I’m going to proceed with something along the lines of what you’ve got there and use the task scheduler.

Steve
by JasonHelmick at 2012-10-01 12:32:01
I just posted the wMI line if remoting is giving you a headache…let me know how it works out!
by stevenmurawski at 2012-10-01 12:34:25
Thanks Jason. I saw those. I was hoping to directly query the state of the app pool, but that doesn’t appear to be possible in the webadministration namespace (so that I could register a WMI event subscriber rather than polling for change myself).

Not a big deal.

Thanks for the pointer.
by stevenmurawski at 2012-10-01 13:08:07
Another option - trigger off the event log entry that is logged when the app pool shuts down - http://serverfault.com/questions/433750/how-can-i-monitor-an-iis-7-5-application-pool-with-wmi/433788#433788
by JasonHelmick at 2012-10-01 14:22:47
Oh ya! I like that answer…I’m trying to use Register-WMIEvent to see if I can get a similar result…so far no luck…but that link is certainly a good idea.
by stevenmurawski at 2012-10-01 15:09:21
I’m sure that information is in WMI somewhere, but I don’t know where exactly it’s being stored. The Event Log trigger for a scheduled task is going to work just fine. From that I can launch my script using the commands from the web administration module.

Thanks for your research Jason!