By Bobby Iliev
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
!
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!
Accepted Answer
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.
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
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:
# 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
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
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!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.