• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1756
  • 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:
<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
0
jinkaz
Asked:
jinkaz
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}'
99.8%
0
 
Dushan De SilvaCommented:
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
0
 
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.
0
 
ahoffmannCommented:
I'd prefer vmstat 'cause it uses the core /proc files
0
 
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.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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