Monitoring system resources using SAR on Ubuntu 20.04
SAR also know as a "System Activity Report" is a system resource monitoring tool for Linux operating systems. It is very useful for system administrators to collect the system performance report and improve system performance to handle more requests. With SAR, you can monitor CPU usage, Memory utilization, I/O devices consumption, battery performance, Plug and play devices, Disk usage, Processor performance, file system, and more.
In this tutorial, we will show you how to install and use SAR to monitor system performance.
Prerequisites
- A server running Ubuntu 20.04.
- A valid domain name pointed with your server IP.
- A root password is configured on the server.
Install SAR
By default, SAR package is available in the Ubuntu default repository. You can install it by just running the following command:
apt-get install sysstat -y
Once the installation is finished, start the SAR service and enable it to start at system reboot with the following command:
systemctl start sysstat
systemctl enable sysstat
Next, you can verify the installed version of SAR with the following command:
sar -V
You should get the SAR version in the following output:
sysstat version 12.2.0 (C) Sebastien Godard (sysstatorange.fr)
During the installation, SAR generates a Cron file for collecting and generating reports every 10 minutes. You can check it with the following command:
cat /etc/cron.d/sysstat
You should get the following 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
Monitor CPU Usage
You can run the following command to monitor CPU usage 4 times every 1 seconds.
sar 1 4
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:32:39 AM CPU %user %nice %system %iowait %steal %idle 09:32:40 AM all 0.00 0.00 0.00 0.00 0.00 100.00 09:32:41 AM all 0.00 0.00 0.00 0.00 0.00 100.00 09:32:42 AM all 0.00 0.00 0.00 0.00 0.00 100.00 09:32:43 AM all 0.00 0.00 0.00 0.00 0.00 100.00 Average: all 0.00 0.00 0.00 0.00 0.00 100.00
You can also save the generated output to a file using the -o option.
sar 1 4 -o cpu-report
You can see this report later using the -f option as shown below:
sar -f cpu-report
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:35:01 AM CPU %user %nice %system %iowait %steal %idle 09:35:02 AM all 0.50 0.00 1.00 0.00 0.00 98.50 09:35:03 AM all 0.00 0.00 0.50 0.00 0.00 99.50 09:35:04 AM all 0.00 0.00 0.00 0.00 0.00 100.00 09:35:05 AM all 0.50 0.00 0.50 0.00 0.00 99.00 Average: all 0.25 0.00 0.50 0.00 0.00 99.25
Monitor Memory Usage
You can use -r option to monitor the memory usage of your system.
To monitor Memory usage 4 times every 1 seconds, run the following command:
sar -r 1 4
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:37:29 AM kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty 09:37:30 AM 3609412 3733536 47556 1.18 24180 290460 102048 2.26 138240 206508 0 09:37:31 AM 3609412 3733536 47556 1.18 24180 290460 102048 2.26 138240 206508 0 09:37:32 AM 3609444 3733568 47524 1.18 24180 290460 102048 2.26 138240 206508 0 09:37:33 AM 3609444 3733568 47524 1.18 24180 290460 102048 2.26 138240 206508 0 Average: 3609428 3733552 47540 1.18 24180 290460 102048 2.26 138240 206508 0
Monitor Paging Statistics
You can use -B option to monitor the paging statistics of your system.
Run the following command to display paging statistics.
sar -B 1 4
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:39:03 AM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff 09:39:04 AM 0.00 0.00 4.00 0.00 1.00 0.00 0.00 0.00 0.00 09:39:05 AM 0.00 0.00 1.00 0.00 8.00 0.00 0.00 0.00 0.00 09:39:06 AM 0.00 0.00 0.00 0.00 8.00 0.00 0.00 0.00 0.00 09:39:07 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: 0.00 0.00 1.25 0.00 4.25 0.00 0.00 0.00 0.00
Monitor Block Device Statistics
You can use -d option with SAR command to display the block device statistics report. You can also use -p option to display output in more readable format:
sar -d -p 1 4
You should see the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:41:15 AM DEV tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util 09:41:16 AM sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:41:17 AM sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:41:18 AM sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:41:19 AM sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Monitor Network Statistic
You can use -n option with SAR command to monitor network statistics in your system. You can use DEV, EDEV, NFS, NFSD, SOCK, IP, EIP, ICMP, EICMP, TCP, ETCP, UDP, SOCK6, IP6, EIP6, ICMP6, EICMP6 & UDP6 with -n option to monitor specific device. You can also use the option ALL to monitor all network devices.
To monitor all network devices, run the following command:
sar -n ALL 1 4
To monitor statistic of all network interface, run the following command:
sar -n DEV 1 4
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:51:18 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil 09:51:19 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:19 AM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:19 AM eth0 12.00 2.00 0.91 0.23 0.00 0.00 0.00 0.00 09:51:19 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil 09:51:20 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:20 AM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:20 AM eth0 16.00 12.00 1.13 2.22 0.00 0.00 0.00 0.00 09:51:20 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil 09:51:21 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:21 AM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:21 AM eth0 6.00 12.00 0.46 3.02 0.00 0.00 0.00 0.00 09:51:21 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil 09:51:22 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:22 AM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:51:22 AM eth0 9.00 22.00 0.66 3.49 0.00 0.00 0.00 0.00 Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: eth0 10.75 12.00 0.79 2.24 0.00 0.00 0.00 0.00
Monitor Swap Usage
You can use -S optio to monitor swap usage statistics.
sar -S 1 4
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:54:18 AM kbswpfree kbswpused %swpused kbswpcad %swpcad 09:54:19 AM 483800 0 0.00 0 0.00 09:54:20 AM 483800 0 0.00 0 0.00 09:54:21 AM 483800 0 0.00 0 0.00 09:54:22 AM 483800 0 0.00 0 0.00 Average: 483800 0 0.00 0 0.00
Monitor I/O Operations
To monitor details about I/O operations like transaction per second, read per second, write per second, run the following command:
sar -b 1 4
You should get the following output:
Linux 5.4.0-29-generic (ubuntu2004) 01/04/2021 _x86_64_ (2 CPU) 09:56:58 AM tps rtps wtps dtps bread/s bwrtn/s bdscd/s 09:56:59 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:57:00 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:57:01 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:57:02 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Conclusion
In the above post, you learned how to install and use SAR command line tool to monitor your system performance. I hope this will helps you to improve your system performance.