LLI Wiki — My Digital Knowledge Base

Monitoring Linux performance with sar (sysstat)

sar commands from sysstat to analyse CPU, memory, disk I/O and network on Linux, including reading historical data.

sar (System Activity Reporter) is part of the sysstat package. It reports CPU, memory, swap, paging, disk, file system, run queue and network statistics, either live or from the history files that sysstat collects in the background.

Installation

sudo dnf install sysstat        # RHEL, Fedora, AlmaLinux
sudo apt-get install sysstat    # Debian, Ubuntu
sudo systemctl enable --now sysstat.service

Live statistics

Syntax: sar [option] [interval [count]]

Command Statistics
sar 2 10 CPU utilisation: 10 samples, one every 2 seconds
sar -u ALL All CPU fields
sar -P ALL 1 1 Statistics per CPU core
sar -q Run queue and load averages
sar -r Memory utilisation
sar -S Swap space utilisation
sar -B Paging statistics
sar -W Swap in and swap out
sar -R Memory statistics (free, buffer and cached pages)
sar -b I/O and transfer rate statistics
sar -d Activity per block device
sar -F 1 1 File system utilisation
sar -w Context switches and task creation
sar -n DEV 5 10 Throughput per network interface

Network keywords (sar -n)

Keyword Description
DEV Network interface statistics
EDEV Network interface errors
NFS / NFSD NFS client / server activity
SOCK Sockets in use (IPv4)
IP / EIP IPv4 traffic / errors
ICMP / EICMP ICMPv4 traffic / errors
TCP / ETCP TCPv4 traffic / errors
UDP UDPv4 traffic
SOCK6, IP6, EIP6, ICMP6, UDP6 IPv6 equivalents
ALL All of the above (very long output)

Historical data

sysstat writes one daily file, saDD (DD = day of the month). sar -f reads it back:

sar -f /var/log/sa/sa11
sar -u ALL -s 10:00:00 -e 12:00:00
sar -r -s 00:00:00 -e 24:00:00

The path depends on the distribution: /var/log/sa on the RHEL family, /var/log/sysstat on Debian and Ubuntu.

Collection interval

On Debian and Ubuntu, collection is scheduled by cron in /etc/cron.d/sysstat. To collect every 2 minutes instead of the default every 10 minutes, replace the default 5-55/10 * * * * line with:

*/2 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1

On RHEL 8, collection is driven by the systemd timer sysstat-collect.timer. To collect every minute:

systemctl edit sysstat-collect.timer

Add to the override file:

[Unit]
Description=Run system activity accounting tool every 1 minute

[Timer]
OnCalendar=
OnCalendar=*:00/1

Then reload and restart the timer:

systemctl daemon-reload
systemctl restart sysstat-collect.timer
systemctl status sysstat-collect.timer

Notes

Further reading

Mastodon LLIMastodon SocialFosstodon