In this guide, we will discuss how to generate CPU, memory and I/O report using sar command in linux.
SAR stands for System Activity Report, as its name suggest, sar command is used to collect, report & save CPU, Memory, I/O usage in Unix/Linux like operating systems. SAR command generate the reports on the fly and can also save the reports in log files as well.
Note: In case, sar is not installed on your linux system then use beneath steps,
Install SAR on RHEL / Rocky Linux / AlmaLinux
$ sudo dnf install sysstat -y
Install SAR on Ubuntu 22.04 / 24.04
$ sudo apt update $ sudo apt install -y sysstat
Start the sadc (system activity data collector) service(sysstat) so that it saves the reports in log file “/var/log/sa/saDD” where DD represents Current day and already existing files will be archived.
$ systemctl start sysstat $ systemctl enable sysstat
Once the sysstat service started then sar will start collecting the data every 10 minutes and generate its report daily. On RHEL like distributions, following systemd file is responsible for collecting and generating reports.
$ systemctl cat sysstat-collect.timer
On Ubuntu 22.04/24.04, following cron tab is responsible for generating the report every 10 minutes.
$ cat /etc/cron.d/sysstat
Below is the default config file of sysstat (sar command) on RHEL systems.
$ cat /etc/sysconfig/sysstat
Default Configuration of sysstat on Ubuntu 22.04/24.04
$ cat /etc/default/sysstat
Without any further delay, let’s deep dive into examples of sar command and see how the reports are generated.
1) Generate CPU Report on the Fly 5 times every 2 seconds
To generate the CPU report 5 times every 2 seconds, run
$ sar 2 5
If the %I/O wait is more than zero for a longer period of time then we can consider there is some bottleneck in I/O system (Hard disk or Network )
2) Save sar command output to a file
To save the sar command output to a file, use -o option, example is shown below
$ sar 2 5 -o /tmp/data > /dev/null 2>&1
Now use “sar -f <file-name>” to display the reports.
$ sar -f /tmp/data
3) Generating Memory Usage Report
-r option in the sar command is used to generate memory usage report.
[root@localhost ~]# sar -r 2 5
kbcommit & %commit is the overall memory used including RAM & Swap
4) Generating Paging Statistics Report
-B option in the sar command is used to display paging statistics. Example is shown below,
[root@localhost ~]# sar -B 2 5
In the report majflts/s shows the major faults per second means number of pages loaded into the memory from disk (swap), if its value is higher then we can say that system is running out of RAM.
%vmeff indicates the number of pages scanned per second, if it’s vaule is 100 % its is consider OK and when it is below 30 % then there is some issue with virtual memory. Zero value indicates that there is no page scanned during that time.
5) Generating Block Device Statistics Report
-d option in the sar command is used to display the block device statistics report. Using option -p (pretty-print) along with -d make the dev column more readable, example is shown below :
[root@localhost ~]# sar -d -p 2 4
6) Generating Network Statistic Report
-n option in the sar command is used to generate network statistic report. Below is the synatx :
$ sar -n {keyword} or {ALL}
Following keywords can be used : DEV, EDEV, NFS, NFSD, SOCK, IP, EIP, ICMP, EICMP, TCP, ETCP, UDP, SOCK6, IP6, EIP6, ICMP6, EICMP6 & UDP6.
To generate all network statistic report run following command :
$ sar -n ALL
7) Reading SAR Log File using -f
As we have stated above that sar logs files are kept under /var/log/sa/saDD, using -f option in sar command we can read these log files. One example is shown below,
$ sar -r -f /var/log/sa/sa16
Read Also: How to Sync Time in Linux Server using Chrony
Read Also: 20 ps Command Examples to Monitor Linux Processes
tramendous editorials about sar.
good work team
thanx
Thanks for the Information Sharing. Appreciate it Pradeep.
Great Info.
Thanks a lot.
thanks for your sharing information
Awesome!
How to get one month logs with a single command for Ram and CPU
Great tutorial. Next step could be to analyze sar report to get charts and aggregated data. For that we can follow ‘https://github.com/sumitbiswasgit/sarbox’
Nice info
Hi,
I got results if I use “sar 2 6” . This works perfectly fine.
But if I just say sar , it shows “LINUX RESTART” message.
The file in /etc/cron.d/sysstat looks fine to me. Not sure whats the issue? Anyone has the same issue?
Thanks
Anjum
command to generate the SAR report for the last one month