iostat - Linux


Overview

iostat is a command-line utility used in Unix-based systems to monitor system input/output device loading by observing the time devices are active in relation to their average transfer rates. This command provides valuable data allowing administrators to optimize system performance and troubleshoot potential bottlenecks in disk I/O. It is particularly useful in environments where efficient data storage and retrieval are critical, such as database management and web servers.

Syntax

The basic syntax of the iostat command is as follows:

iostat [options] [device...] [interval] [count]
  • device: Specifies the device(s) to monitor.
  • interval: Sets the delay between reports in seconds.
  • count: Number of reports to generate.

Options/Flags

iostat comes with several options and flags that modify its behavior:

  • -c: Display only the CPU usage statistics.
  • -d: Show only the device statistics.
  • -x: Display extended statistics that provide deeper insights into hardware performance.
  • -k: Output statistics in kilobytes per second rather than blocks per second, which is the default.
  • -m: Output statistics in megabytes per second.
  • -p [device]: Show statistics for block devices and all their partitions.
  • -t: Include a timestamp in each report, helpful for long-term monitoring.
  • -V: Display the version of the iostat program.

Defaults are usually sufficient for casual monitoring, but extended and device-specific flags can be very useful in more focused analyses.

Examples

Here are some common usage scenarios for iostat:

  1. Basic Disk and CPU Usage

    iostat
    
  2. Extended Device Statistics with 2-second Intervals

    iostat -dx 2
    
  3. Monitoring Specific Devices

    iostat -p sda 5
    
  4. Display Disk and CPU Statistics in MB/s

    iostat -m 2 10
    

Common Issues

  • High iowait: This can indicate disk bottlenecks. Using -x can help identify which devices are contributing to iowait.
  • Misinterpretation of outputs: Make sure to understand that iostat reports average statistics since the last reboot when run without an interval. Use intervals to get current insights.

Integration

Combine iostat with other commands to create powerful monitoring scripts or one-liners. Here’s an example of logging disk statistics every 5 seconds, appending to a log file:

while true; do iostat -dx 5 12 >> disk_stats.log; done

Use awk or grep with iostat to filter and process output for automated monitoring or alerting scripts.

  • vmstat: Reports information about processes, memory, paging, block IO, traps, and CPU activity.
  • mpstat: Display the CPU statistics.
  • top: Provides a dynamic real-time view of a running system.

For more detailed information, consult the man page for iostat (run man iostat in your terminal) or visit Kernel.org’s iostat documentation.