Page 1 of 2
Monitor top 10 processes using up the most memory
Posted: Tue Jan 12, 2016 11:19 am
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.
Re: Monitor top 10 processes using up the most memory
Posted: Tue Jan 12, 2016 12:18 pm
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!
Re: Monitor top 10 processes using up the most memory
Posted: Tue Jan 12, 2016 12:38 pm
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
Re: Monitor top 10 processes using up the most memory
Posted: Tue Jan 12, 2016 4:37 pm
by digitallife
rkennedy:
This will work from me. Thanks for your input.
Re: Monitor top 10 processes using up the most memory
Posted: Tue Jan 12, 2016 4:44 pm
by rkennedy
No problem. Do you have any questions on how to implement this or are you good to go?
Re: Monitor top 10 processes using up the most memory
Posted: Mon Mar 21, 2016 8:11 am
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,
Re: Monitor top 10 processes using up the most memory
Posted: Mon Mar 21, 2016 11:34 am
by rkennedy
Sure, are you looking to check memory or CPU, and for OS?
Re: Monitor top 10 processes using up the most memory
Posted: Mon Mar 21, 2016 11:39 am
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.
Re: Monitor top 10 processes using up the most memory
Posted: Mon Mar 21, 2016 3:52 pm
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.
Re: Monitor top 10 processes using up the most memory
Posted: Tue Mar 22, 2016 11:19 am
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}} "