Monitoring System Usage

Monitoring System Usage

Staying aware of what's happening on your machine is a big part of system administration. The two most important things to watch on a day-to-day basis are (1) disk space and (2) load on the processor(s). Run out of disk space, and many processes stop working because they have no place to create temporary data. Let a runaway program consume too much processor time, and everything else slows down.

Monitoring disk space

Anticipating the need to add disk space before you run out is an important system-administration function.

You can view both available and used space for the entire system or for any directory in the file system.

To see a summary of disk usage for the entire system:

  • df -lk

    The df command ( display free disk space ) displays information about all the disks mounted on your system. The -l option tells df to display only locally mounted file systems (omitting network drives and some special entries that are not really disks), while the -k option tells df to show disk space in kilobytes, instead of the default half-kilobyte (512-byte) units ( Figure 11.22 ).

    Figure 11.22. Annotated output from df -lk displaying free space (in kilobytes) on the system disks.

Tip

  • If you supply a directory as an argument to df , it shows a listing for only the file system containing the directory, so

    df -k .

    shows a listing only for the file system containing the current directory (which is also a tricky way of finding out which file system the current directory is located in).

    df -k /Users

    shows a listing only for the file system containing /Users . If the directory is a symlink (see "About Links [the Unix Version of Aliases]" in Chapter 5), then df reports on the file system containing the directory that the link points to, not the link itself.


To see disk usage for a particular directory:

  • du -sk directory

    The -s asks for a summary of the target directory, and the -k says to show sizes in kilobytes. Table 11.3 shows all the options for the du command.

    Table 11.3. Options for the du Command

    O PTION

    M EANING /U SE

    -H

    Follows symbolic links in the command-line arguments. (Symbolic links encountered inside directories are not followed.)

    -L

    Follows all symbolic links.

    -P

    Does not follow symbolic links.

    -a

    Displays something for every file counted.

    -k

    Displays sizes in kilobytes instead of the system default for measuring files (usually 512-byte "blocks").

    -c

    Displays a grand total at the end.

    -s

    Displays a subtotal for each directory on the command line.

    -x

    Does not follow symbolic links that point to directories on other volumes .


    To see a summary of the disk space used by puffball's home directory:

    sudo du -sk /Users/puffball

    The sudo is needed in this case because you might not have read permission on everything in the directory.

Compare with Aqua: Activity Monitor Application

Mac OS X comes with a nice GUI application called Activity Monitor, located in the /Applications/Utilities folder, which lets you monitor active processes, memory, network activity, and so on. Still, you may want to use the command-line tools for several reasons:

  • If you are logged in to the machine using ssh over the Internet and have access only to the command-line tools.

  • If you need information, such as disk usage by directory as supplied by the du command, that isn't available in Activity Monitor.

  • If you want to redirect activity or resource information to a file, send it in e-mail, or use the information in script.

  • If you are already working at the command line, in which case it may be faster to simply type top than to launch Activity Monitor.

  • If you are on another Unix system. The command-line tools for monitoring the system described in this chapter are available on virtually any Unix system you might use, while Activity Monitor is available only on Mac OS X.


To see disk usage for several directories:

  • du -sk dir1 dir2 dir3 . . .

    Simply list as many directories as you like on the command line. The -s option generates a subtotal for each one. For example, if you type

    du -sk Documents Pictures Movies

    you get the output shown in Figure 11.23 .

    Figure 11.23. Using du -sk to get disk-use subtotals for several directories.
     localhost:~ vanilla$  du -sk Documents Pictures Movies  82740  Documents 71204  Pictures 320    Movies localhost:~ vanilla$ 

    You can see how much each user is using with

    sudo du -sk /Users/*

    The shell expands the * so that du gets a list of several directories. You need to sudo to get permission to read other users' home directories.

Tip

  • Add the -c option to get a grand total of disk space used. For example:

    sudo du -skc /Users/*


Monitoring processes and load on your machine

If you have read Chapter 2, "Using the Command Line," then you already have seen two of the best tools for monitoring running processes: ps and top . (See "About Commands, Processes, and Jobs," in Chapter 2.)

Reading the man pages for ps and top yields a great deal of information about the kinds of things you can monitor on your system. Here are some highlights for the top command, which shows a wealth of information in real time. You might want to compare what we show here with the various views of system activity in the Activity Monitor application, which we mentioned earlier. For example, in Activity Monitor you may limit the display of active processes to ones associated with windows in the Aqua interface ("Windowed Processes").

More about top

Figure 11.24 is an annotated example of output from top , showing the most significant indicators of system use.

Figure 11.24. Annotated example of output from the top command.

Note how the ID of each process is displayed. If a process gets out of control, you can use the kill command (described in Chapter 2) to terminate it. You need to use sudo to kill processes you didn't start yourself. Be very careful, since you could crash the machine by killing a system process, such as the process 1 ( launchd ).

If you use top frequently, you will notice that your system spends a great deal of time doing very little. It is quite common for computers, especially desktop machines used by only a few people, to run at 90 percent idle. Most of the computing power in the world is wasted , just generating heat and performing no useful work. For ways to put those spare CPU cycles to work for a good cause, see Seti@home (http://setiathome.ssl.berkeley.edu), distributed.net (www.distributed.net), and Xgrid (www.apple.com/server/macosx/features/ xgrid .html).

Another noteworthy command is uptime , which shows the same thing as the first line of output from the w command. The top command also displays the information from uptime in its first few lines, as well as a great deal of other information.

To see system load and uptime:

  • uptime

    This gives you a single line of output, 9:42PM up 3 days, 22:20, 6 users , load averages: 0.42, 0.89, 0.85 that shows you the current time, the length of time the system has been up, the number of users logged in, and three load averages taken over the past 1, 5, and 15 minutes. The load average is the average number of processes waiting for processor time. In a multitasking system like Unix, only one process is actually running on each CPU at any given instant. The operating system allows each process to run for a tiny fraction of a second and then lets another process run. Because this sharing is enforced by the operating system, it is called preemptive multitasking and is one of the major reasons for Unix's stability (as opposed to cooperative multitasking in Mac OS 7 through 9, in which applications were supposed to play well together without supervision).

vm_statTool for Monitoring Memory Usage

Another command worth knowing about is vm_stat ( virtual memory statistics ), often called vmstat on other Unix systems. vm_stat reports on several aspects of memory usage, in real time.

vm_stat is normally executed as

vm_stat 5

which gives a summary since startup and then a new line every 5 seconds. It keeps running until you press .

Interpreting the output of vm_stat takes some experience, but the pageout column is similar to the data shown by top that column should be zero most of the time. Otherwise, either your system is short of memory or something is using it up.




Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net