Question

How to install and configure SAR/sysstat on Ubuntu?

Monitoring your server resources is a crucial part of identifying any bottlenecks and possible issues on your server.

The sar command allows you to capture the utilization of your resources like RAM, CPU, Disk I/O and etc.

In this post, I will show you how to install and configure sar!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 3, 2020
Accepted Answer

Prerequisites

In order to complete this tutorial, you will need to have an Ubuntu 18.04 server with a non-root sudo-enabled user account and a basic firewall. This can be configured using our initial server setup guide for Ubuntu 18.04.

Installing sar/sysstat

First, let’s start by updating your local repositories:

  1. sudo apt update

After that as the sar command is part of the sysstat package in order to install it, you need to run the following command:

  1. sudo apt install sysstat

After that you can check the sar version by running the following:

  1. sar -V

Configuring sar/sysstat

After the installation, make sure to start and enable the sysstat service:

  1. sudo systemctl start sysstat
  2. sudo systemctl enable sysstat

This will add the required cron jobs so that the system data is collected accordingly.

The cron jobs will be added at:

  1. cat /etc/cron.d/sysstat

And the file will look like this:

Output
# 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

Using of sar

In order to

The sar command has a lot of arguments and options, but here is a list of some of the most popular ones which you might need:

Let’s start by checking the CPU usage on your server:

sar -u

This will show you the CPU usage for the current day.

If you wanted to check the current usage in real-time, you could specify 2 more arguments:

sar -u 2 30

The first argument which is 2 means that the sar command should run every 2 seconds and the second 30 means that the command should be executed 30 times.

That way you will see on your screen your CPU usage every 2 seconds for 30 times:

sar 1 30
Linux 4.15.0-101-generic (docker)       11/03/20        _x86_64_        (2 CPU)

14:21:16        CPU     %user     %nice   %system   %iowait    %steal     %idle
14:21:17        all      1.00      0.00      0.50      0.00      0.00     98.51
14:21:18        all      0.00      0.00      0.00      0.00      0.00    100.00
14:21:19        all      0.00      0.00      0.00      0.00      0.00    100.00
14:21:20        all      0.00      0.00      0.50      0.00      0.00     99.50
14:21:21        all      1.00      0.00      0.50      0.00      0.00     98.51
14:21:22        all      2.48      0.00      0.50      0.00      0.00     97.03
14:21:23        all      1.00      0.00      0.00      0.00      0.00     99.00
14:21:24        all      0.50      0.00      0.50      0.00      0.00     99.00

If you wanted to check your Memory usage instead, you could use the -r argument rather than -u.

sar -r 2 30

Some other useful arguments are -b which shows the Disk I/O usage and the -n which shows the network usage.

For more information make sure to check the man pages:

https://manpages.debian.org/testing/sysstat/sar.sysstat.1.en.html

Conclusion

The sysstat package also provides you with other useful tools like:

  • iostat - reports CPU statistics and input/output statistics for block devices and partitions.

  • mpstat - reports individual or combined processor related statistics.

  • pidstat - reports statistics for Linux tasks (processes) : I/O, CPU, memory, etc.

  • tapestat - reports statistics for tape drives connected to the system.

  • cifsiostat - reports CIFS statistics.

  • Sysstat - also contains tools you can schedule via cron or systemd to collect and historize performance and activity data:

  • sar - collects, reports and saves system activity information (see below a list of metrics collected by sar).

  • sadc - is the system activity data collector, used as a backend for sar.

  • sa1 - collects and stores binary data in the system activity daily data file. It is a front end to sadc designed to be run from cron or systemd.

  • sa2 - writes a summarized daily activity report. It is a front end to sar designed to be run from cron or systemd.

  • sadf - displays data collected by sar in multiple formats (CSV, XML, JSON, etc.) and can be used for data exchange with other programs. This command can also be used to * draw - graphs for the various activities collected by sar using SVG (Scalable Vector Graphics) format.

If you like the tool make sure to star it on GitHub and contribute:

https://github.com/sysstat/sysstat

Hope that this helps!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel