Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-01-2008, 11:48 AM   #1
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Question 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

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
Old 05-01-2008, 03:58 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,075
Blog Entries: 14

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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.
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.
Old 05-01-2008, 04:24 PM   #3
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
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.
Old 05-01-2008, 07:09 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nagios check_sun_hardware script alek66 Solaris / OpenSolaris 2 02-14-2008 06:22 AM
BASH Script to get cpu idle % pgb205 Programming 5 12-25-2007 11:34 PM
Add pixel values and percentages in CSS trainpic Programming 3 06-30-2006 11:41 AM
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 08:32 AM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 01:50 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration