Monitor top 10 processes using up the most memory
-
- Posts: 27
- Joined: Mon Nov 17, 2014 12:29 pm
Monitor top 10 processes using up the most memory
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.
Hopefully this make sense.
Re: Monitor top 10 processes using up the most memory
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!
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
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) -
and, for Windows (powershell) -
Now, for memory, you should be able to use these modified versions.
Linux -
Windows -
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
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
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
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
-
- Posts: 27
- Joined: Mon Nov 17, 2014 12:29 pm
Re: Monitor top 10 processes using up the most memory
rkennedy:
This will work from me. Thanks for your input.
This will work from me. Thanks for your input.
Re: Monitor top 10 processes using up the most memory
No problem. Do you have any questions on how to implement this or are you good to go?
Former Nagios Employee
Re: Monitor top 10 processes using up the most memory
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
Sure, are you looking to check memory or CPU, and for OS?
Former Nagios Employee
Re: Monitor top 10 processes using up the most memory
yeah i want to check both CPU and memory for windows and Linux servers.rkennedy wrote:Sure, are you looking to check memory or CPU, and for OS?
Re: Monitor top 10 processes using up the most memory
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 -
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.
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 -
Former Nagios Employee
Re: Monitor top 10 processes using up the most memory
Hi rkennedy,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 -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.Code: Select all
topcpu = cmd /c echo scripts\powershell\topcpu.ps1; exit(0) | powershell.exe -command -
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}} "