Monitor top 10 processes using up the most memory

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
digitallife
Posts: 27
Joined: Mon Nov 17, 2014 12:29 pm

Monitor top 10 processes using up the most memory

Post by digitallife »

I'm monitoring the memory via NRPE which works great. However, when the threshold is met, I have no idea which processes are running high on memory. So, is there a way for Nagios to display the top 10 processes using the most memory when the check_mem hit it threshold? Something like taking a snap shot(top) of the environment and pipe to a file.

Hopefully this make sense.
jolson
Attack Rabbit
Posts: 2560
Joined: Thu Feb 12, 2015 12:40 pm

Re: Monitor top 10 processes using up the most memory

Post by jolson »

Not without a wrapper - I had this conversation with some of the people in IRC, and it's a difficult thing to accomplish.

What you can do is check on the memory usage of a particular process with a plugin such as this one:
https://exchange.nagios.org/directory/P ... ge/details

There is no way I know of to check on the memory usage of all processes as a whole and snapshot the top memory hogs when a limit is breached - you may need an event handler or a wrapper script to achieve that functionality.

Thanks!
Twits Blog
Show me a man who lives alone and has a perpetually clean kitchen, and 8 times out of 9 I'll show you a man with detestable spiritual qualities.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Monitor top 10 processes using up the most memory

Post by rkennedy »

Are the machines you want to check windows or linux?

I wrote a few lines for a ticket I had not too long ago to do the exact same thing, except for CPU. I'm sure they can be modified to work for memory instead. You will want to use this with event handlers.

For frame or reference, here they are. Linux (bash) -

Code: Select all

#!/bin/bash
date=$(date)
echo -e "$date" >> checktopcpu.txt
ps -eo pcpu,args --sort=-%cpu|head >> checktopcpu.txt
echo -e "\n" >> checktopcpu.txt
and, for Windows (powershell) -

Code: Select all

date | Out-File "C:\Program Files\NSClient++\scripts\powershell\topcpu.txt" -append
Get-Process | Sort CPU | Select -last 10 | Out-File "C:\Program Files\NSClient++\scripts\powershell\topcpu.txt" -append
Now, for memory, you should be able to use these modified versions.

Linux -

Code: Select all

#!/bin/bash
date=$(date)
echo -e "$date" >> checktopmem.txt
ps axo rss,comm,pid \
| awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } \
END { for (proc in proc_list) { printf("%d\t%s\n", \
proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}' >> checktopmem.txt
echo -e "\n" >> checktopmem.txt
Windows -

Code: Select all

date | Out-File "C:\Program Files\NSClient++\scripts\powershell\topmem.txt" -append
Get-Process | Sort WS | Select -last 10 | Out-File "C:\Program Files\NSClient++\scripts\powershell\topmem.txt" -append
Former Nagios Employee
digitallife
Posts: 27
Joined: Mon Nov 17, 2014 12:29 pm

Re: Monitor top 10 processes using up the most memory

Post by digitallife »

rkennedy:

This will work from me. Thanks for your input.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Monitor top 10 processes using up the most memory

Post by rkennedy »

No problem. Do you have any questions on how to implement this or are you good to go?
Former Nagios Employee
CloudOps
Posts: 88
Joined: Mon Feb 08, 2016 12:52 am

Re: Monitor top 10 processes using up the most memory

Post by CloudOps »

rkennedy wrote:No problem. Do you have any questions on how to implement this or are you good to go?

Hi rkennedy,

can you help me how to implement this?

Thanks,
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Monitor top 10 processes using up the most memory

Post by rkennedy »

Sure, are you looking to check memory or CPU, and for OS?
Former Nagios Employee
CloudOps
Posts: 88
Joined: Mon Feb 08, 2016 12:52 am

Re: Monitor top 10 processes using up the most memory

Post by CloudOps »

rkennedy wrote:Sure, are you looking to check memory or CPU, and for OS?
yeah i want to check both CPU and memory for windows and Linux servers.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Monitor top 10 processes using up the most memory

Post by rkennedy »

You'll need to create the corresponding script (ps1 or sh), and then create a command for it in NSClient++ / NRPE.

For example, in NSClient++, it would be similar to this -

Code: Select all

topcpu = cmd /c echo scripts\powershell\topcpu.ps1; exit(0) | powershell.exe -command -
Then, you'll need to create an event handler to trigger the newly created commands over check_nrpe / check_nt. Once those have been created, assign the event handler to the service that's checking your MEM or CPU. Any time a state change occurs, it will log the data to the corresponding file on the local client machine.
Former Nagios Employee
CloudOps
Posts: 88
Joined: Mon Feb 08, 2016 12:52 am

Re: Monitor top 10 processes using up the most memory

Post by CloudOps »

rkennedy wrote:You'll need to create the corresponding script (ps1 or sh), and then create a command for it in NSClient++ / NRPE.

For example, in NSClient++, it would be similar to this -

Code: Select all

topcpu = cmd /c echo scripts\powershell\topcpu.ps1; exit(0) | powershell.exe -command -
Then, you'll need to create an event handler to trigger the newly created commands over check_nrpe / check_nt. Once those have been created, assign the event handler to the service that's checking your MEM or CPU. Any time a state change occurs, it will log the data to the corresponding file on the local client machine.
Hi rkennedy,

i have to save the command into ps1 file?

command " date | Out-File "C:\Program Files\NSClient++\scripts\powershell\topcpu.txt" -append
Get-Process | Sort CPU | Select -last 10 | Out-File "C:\Program Files\NSClient++\scripts\powershell\topcpu.txt" -append "

then add into nsclient file? i have tried that but throwing some error.

Also i have powershell script to get process memory usage , can you guide me to get the output to display in nagios :
"get-wmiobject WIN32_PROCESS | Sort-Object -Property ws -Descending|select -first 5|Select processname, @{Name="Mem Usage(MB)";Expression={[math]::round($_.ws / 1mb)/1024}} "
Locked