Wednesday, June 22, 2011

Monitoring on Unix from scratch

Linux, and in general many Unix-like systems, bundle lots of useful command line tools for monitoring the resources of a machine for performance's sake, with tracked parameters such as occupied memory, CPU utilization, or disk requests.
This list of commands, complete with flags and sample output, should be run while there is some kind of load on the system, in order to find out for example which resources are scarce (usually I/O) and which are instead not the bottleneck and shouldn't be upgraded.
There is also a different family of tools which run their workloads over an empty system, like Iozone and other benchmarks; in this article our interest is over monitoring tools that run over an existing production system.

top
top is maybe the most famous monitoring tool, which Linux users like me commonly use at home. By default, it works in real time, showing the processes ordered by their CPU utilization.
You can use the -d $i flag to change the updating interval, but more importantly -b to get top measurements on the standard output (stop it with CTRL-C).
[11:03:58][giorgio@Desmond:~]$ top -b > top.log
^C[11:04:10][giorgio@Desmond:~]$ head top.log
top - 11:04:09 up  1:09,  3 users,  load average: 0.00, 0.08, 0.12
Tasks: 166 total,   1 running, 164 sleeping,   0 stopped,   1 zombie
Cpu(s):  6.8%us,  2.0%sy,  0.0%ni, 88.0%id,  3.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3344984k total,  2202872k used,  1142112k free,   451016k buffers
Swap:  3905532k total,        0k used,  3905532k free,   929932k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
  920 root      20   0 77108  19m 9348 S    2  0.6   2:16.51 Xorg               
 2515 giorgio   20   0  156m  16m  12m S    2  0.5   0:06.72 gnome-terminal     
 2933 giorgio   20   0  802m 291m  33m S    2  8.9   3:54.36 firefox-bin

free
free displays current RAM statistics. You can use the -m switch to display memory quantities in megabytes instead of 109553440-like byte amount.
By default, free runs only once: the -s 5 (or any other value) flag will make it print the information every 5 seconds.
The used and free column show the amount of used and free RAM in the system:
Read more: Web Builder Zone