Question

How to install and configure SAR/sysstat on Ubuntu?

Posted November 3, 2020 358 views
Linux BasicsUbuntuMonitoringSystem ToolsLinux Commands

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!

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.

×
1 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:

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

  • sudo apt install sysstat

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

  • sar -V

Configuring sar/sysstat

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

  • sudo systemctl start sysstat
  • 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:

  • 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!

by Justin Ellingwood
When you first create a new Ubuntu 18.04 server, there are a few configuration steps that you should take early on as part of the basic setup. This will increase the security and usability of your server and will give you a solid foundation for subsequent...
Submit an Answer