LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-01-2008, 10:48 AM   #1
helptonewbie
Member
 
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
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
 
Old 05-01-2008, 02:58 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 05-01-2008, 03:24 PM   #3
helptonewbie
Member
 
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, 06:09 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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


Reply



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 05:22 AM
BASH Script to get cpu idle % pgb205 Programming 5 12-25-2007 10:34 PM
Add pixel values and percentages in CSS trainpic Programming 3 06-30-2006 10:41 AM
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 07:32 AM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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