Monitor CPU and RAM with a shell script

Hello all,

I want to write a small shell script for Linux red-hat 3.0 that will automatically monitor the CPU usage and memory.

My plan is to put the script in a cron and write to a file the results every X seconds.
In the end I want to perform an average on all results I have.

I know the command 'top' but not quite sure how to extract the data from its output.
What I'm looking for if possible is something like:
<timestamp>
CPU X% free
RAM X% free

The script should run while I'm keeping the machine busy so the I'll use the results for performance analsys.

Thanks,
jinkaz
jinkazAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duncan RoeSoftware DeveloperCommented:
You should not expect there to be any RAM free in a busy system, no matter how much you have. Linux will use it all for i/o/buffers and cache. You can choose to regard those as "free" although small values may correlate with reduced performance. For %idle, run top in batch mode, 1 iteration, take only the first 3 lines:

$ top -b -n 1|head -3
top - 07:29:09 up 7 days, 17:47, 10 users,  load average: 0.02, 0.03, 0.00
Tasks: 106 total,   1 running, 105 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1% us,  0.1% sy,  0.0% ni, 99.8% id,  0.0% wa,  0.0% hi,  0.0% si

You just want the idle, so extend the command pipe:


$ top -n 1|head -n 3|tail -n 1|awk '{print $8}'
99.8%

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dushan De SilvaTechnology ArchitectCommented:
You can try with Big Brother.
See http://bb4.com/
http://demo.bb4.com/bb/

Best solution ever I  saw is Big Brother. You can install Big brother professional server in a one machine.
 And you can installl Big brohter client on other machines.
Then you can view in web base CPU, HardDisk, EMAIL, FTP, SSH, HTTP, ..etc is up or not.
Please download windows installation from http://www.quest.com/bigbrother/DLtrial_options.asp
If you need any help regarding BB Installation plz reply to me.
BR Dushan
wnrossCommented:
rather than beating top into shap, grab your raw data from
/proc/freemem and /proc/loadavg

Now the standard for doing this is mrtg + snmp + sysstat

This gives you pretty usage graphs udated every 5 minutes.
you can also run vmstat

vmstat 1

will print off all the important stats every second (note that your processor has to run vmstat so some of the load
is the monitor itself.)
eg:
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo        in    cs      us sy id wa
 0  0   7312   7276  19456 127108    0      0     0         0    110    12  0  1 99  0
 0  0   7312   7276  19456 127108    0      0     0         0    108    11  1  0 99  0
 0  0   7312   7276  19456 127108    0      0     0         0    106    14  0  1 99  0

(Yes this is a low end system)
The key stats are swap (so = page swapped to disk, si page swapped from disk)
io (bi = blocks read, bo = blocks written)
and cs (context switches...literally how many running processes)

vmstat gives frightening stats unless you run it for longer than one count.

Oh, and its installed by default on most linux platforms
-Bill
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

jinkazAuthor Commented:
Is vmstat better than top ? by saying better I mena performance wise (I presume that both vmstat and top are accurate in reading the system stats).

I'll try putting vmstat in a cron so it will write the stats to a file every 10 seconds. hopefully it will no affect the performance too much.
ahoffmannCommented:
I'd prefer vmstat 'cause it uses the core /proc files
wnrossCommented:
If you want better information, install mrtg and sysstat, you might have to update a couple of perl scripts, but then you can get
continuously updating CPU and RAM graphs so you can monitor trends.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.