Best commands to use to find out the total CPU and memory usage of a Linux machine in the following situation

8,939

Solution 1

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

Solution 2

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

Solution 3

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:
8,939

Related videos on Youtube

His
Author by

His

Updated on September 17, 2022

Comments

  • His
    His over 1 year

    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!

  • Govindarajulu
    Govindarajulu almost 15 years
    Why, exactly, is top best with RHEL?