Chapter 6. System Monitoring Utilities

Contents

6.1. List of Open Files: lsof
6.2. User Accessing Files: fuser
6.3. File Properties: stat
6.4. USB Devices: lsusb
6.5. Information about a SCSI Device: scsiinfo
6.6. Processes: top
6.7. Process List: ps
6.8. Process Tree: pstree
6.9. Who Is Doing What: w
6.10. Memory Usage: free
6.11. Kernel Ring Buffer: dmesg
6.12. File Systems and Their Usage: mount, df, and du
6.13. The /proc File System
6.14. PCI Resources: lspci
6.15. System Calls of a Program Run: strace
6.16. Library Calls of a Program Run: ltrace
6.17. Specifying the Required Library: ldd
6.18. Additional Information about ELF Binaries
6.19. Interprocess Communication: ipcs
6.20. Time Measurement with time

Abstract

A number of programs and mechanisms, some of which are presented here, can be used to examine the status of your system. Also described are some utilities that are useful for routine work, along with their most important parameters.

For each of the commands introduced, examples of the relevant outputs are presented. In these examples, the first line is the command itself (after the > or # sign prompt). Omissions are indicated with square brackets ([...]) and long lines are wrapped where necessary. Line breaks for long lines are indicated by a backslash (\).

# command -x -y
output line 1
output line 2
output line 3 is annoyingly long, so long that \
    we have to break it
output line 3
[...]
output line 98
output line 99

The descriptions have been kept short to allow as many utilities as possible to be mentioned. Further information for all the commands can be found in the man pages. Most of the commands also understand the parameter --help, which produces a brief list of the possible parameters.


6.1. List of Open Files: lsof

To view a list of all the files open for the process with process ID PID, use -p. For example, to view all the files used by the current shell, enter:


tester@linux:~> lsof -p $$
COMMAND  PID   USER   FD   TYPE DEVICE    SIZE   NODE NAME
bash    5552 tester  cwd    DIR    3,3    1512 117619 /home/tester
bash    5552 tester  rtd    DIR    3,3     584      2 /
bash    5552 tester  txt    REG    3,3  498816  13047 /bin/bash
bash    5552 tester  mem    REG    0,0              0 [heap] (stat: No such \
 file or directory)
bash    5552 tester  mem    REG    3,3  217016 115687 /var/run/nscd/passwd
bash    5552 tester  mem    REG    3,3  208464  11867 \
 /usr/lib/locale/en_GB.utf8/LC_CTYPE
bash    5552 tester  mem    REG    3,3  882134  11868 \
 /usr/lib/locale/en_GB.utf8/LC_COLLATE
bash    5552 tester  mem    REG    3,3 1386997   8837 /lib/libc-2.3.6.so
bash    5552 tester  mem    REG    3,3   13836   8843 /lib/libdl-2.3.6.so
bash    5552 tester  mem    REG    3,3  290856  12204 /lib/libncurses.so.5.5
bash    5552 tester  mem    REG    3,3   26936  13004 /lib/libhistory.so.5.1
bash    5552 tester  mem    REG    3,3  190200  13006 /lib/libreadline.so.5.1
bash    5552 tester  mem    REG    3,3      54  11842 \
 /usr/lib/locale/en_GB.utf8/LC_NUMERIC
bash    5552 tester  mem    REG    3,3    2375  11663 \
 /usr/lib/locale/en_GB.utf8/LC_TIME
bash    5552 tester  mem    REG    3,3     290  11736 \
 /usr/lib/locale/en_GB.utf8/LC_MONETARY
bash    5552 tester  mem    REG    3,3      52  11831 \
 /usr/lib/locale/en_GB.utf8/LC_MESSAGES/SYS_LC_MESSAGES
bash    5552 tester  mem    REG    3,3      34  11862 \
 /usr/lib/locale/en_GB.utf8/LC_PAPER
bash    5552 tester  mem    REG    3,3      62  11839 \
 /usr/lib/locale/en_GB.utf8/LC_NAME
bash    5552 tester  mem    REG    3,3     127  11664 \
 /usr/lib/locale/en_GB.utf8/LC_ADDRESS
bash    5552 tester  mem    REG    3,3      56  11735 \
 /usr/lib/locale/en_GB.utf8/LC_TELEPHONE
bash    5552 tester  mem    REG    3,3      23  11866 \
 /usr/lib/locale/en_GB.utf8/LC_MEASUREMENT
bash    5552 tester  mem    REG    3,3   21544   9109 \
 /usr/lib/gconv/gconv-modules.cache
bash    5552 tester  mem    REG    3,3     366   9720 \
 /usr/lib/locale/en_GB.utf8/LC_IDENTIFICATION
bash    5552 tester  mem    REG    3,3   97165   8828 /lib/ld-2.3.6.so
bash    5552 tester    0u   CHR  136,5              7 /dev/pts/5
bash    5552 tester    1u   CHR  136,5              7 /dev/pts/5
bash    5552 tester    2u   CHR  136,5              7 /dev/pts/5
bash    5552 tester  255u   CHR  136,5              7 /dev/pts/5
  

The special shell variable $$, whose value is the process ID of the shell, has been used.

The command lsof lists all the files currently open when used without any parameters. Because there are often thousands of open files, listing all of them is rarely useful. However, the list of all files can be combined with search functions to generate useful lists. For example, list all used character devices:


tester@linux:~> lsof | grep CHR
bash      3838     tester    0u      CHR  136,0                 2 /dev/pts/0
bash      3838     tester    1u      CHR  136,0                 2 /dev/pts/0
bash      3838     tester    2u      CHR  136,0                 2 /dev/pts/0
bash      3838     tester  255u      CHR  136,0                 2 /dev/pts/0
bash      5552     tester    0u      CHR  136,5                 7 /dev/pts/5
bash      5552     tester    1u      CHR  136,5                 7 /dev/pts/5
bash      5552     tester    2u      CHR  136,5                 7 /dev/pts/5
bash      5552     tester  255u      CHR  136,5                 7 /dev/pts/5
X         5646       root  mem       CHR    1,1              1006 /dev/mem
lsof      5673     tester    0u      CHR  136,5                 7 /dev/pts/5
lsof      5673     tester    2u      CHR  136,5                 7 /dev/pts/5
grep      5674     tester    1u      CHR  136,5                 7 /dev/pts/5
grep      5674     tester    2u      CHR  136,5                 7 /dev/pts/5