script to show top processes of high CPU load

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

script to show top processes of high CPU load

Post 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.
Last edited by nagmoto on Sat Jan 30, 2016 3:46 am, edited 2 times in total.
User avatar
hsmith
Agent Smith
Posts: 3539
Joined: Thu Jul 30, 2015 11:09 am
Location: 127.0.0.1
Contact:

Re: script to show top processes of high CPU load

Post 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.
Former Nagios Employee.
me.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: script to show top processes of high CPU load

Post 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
Former Nagios Employee
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

Re: script to show top processes of high CPU load

Post 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/
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: script to show top processes of high CPU load

Post 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?
Former Nagios Employee
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

Re: script to show top processes of high CPU load

Post 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.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: script to show top processes of high CPU load

Post 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!
Former Nagios Employee
Locked