powershell monitor if requests are occomulating under an appPoolRSS

6 replies

Last post Jun 02, 2020 07:31 PM by carehart

  • powershell monitor if requests are occomulating under an appPool

    Feb 08, 2020 08:50 AM|tamimh|LINK

    Hi

    our Dev team have 20 upplications under default website on IIS 10, Server 2012R2

    sometimes one of the applications is stuck.

    in IIS when I go to worker processes and click AppPoolname, I see under the stuck appPool many requests.

    I need to

    1. monitor if many requests are occomulating under an appPool

    2. recycle the problematic appPool.

    I only managed to do this partially using appcmd:

    appcmd.exe list requests | find /c /v ""
    

    I prefer working with powershell but could not find how to accomplish this.

    any suggestions?

  • Re: powershell monitor if requests are occomulating under an appPool

    Feb 08, 2020 11:00 PM|carehart|LINK

    If you're asking simply how in powershell to get that equivalent count of requests (which as you've written it is across all app pools), you can use simply:

    (Get-WebRequest).count

    And to recycle a given app pool, you can use simply:

    Restart-WebAppPool apppoolname

    And in case you or someone seeing this in the future wants to get that count for some GIVEN app pool, that can be done with:

    (Get-WebRequest -AppPool "poolname").count

    You could also list only those longer than a given duration, such as with this (for 5 seconds):

    (Get-WebRequest -AppPool "poolname" | where {$_.timeElapsed -gt 5000} ).count

    Hope that helps get you where you want to be.

    /charlie
  • Re: powershell monitor if requests are occomulating under an appPool

    Feb 09, 2020 06:03 AM|tamimh|LINK

    Thanks carehart
    I need to loop through each app pool and check if there are requests stuck.
    I will try it out
  • Re: powershell monitor if requests are occomulating under an appPool

    Feb 10, 2020 08:50 AM|Yuk Ding|LINK

    Hi tamimh,

    You could use this to get all IIS sites name.

    Get-ChildItem -Path IIS:\Sites

    Then you could monitor its request based on

    Get-WebRequest

    I think if you want to loop this, you could add this operation to task scheduler.

    Best Regards,

    Jokies Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.
  • Re: powershell monitor if requests are occomulating under an appPool

    Feb 12, 2020 09:06 AM|tamimh|LINK

    $pools = Get-IISAppPool
    foreach ($app in $pools) 
        { $app.Name +" :" + (Get-WebRequest).count }

    that gave me the desired request count. however it is very slow when there are many requests in the queue.

  • Re: powershell monitor if requests are occomulating under an appPool

    Mar 16, 2020 04:54 PM|carehart|LINK

    Hey Tamimh, there is a solution for that slowness you experience with the approach you are taking. And on the surface it's easy, but there are some gotchas to be aware of, with simple solutions, so this will read more like a blog post (and I will blog the info elsewhere, but didn't want to keep you waiting until I may do that).

    First, though, I'll clarify that I had been wanting to address your last comment, where you said that (and marked as "the answer" was not performing well). But sadly, once you marked it as the answer, the thread was locked and I could no longer reply. An admin here (rovistar) just kindly unlocked the thread for us.

    Background/intro

    So I had done more digging after you reported how the approach we had all taken (me, Duk, you) in response to your original question of how to do in PS what you did in appcmd. But yep, once you have MANY running (or queued requests), and perhaps across many app pools, that approach of using get-webrequest is not optimal.

    Instead, what you can do instead is leverage the perfmon counters available to track the count. There is good news/bad news with this.

    Easily getting a count of how many requests (if any) are running, across all app pools

    First the good news is that the count is fast--as indeed, it is JUST the count, via a perfmon metric that Windows is already running.

    And if you just want a count of requests (running or queued) across ALL app pools, the counter is this:  

    \W3SVC_W3WP(*)\Active Requests

    And you can get to that via PS with this:

    Get-Counter -Counter '\W3SVC_W3WP(*)\Active Requests'

    Trouble, if no app pools are running

    But this is where the "fun" (bad news) begins. First, note that if you run this when there are NO app pools having been instantiated (or they have quiesced for not having requests for the app pool timeout time), that will get an error. For example:

    Get-Counter : The specified instance is not present.
    At line:1 char:1
    + Get-Counter -Counter '\W3SVC_W3WP(*)\Active Requests'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
        + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand
    

    The problem is that sadly there IS no counter of that name if no app pools are running. But you can solve that easily by wrapping the call in a try catch, which ignores the error:

    try{
        Get-Counter -Counter '\W3SVC_W3WP(*)\Active Requests'  -ErrorAction stop
    }
    catch {
        echo "counter not found"
    }

    Even better, you can do that in one line in PS:

    try{Get-Counter -Counter '\W3SVC_W3WP(*)\Active Requests'  -ErrorAction stop} catch {}

    With that, you will either get a count or nothing in reply, and I will leave you to do further tweaking to suit your need for "monitoring" this and responding as you may desire.

    Getting the count per app pool

    But going back to your original intent, you wanted to watch things per app pool. Here again there is good news/bad news.

    First the good news: the output above WILL list not only the total for all pools but ALSO the count PER app pool. Sweet. Here's an example:

    Timestamp                 CounterSamples
    ---------                 --------------
    3/16/2020 11:30:03 AM     \\yourmachine\w3svc_w3wp(_total)\active requests :
                              0
    
                              \\yourmachinename\w3svc_w3wp(24240_yourapppoolname)\active requests :
                              0

    But if you may want to get that count for just a given app pool, and so you may want to run a variant of that command to request just a given pool.

    That's where there's some more bad news: as the output of the command above shows, the app pool in the counter shown (w3svc_w3wp(24240_yourapppoolname)) is technically showing the the given app pool INSTANCE name. It's not just the app pool name but also the windows process id for that specific instance of that app pool. So while you may wish you could just request the count for the pool, you can't. You need to find what instances exist for that pool (there may be more than one, for instance while an app pool is recycling).

    Again, I will leave you to dig into that for your needs. I just wanted to raise the concern for you.

    Only showing those that DO have a count gt than some some number

    But despite that challenge, I will say that you can do still more with what these counters uncover. For instance, you may well wish to only see which app pools have a count of requests that exceeds some number, or you may want to remove all those from the list which have 0 requests. You could do either with code like the following (which can just be run at the powershell command line, as the 2 lines):

    $Counter = Get-Counter -Counter "\W3SVC_W3WP(*)\Active Requests"
    $Counter.CounterSamples | Where-Object { $_.CookedValue -gt "0" }

    Showing a list of app pools with reverse descending count of requests

    You can even get it to list the results in reverse descending order by count of requests:

    $Counter = Get-Counter -Counter "\W3SVC_W3WP(*)\Active Requests"
    $Counter.CounterSamples | Where-Object { $_.CookedValue -gt "0" } | Sort-Object -Property CookedValue -Descending | Format-Table -Property Path, InstanceName, CookedValue -AutoSize

    Some more related observations/info you can obtain

    Finally, you may wonder where I got that info. You can see more detail about the counters for EACH pool, with this:

    >> $Counter = "\W3SVC_W3WP(*)\Active Requests"
    >> $Data = Get-Counter $Counter
    >> $Data.CounterSamples | Format-List -Property *

    Conclusion

    So with all that, I hope you find that you can get what you want. It was a worthwhile exercise for me to research and document this, for myself either way (and perhaps it will benefit still others). But if you (or others) have feedback, or questions, I welcome it.

    Finally, if you may feel that this is a better answer than the one that you marked (since it doesn't have the negative performance impact of that one), we can see if it becomes locked again, and perhaps rovistar would be able to unlock it again.

    /charlie
  • Re: powershell monitor if requests are occomulating under an appPool

    Jun 02, 2020 07:31 PM|carehart|LINK

    Hey tamimh, how did things sort out? Did my last, extended answer prove helpful? Nothing is marked here as an answer, which may help future readers who find this thread.

    /charlie