Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

These are pretty much questions from a newbie. (I have looked at the other related questions and answers, but none seemed to be exactly answering my own original questions.)

I need to remotely retrieve, parse, and report in a Java program the CPU and memory usage of a Linux machine. These should be done in two separate commands instead of one to decouple them to make it easier if one command needs to be changed in the future.

So my questions are:

  • What is the best command to use to retrieve the total CPU usage of a machine for this purpose?
  • What is the best command to use to retrieve the total memory usage of a machine for this purpose?

By best, I mean the output of the commands are:

  • Standard (independent/should be the same across the Linux flavours -- not mandatory, but would be good. target OS though is RHEL 5)
  • Can easily be parsed (not cluttered with other information I am not interested in).

Thanks!

share|improve this question
add comment

8 Answers

up vote 1 down vote accepted

The top command displays both memory and CPU information along with other statistics.

If you want to call it from a script or application use the '-n 1' flag so that it does not run in interactive mode.

e.g to get CPU usage run

 top -n 1 | grep "Cpu"
 (result)
 Cpu(s):  1.6%us,  1.8%sy,  0.0%ni, 92.1%id,  0.6%wa,  0.1%hi,  3.8%si,  0.0%st

for memory usage call

top -n 1 | grep "Mem"
(result)
Mem:   1035240k total,   773088k used,   262152k free,   160348k buffers
share|improve this answer
add comment

Rather than re-inventing the wheel, I would suggest using an SNMP daemon on the system you intend to monitor and an SNMP client library in your Java application.

Both of the items that you wish to monitor are included in net-snmp's standard MIB/OIDs.

You then needn't worry about extracting the variables you need from the output of other utilities or the portability of how you're going to transport that information safely (presumably a remote shell).

share|improve this answer
add comment

If you really must parse the output from a console command, you probably want to do as less piping and grepping in it as possible.

The command that probably has the easiest output you can parse is vmstat.

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 4  0    420  54512  20408 1504148    0    0  1193   994  530 1397 10  6 76  7

As far as memory is concerned, mind that the 'free' above is the amount of memory that is free, without counting buffers and cache as free, so you probably want to add buff and cache to free to get the 'real' free amount of MiB's (i.e. physical memory that is directly available to the system for use).

Below CPU, the us, sy, id and wa values correspond with 'user', 'system', 'idle' and 'iowait'. Like all other tools listed, vmstat shows you the status at a single point in time. Be sure to refresh often ;-)

share|improve this answer
add comment

The "parsing" part of the answer involves playing with standard output and string formatting anyway, unless you use SNMP which (if it is an option) could give you a lot more informations.

Apart from that, the "free" command shows numbers about used memory, "df" about disk usage, and "top" about different values.

share|improve this answer
add comment

top

sysstat

both command will give best result from u H/W status "top" is best with RHEL

share|improve this answer
    
Why, exactly, is top best with RHEL? –  wzzrd Jun 22 '09 at 9:21
add comment

The "True Unix way" is to make scripts using available system information commands: free, ps, w, vmstat, iostat, ifstat, netstat, etc (hint : don't use "top", it's for interactive use, dammit!)

The "Java way" is to use a library, SNMP comes naturally to mind first but it could be too big a hammer to crack such a small nut.

So :

  • if your application won't extend much, go the easy way, use "ps", "w", parse "/proc/meminfo", etc.
  • if your application should evolve beyond this basic need, go SNMP.
share|improve this answer
add comment

If your collector is written in Java you could consider integrating Sigar into your application. Sigar is the library that the Hyperic monitoring tool uses to collect operating system statistics.

share|improve this answer
add comment

I like htop for its color and sorting

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.