Page 1 of 3

Check Windows Process memory

Posted: Mon Jan 11, 2016 11:42 am
by JohnFLi
I am in need for checking the memory usage for a specific memory process, w3wp.exe (as an example)

I havent found anything for doing that for windows. ANybody know of one?

Re: Check Windows Process memory

Posted: Mon Jan 11, 2016 11:57 am
by rkennedy
You should be able to use powershell, with the Get-Process command. From there, execute the check with NSClient and pull the information. Here's an example of the powershell Get-Process command. -

Code: Select all

PS C:\Users\rkennedy> get-process firefox

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    984     166   551664     584716  1284 1,014.38   8172 firefox
From there, filter for the type of memory you're looking for.

I was looking for a way to return the Memory (as task manager would), but this was the closest I could find.

Re: Check Windows Process memory

Posted: Mon Jan 11, 2016 4:39 pm
by JohnFLi
Great, thank you. That almost is what I am looking for.

with the w3wp process, there can be multiple instances of it running.
I found how to get the Total memory used....but any idea on how to return only the one that is using the most memory?
Basicly, there is a need to check to see if any one process (in a single instance) is using more than 1 gig of memory.

Re: Check Windows Process memory

Posted: Mon Jan 11, 2016 4:58 pm
by rkennedy
You should be able to with this -

Code: Select all

get-process vmware-vmx|sort-object -property ws -descending|select -first 1
The sort-object will filter based on the highest WS, and then only show the first result.

An example -

Code: Select all

PS C:\Users\rkennedy> get-process vmware-vmx|sort-object -property ws -descending|select -first 1

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    407      27    33692    2036532 -1832   654.69   5896 vmware-vmx

Re: Check Windows Process memory

Posted: Mon Jan 11, 2016 5:09 pm
by JohnFLi
Great, and thank you again.

Which one of those columns is what I see when looking in task manager?

Code: Select all

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                     
-------  ------    -----      ----- -----   ------     -- -----------                                     
   1884      97   287676     314068   620    33.13  13072 SomeLameProcessNametheOurDevsMade                                            

Re: Check Windows Process memory

Posted: Mon Jan 11, 2016 5:18 pm
by JohnFLi
Think I may have found it.....

Code: Select all

get-process Name of process|sort-object -property ws -descending|select Name,PrivateMemorySize -first 1
but it is still a little bit off

Re: Check Windows Process memory

Posted: Tue Jan 12, 2016 10:40 am
by lmiltchev
but it is still a little bit off
Did you mean it is a little bit off compared to what you see in the Windows Task Manager? It is normal to be a little bit off as you cannot check it at the exact same time.
If this is a working solution for you, let us know if it is all right to lock this topic. Thank you!

Re: Check Windows Process memory

Posted: Tue Jan 12, 2016 11:13 am
by JohnFLi
yeah, that's what I figured........
now i'm trying to figure out how to get that value back to Nagios.

when I send what it puts out, it gives.

Code: Select all

                                                                     WorkingSet
                                                                     ----------
                                                                      469422080
other than figuring that out, you can close this.

Re: Check Windows Process memory

Posted: Tue Jan 12, 2016 5:46 pm
by rkennedy
What command are you running to have it narrow down to that result? I looked around for ways to format things down to one line (which is what needs to be done) with powershell, but I haven't been able to find too much.

Re: Check Windows Process memory

Posted: Tue Jan 12, 2016 6:24 pm
by JohnFLi
oy.......this is killing me.... I am slowly getting closer. Let me show what I have going on so far.

The powershell script:

Code: Select all

param([int] $warn, [int] $crit)

$Output1 = ""
$LastExitCode = 0
$output = ""
$myW3WP = Get-Process w3wp |  Sort -Descending WS| select -first 1 | Measure-Object WS -Sum 
$myW3WP = ("{0:N2} " -f ($myW3WP.sum / 1gb))
 
if ($myW3WP -gt $crit)
        {
            $LastExitCode = 2
        }
    elseif ($myW3WP -gt $warn)
        {
            $LastExitCode = 1
        }
    else 
        {
            $LastExitCode = 0
        }

$output =  $LastExitCode
when the memory for the top user of w3wp (in task manager) reads 396,572 k the script reports 0.42 (close enough)