Check Windows Process memory

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Check Windows Process memory

Post 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?
Everybody is somebody else’s weirdo
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Check Windows Process memory

Post 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.
Former Nagios Employee
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Check Windows Process memory

Post 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.
Everybody is somebody else’s weirdo
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Check Windows Process memory

Post 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
Former Nagios Employee
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Check Windows Process memory

Post 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                                            
Everybody is somebody else’s weirdo
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Check Windows Process memory

Post 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
Everybody is somebody else’s weirdo
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Check Windows Process memory

Post 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!
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Check Windows Process memory

Post 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.
Everybody is somebody else’s weirdo
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Check Windows Process memory

Post 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.
Former Nagios Employee
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Check Windows Process memory

Post 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)
Everybody is somebody else’s weirdo
Locked