Page 1 of 1

script to show top processes of high CPU load

Posted: Fri Jan 29, 2016 9:35 am
by nagmoto
I haven't done search on google.
1st browsing check on https://exchange.nagios.org/directory/P ... e-and-Load does not list the script I need.
Is there a script that can do this ?
If CPU load is over threshold then display the top couple CPU loaded processes.
The goal is to show processes occasionally ran and causing cpu load to spike up.

Thanks for the pointer.

Re: script to show top processes of high CPU load

Posted: Fri Jan 29, 2016 11:53 am
by hsmith
I don't know of one off the top of my head, but it wouldn't be hard to script in bash.

Use this sort of logic:

If cpu usage is under a certain threshold, echo the message "CPU Usage OK."
If cpu usage is not okay, echo the top process from a top command.

Something like this could get you that information:

Code: Select all

 top | head -n10 | tail -n3 | awk '{print $10 " " $11 " " $13}' 
It's not the prettiest, but I hope it gives you an idea.

Re: script to show top processes of high CPU load

Posted: Tue Feb 02, 2016 12:35 pm
by rkennedy
I wrote a couple lines to do this a while back, while it's not pretty, and I wouldn't call it a script - it will do what you're looking for. You'll want to set up an event_handler which will trigger this each time a state change occurs on the cpu check. The file it logs to is C:\Program Files\NSClient++\scripts\powershell\topcpu.txt.

I call it... topcpu.ps1!

Code: Select all

date | Out-File "C:\Program Files\NSClient++\scripts\powershell\topcpu.txt" -append
Get-Process | Sort WS | Select -last 10 | Out-File "C:\Program Files\NSClient++\scripts\powershell\topcpu.txt" -append
Add this under your external scripts configuration -

Code: Select all

;topcpu external script
topcpu = cmd /c echo scripts\powershell\topcpu.ps1; exit(0) | powershell.exe -command -
If you're looking for linux, here's a bash script that will log to checktopcpu.txt in the same directory -

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

Re: script to show top processes of high CPU load

Posted: Tue Feb 02, 2016 2:28 pm
by nagmoto
[quote="rkennedy"]I wrote a couple lines to do this a while back, while it's not pretty, and I wouldn't call it a script - it will do what you're looking for. You'll want to set up an event_handler which will trigger this each time a state change occurs on the cpu check. The file it logs to is C:\Program Files\NSClient++\scripts\powershell\topcpu.txt.

I had to read this post to understand how to execute the topcpu script on remote machine via NRPE.

https://blog.karssen.org/2010/09/14/nag ... -machines/

Re: script to show top processes of high CPU load

Posted: Tue Feb 02, 2016 2:35 pm
by rkennedy
Ah - I didn't write much instructions as I didn't know if you needed them or not.

Are you using windows or linux? Using the guide posted, were you able to get this working?

Re: script to show top processes of high CPU load

Posted: Tue Feb 02, 2016 5:30 pm
by nagmoto
rkennedy wrote:Ah - I didn't write much instructions as I didn't know if you needed them or not.

Are you using windows or linux? Using the guide posted, were you able to get this working?
I have both windows and Unix clients. I will test this approach in my test lab first.
Also I am thinking of inserting the login of picking out top process into following script
https://github.com/shamil/nagios-plugin ... eck_cpu.sh


This ticket can be closed, I just need to pick the approach with less impact.

Thanks again.

Re: script to show top processes of high CPU load

Posted: Wed Feb 03, 2016 10:02 am
by rkennedy
Sounds good! Glad to see this will work out for you.

I'll close this out now, but feel free to open a new thread if you ever need assistance in the future!