Linux sar Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux sar (System Activity Reporter) command to monitor and analyze system performance metrics. The sar command is a powerful tool that collects, reports, and saves system activity information, providing a comprehensive view of various performance metrics, including CPU utilization, memory usage, network traffic, and disk I/O. You will start by understanding the purpose of the sar command and how to install the required sysstat package. Then, you will explore the different options and flags available with the sar command to analyze specific system performance aspects. Finally, you will practice using the sar command to monitor and interpret system performance data.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") subgraph Lab Skills linux/apt -.-> lab-422900{{"`Linux sar Command with Practical Examples`"}} linux/ps -.-> lab-422900{{"`Linux sar Command with Practical Examples`"}} linux/top -.-> lab-422900{{"`Linux sar Command with Practical Examples`"}} linux/free -.-> lab-422900{{"`Linux sar Command with Practical Examples`"}} end

Understand the sar Command and Its Purpose

In this step, you will learn about the sar (System Activity Reporter) command in Linux and understand its purpose in system monitoring and performance analysis.

The sar command is a powerful tool that collects, reports, and saves system activity information. It provides a comprehensive view of various system performance metrics, including CPU utilization, memory usage, network traffic, and disk I/O. The sar command is part of the sysstat package, which is typically pre-installed on most Linux distributions.

To install the sysstat package, run the following command:

sudo apt-get update
sudo apt-get install sysstat

Once the installation is complete, you can start using the sar command to monitor your system's performance.

Example usage:

sudo sar -u 1 5

This command will display 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: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 shows the CPU utilization for the system, with the %user, %nice, %system, %iowait, %steal, and %idle metrics displayed.

In the next step, you will explore the various options and flags available with the sar command to analyze different system performance metrics.

Explore sar Command Options and Flags

In this step, you will explore the various options and flags available with the sar command to analyze different system performance metrics.

The sar command provides a wide range of options to collect and display specific system performance data. Some of the commonly used options include:

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

Let's try some of these options:

sudo sar -u 1 5

This command will display 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 try the -r option to display memory utilization:

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 shows various memory-related metrics, such as free memory, used memory, buffer, and cache.

You can explore other options like -d, -n, -b, and -p to analyze different system performance aspects.

Remember, you can always use the sar --help command to get a list of all available options and their descriptions.

Analyze System Performance Metrics Using sar

In this step, you will learn how to analyze various system performance metrics using the sar command.

The sar command provides a wide range of options to collect and display specific system performance data. Let's explore some common use cases:

  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 example:

sudo sar -u -r -d 1 10

This command will display CPU, memory, and disk I/O statistics every 1 second for 10 iterations.

The sar command provides a wealth of information, and you can use it to identify performance bottlenecks, monitor system health, and troubleshoot issues in your Linux environment.

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.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like