• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1763
  • Last Modified:

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:
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.

4 Solutions
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}'
Dushan De SilvaTechnology ArchitectCommented:
You can try with Big Brother.
See http://bb4.com/

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
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.)
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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.
I'd prefer vmstat 'cause it uses the core /proc files
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now