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!
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).
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
for memory usage call
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.
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 ;-)
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.
top
sysstat
both command will give best result from u H/W status "top" is best with RHEL
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 :
I like
htop
for its color and sortingIf 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.