LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to get current cpu percentages in a script for nagios. (bash) (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-get-current-cpu-percentages-in-a-script-for-nagios-bash-639149/)

helptonewbie 05-01-2008 10:48 AM

Trying to get current cpu percentages in a script for nagios. (bash)
 
Hi All,
I'd like to write a script that can get the cpu percentages, thats al i require i can do the rest no problem, so really all i'm looking for is a way to get current cpu percentages, i was looking at procinfo but i believe this provides data such as percentage time and not actual percentage being currently used by the system, which i think is what is displayed in top. So if there is anything that can get that other than top, or perhaps i will have to put top into batch mode run it once and then grep out the parts i require, i don't know? There are scripts out there already for nagios that are perl or python or something else php etc etc, but nothing as yet i've found with bash that i can pull appart and set-up for my own purposes.
How can it be done
cheers,

ps - want things like sys, user, idle if it is top show me the one liner that could put it into a varible i can take it from there:

USER_CPU=`top -b -n1 | grep ^user:`|
you know i can only think if the only way is to use top that it will be along those lines...cheers

MensaWater 05-01-2008 02:58 PM

vmstat provides the information of percentages (user, system, idle and wait) along with other information.

See man page for details.

Typically you want to run it a few iterations and ignore the first line.
e.g.
vmstat 5 5

However, I think most people monitor load rather than %. (That is what we do here.)

You also might want to look at the NRPE Plugin designed for Linux/UNIX. It has a some tools built in that might do the monitoring you want once confirgured.

helptonewbie 05-01-2008 03:24 PM

Hi there,
yes i'm already using nrpe, and i'm using the check_load already but load checking is not alwasy that great as it measures the load in the number of processes that are wanting to use the processor rather than telling me any details of how much the processor is actually doing, and there isn't a default nagios plugin to do this. although people have written some perl and python script looking at nagiosexchange website, i prefer to write my own stuff in bash most of the time, then i can mkae it perfectly to my needs. As i will look into vmstat..cheers for that.

chrism01 05-01-2008 06:09 PM

The prob with using CPU % is it varies enormously and rapidly. Try watching top for even 2 mins to see what I mean.
That's why people use load instead. It's smoother, so less likely to trigger unnecessary alarms.


All times are GMT -5. The time now is 03:13 AM.