How to Install and use SAR on Linux

System Activity Reporter or SAR is a program utility for analyzing Linux system performances. SAR measures CPU activity, memory paging, network issues, process and thread allocation, etc.

It collects data every 10 minutes and it gathers in the /proc filesystem. In this blog post, we will use the Ubuntu 22.04 as OS. You can choose any Linux distro you want.

Installing SAR on Ubuntu 22.04 is a straightforward process; installation and configuration may take up to 10 minutes. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

We assume that you have a fresh installation of Ubuntu 22.04. That’s why we need to update the package to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install SAR

The SAR package is, by default, included in the sysstat package. To install SAR execute the following command:

sudo apt-get install sysstat

Once installed, start and enable the service:

sudo systemctl enable sysstat && sudo systemctl start sysstat

Check if the service is up and running:

sudo systemctl status sysstat

You should receive the following output:

root@host:~# sudo systemctl status sysstat
● sysstat.service - Resets System Activity Logs
     Loaded: loaded (/lib/systemd/system/sysstat.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2023-09-08 09:37:51 CDT; 1min 20s ago
       Docs: man:sa1(8)
             man:sadc(8)
             man:sar(1)
   Main PID: 8753 (code=exited, status=0/SUCCESS)
        CPU: 18ms

Sep 08 09:37:51 host.test.vps systemd[1]: Starting Resets System Activity Logs...
Sep 08 09:37:51 host.test.vps systemd[1]: Finished Resets System Activity Logs.

Step 3. Enabling SAR

After installation, SAR is not enabled by default. To enable it open the /etc/default/sysstat file with your favorite editor and modify it to look like this:

#
# Default settings for /etc/init.d/sysstat, /etc/cron.d/sysstat
# and /etc/cron.daily/sysstat files
#

# Should sadc collect system activity informations? Valid values
# are "true" and "false". Please do not put other values, they
# will be overwritten by debconf!
ENABLED="true"

Save the file, close it, and restart the service.

sudo systemctl restart sysstat

As we mentioned in the lead, the SAR collects data every 10 minutes. This can be checked and confirmed in the /etc/cron.d/sysstat file.

root@host:~# cat /etc/cron.d/sysstat
# The first element of the path is a directory where the debian-sa1
# script is located
PATH=/usr/lib/sysstat:/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin

# Activity reports every 10 minutes everyday
5-55/10 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1

# Additional run at 23:59 to rotate the statistics file
59 23 * * * root command -v debian-sa1 > /dev/null && debian-sa1 60 2

Step 4. How to use SAR

In this final step, we will provide you with some commands about SAR usage.

For example, to check the CPU usage execute the following command:

sar -u

You should get output similar to this:

root@host:~# sar -u
Linux 5.15.0-83-generic (host.test.vps)      09/08/2023      _x86_64_        (3 CPU)

06:37:51 AM  LINUX RESTART      (3 CPU)

06:40:16 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
06:50:16 AM     all      0.01      0.00      0.15      0.00      0.05     99.79
Average:        all      0.01      0.00      0.15      0.00      0.05     99.79

06:50:40 AM  LINUX RESTART      (3 CPU)

07:00:11 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
07:10:16 AM     all      0.01      0.00      0.28      0.00      0.12     99.59
07:20:16 AM     all      0.01      0.00      0.19      0.00      0.03     99.77
Average:        all      0.01      0.00      0.23      0.00      0.08     99.68

To display free and available memory, execute the following command:

sar -r

You should get output similar to this:

root@host:~# sar -r
Linux 5.15.0-83-generic (host.test.vps)      09/08/2023      _x86_64_        (3 CPU)

06:37:51 AM  LINUX RESTART      (3 CPU)

06:40:16 AM kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
06:50:16 AM   2933644   3495596    220264      5.50     46204    716808   1051440     20.80    494836    373528         0
Average:      2933644   3495596    220264      5.50     462

To check the used Swap space, execute the following command:

sar -S

You should get output similar to this:

root@host:~# sar -S
Linux 5.15.0-83-generic (host.test.vps)      09/08/2023      _x86_64_        (3 CPU)

06:37:51 AM  LINUX RESTART      (3 CPU)

06:40:16 AM kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
06:50:16 AM   1048568         0      0.00         0      0.00
Average:      1048568         0      0.00         0      0.00

06:50:40 AM  LINUX RESTART      (3 CPU)

07:00:11 AM kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
07:10:16 AM   1048568         0      0.00         0      0.00
07:20:16 AM   1048568         0      0.00         0      0.00
07:30:16 AM   1048568         0      0.00         0      0.00
07:40:16 AM   1048568         0      0.00         0      0.00
Average:      1048568         0      0.00         0      0.00

Step 5. SAR command in more detail

If you want to know more about the sar command, just execute the following line of code:

man sar

You will get an output with a full description of every option for the sar command.

Hopefully, our guide on how to install and use SAR on Linux was of help to you. We would love to hear from you now:

Did we skip something essential, or do you need a more detailed explanation about any of the steps? What are some other topics or tutorials you would want us to delve into?

Please feel free to share your thoughts in the comment section.

Leave a Comment