Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux vmstat Command: Monitor System Resources

vmstat, short for virtual memory statistics, is a versatile command-line utility in Linux that provides information about system processes, memory, swap, I/O, and CPU activity. This tutorial will introduce you to the vmstat command and its usage.

Running vmstat

To run vmstat, simply type vmstat in your terminal, followed by an optional interval in seconds:

vmstat [interval]

For example, to display statistics with an interval of 5 seconds, use:

vmstat 5

By default, vmstat displays a single set of statistics since the last reboot. If an interval is provided, vmstat will continuously report statistics at the specified interval.

Understanding vmstat output

The output of the vmstat command consists of several sections:

  • Procs

    • r: The number of runnable processes (running or waiting to run).
    • b: The number of processes in uninterruptible sleep (waiting for I/O to complete).
  • Memory

    • swpd: The amount of virtual memory (swap) used in kilobytes.
    • free: The amount of idle memory in kilobytes.
    • buff: The amount of memory used as buffers in kilobytes.
    • cache: The amount of memory used as cache in kilobytes.
  • Swap

    • si: The amount of memory swapped in from disk per second.
    • so: The amount of memory swapped out to disk per second.
  • I/O

    • bi: Blocks received from a block device (blocks input) per second.
    • bo: Blocks sent to a block device (blocks output) per second.
  • System

    • in: The number of interrupts per second, including the clock.
    • cs: The number of context switches per second.
  • CPU

    • us: The percentage of time spent running non-kernel code (user time).
    • sy: The percentage of time spent running kernel code (system time).
    • id: The percentage of time spent idle.
    • wa: The percentage of time spent waiting for I/O.
    • st: The percentage of time stolen from a virtual machine (only visible in a virtualized environment).

Examples of vmstat usage

  • Display a single set of statistics since the last reboot:
vmstat
  • Display statistics every 2 seconds:
vmstat 2
  • Display statistics in megabytes instead of kilobytes:
vmstat -S m
  • Display extended statistics, including detailed disk I/O:
vmstat -d
  • Display disk partition statistics:
vmstat -p PARTITION

Replace PARTITION with the device name of the partition you want to check (e.g., /dev/sda1).

In conclusion, the vmstat command is a valuable tool for monitoring system performance and diagnosing issues related to memory, CPU, and I/O utilization. Familiarizing yourself with the vmstat output and its various options will help you better understand your system's behavior and performance.

  1. How to use the Linux vmstat command:

    • Description: vmstat reports information about system processes, memory, paging, block I/O, traps, and CPU activity.
    • Code:
      # Example: Basic usage of vmstat
      vmstat
      
  2. Interpreting output from the vmstat command:

    • Description: Understand the various columns and their meanings in the vmstat output.
    • Code:
      # Example: Interpret vmstat output
      man vmstat  # Check the manual for detailed information
      
  3. Monitoring CPU usage with vmstat in Linux:

    • Description: Analyze CPU-related statistics including user, system, idle, and wait time.
    • Code:
      # Example: Monitor CPU usage with vmstat
      vmstat 1  # Refresh every 1 second
      
  4. Viewing memory statistics using vmstat:

    • Description: Observe memory-related metrics such as free, used, and buffered memory.
    • Code:
      # Example: View memory statistics with vmstat
      vmstat -s
      
  5. I/O and disk activity analysis with vmstat:

    • Description: Investigate I/O and disk-related metrics, including read and write operations.
    • Code:
      # Example: Analyze I/O and disk activity with vmstat
      vmstat -d
      
  6. Network and interrupts monitoring with vmstat:

    • Description: Examine network-related statistics and interrupts.
    • Code:
      # Example: Monitor network and interrupts with vmstat
      vmstat -i
      
  7. Setting sampling intervals in vmstat on Linux:

    • Description: Adjust the sampling interval to control the frequency of updates.
    • Code:
      # Example: Set sampling intervals with vmstat
      vmstat 5  # Refresh every 5 seconds
      
  8. Customizing output format with vmstat:

    • Description: Tailor the output format by specifying options like -n.
    • Code:
      # Example: Customize output format with vmstat
      vmstat -n 1  # Do not display header and refresh every 1 second
      
  9. Automating system resource monitoring with vmstat:

    • Description: Integrate vmstat into scripts or monitoring tools for automated resource tracking.
    • Code:
      # Example: Automate monitoring with vmstat
      vmstat 1 > output.txt &  # Run in the background and save output to a file