sar Command in Linux

Introduction

In this tutorial, you will master the Linux sar (System Activity Reporter) command, a crucial tool for system administrators. Learn how to monitor and analyze system performance metrics effectively. The sar command is a robust utility that gathers, reports, and stores system activity data, providing a comprehensive overview of key performance indicators like CPU utilization, memory usage, network traffic, and disk I/O. We'll begin by exploring the purpose of the sar command and the installation of the necessary sysstat package. Next, you'll delve into the various options and flags available within the sar command to analyze specific system performance aspects. Finally, you'll gain hands-on experience using the sar command to monitor and interpret system performance data, enabling you to proactively manage your Linux systems.

Understand the sar Command and Its Purpose

In this section, you will discover the sar (System Activity Reporter) command within Linux and understand its vital role in system monitoring and performance analysis for any systemadmin.

The sar command is a potent tool that collects, reports, and archives system activity data. It offers a holistic view of different system performance metrics, including CPU utilization, memory usage, network traffic, and disk I/O. The sar command is included as part of the sysstat package, typically pre-installed on most Linux distributions, simplifying the process for any aspiring root user.

To install the sysstat package, execute the following commands:

sudo apt-get update
sudo apt-get install sysstat

Upon successful installation, you can begin utilizing the sar command to observe your system's performance.

Example usage:

sudo sar -u 1 5

This command will display the CPU utilization at 1-second intervals for a total of 5 iterations.

Example output:

Linux 5.15.0-1023-aws (ip-172-31-19-178)   01/24/2023      _x86_64_        (2 CPU)

01:53:46 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
01:53:47 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:53:48 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:53:49 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:53:50 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:53:51 PM     all      0.00      0.00      0.00      0.00      0.00    100.00

The output presents the CPU utilization for the system, showcasing the %user, %nice, %system, %iowait, %steal, and %idle metrics.

In the subsequent section, you will investigate the diverse options and flags provided by the sar command to scrutinize various system performance metrics.

Explore sar Command Options and Flags

In this section, you'll discover the diverse options and flags offered by the sar command to analyze various system performance metrics, improving your capabilities as a systemadmin.

The sar command boasts a comprehensive array of options for gathering and displaying specific system performance data. Some commonly employed options include:

  • -u: Display CPU utilization details
  • -r: Show memory utilization information
  • -d: Present disk I/O statistics
  • -n: Reveal network statistics
  • -b: Show I/O and transfer rate statistics
  • -p: Display partition statistics
  • -q: Display run queue length and load average

Let's put some of these options into practice:

sudo sar -u 1 5

This command will present the CPU utilization every 1 second for 5 iterations.

Example output:

Linux 5.15.0-1023-aws (ip-172-31-19-178)   01/24/2023      _x86_64_        (2 CPU)

01:55:46 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
01:55:47 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:55:48 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:55:49 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:55:50 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
01:55:51 PM     all      0.00      0.00      0.00      0.00      0.00    100.00

Now, let's utilize the -r option to display memory utilization details:

sudo sar -r 1 5

Example output:

Linux 5.15.0-1023-aws (ip-172-31-19-178)   01/24/2023      _x86_64_        (2 CPU)

01:56:46 PM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
01:56:47 PM   1906732    113236     5.61        0     43484    124148      6.11     67620     45616         0
01:56:48 PM   1906732    113236     5.61        0     43484    124148      6.11     67620     45616         0
01:56:49 PM   1906732    113236     5.61        0     43484    124148      6.11     67620     45616         0
01:56:50 PM   1906732    113236     5.61        0     43484    124148      6.11     67620     45616         0
01:56:51 PM   1906732    113236     5.61        0     43484    124148      6.11     67620     45616         0

This output presents an array of memory-related metrics, such as free memory, used memory, buffer, and cache, which are crucial for a systemadmin.

You can investigate other options like -d, -n, -b, and -p to analyze different aspects of system performance, helping you become a proficient systemadmin.

Always remember that you can use the sar --help command to obtain a list of all available options and their descriptions, expanding your skillset as a root user.

Analyze System Performance Metrics Using sar

In this section, you will discover how to analyze various system performance metrics using the sar command, a vital skill for any Linux systemadmin.

The sar command grants a broad spectrum of options for collecting and presenting specific system performance data. Let's explore some common practical scenarios:

  1. CPU Utilization:
sudo sar -u 1 10

This command will display the CPU utilization every 1 second for 10 iterations.

  1. Memory Utilization:
sudo sar -r 1 10

This command will display the memory utilization every 1 second for 10 iterations.

  1. Disk I/O Statistics:
sudo sar -d 1 10

This command will display the disk I/O statistics every 1 second for 10 iterations.

  1. Network Statistics:
sudo sar -n DEV 1 10

This command will display the network interface statistics every 1 second for 10 iterations.

  1. I/O and Transfer Rate Statistics:
sudo sar -b 1 10

This command will display the I/O and transfer rate statistics every 1 second for 10 iterations.

You can also combine multiple options to analyze different performance metrics simultaneously. For instance:

sudo sar -u -r -d 1 10

This command will display CPU, memory, and disk I/O statistics every 1 second for 10 iterations, providing a holistic view for the systemadmin.

The sar command delivers a wealth of information, which can be leveraged to identify performance bottlenecks, monitor system health, and troubleshoot issues within your Linux environment, making it an essential tool for any systemadmin.

Summary

In this lab, you will learn about the sar (System Activity Reporter) command in Linux and understand its purpose in system monitoring and performance analysis. You will explore the various options and flags available with the sar command to analyze different system performance metrics, such as CPU utilization, memory usage, network traffic, and disk I/O. The sar command is a powerful tool that collects, reports, and saves system activity information, providing a comprehensive view of your system's performance.

400+ Linux Commands