how to use sar command with its examples

This tutorial will give you the glimpse of “sar” utility and its examples!

– sar is a very useful command provided in the Linux/UNIX OS. It comes from sysstat package.

– Using sar command we can get many important stats from the server like CPU usage, memory usage, swap, IO, disk IO, network, paging etc.

– Not only we can get the current statistics but also stats for previous date from the sar log files (/var/log/sa/), remember by default sar stores only latest 7 days data into “/var/log/sa/sa1-31” files. (1-31 for months dates)

– We can make sar to keep logs for more days by editing “/etc/sysconfig/sysstat” file and specifying “HISTORY” attribute.

– I have covered almost all the sar command options here.

sar command examples

1. Installing the sysstat utility

1.1 To install sar command, you need to have “sysstat” package.
# yum install sysstat
# rpm -ivh sysstat-2.3.4
1.2 To configure the sar to retain logs of more than default 7 days.
# vi /etc/sysconfig/sysstat
Change the “HISTORY” parameter

2. CPU usage

2.1 To get current CPU usage
# sar 2 10 # sar -p 2 10 # sar -P ALL 2 10
2.2 To get the CPU usage for previous date (historical data) , consider 14th
# sar -P ALL -f /var/log/sa/sa14
2.3 To get the CPU usage for 10th of month, from 7 AM to 3 PM (i.e. with specifying the time)
# sar -P ALL -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

3. Memory Usage

3.1 To get current memory usage #
# sar -r 2 10
3.2 To get memory usage for previous date (historical data), consider 14th
# sar -r -f /var/log/sa/sa14
3.3 To get memory usage for the 10th of month, from 7 AM to 3 PM (i.e. with specifying the time)
# sar -r -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

4. Swap usage

4.1 To get current swap usage
# sar -S 2 10
4.2 To get swap usage for previous date (historical data), consider 14th
# sar -S -f /var/log/sa/sa14
4.3 To get swap usage for the 10th of month, from 7 AM to 3 PM
# sar -S -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

5. Load Average

5.1 To get current load average stats
# sar -q 2 10
5.2 To get load average stats for previous date (historical data), consider 14th
# sar -q -f /var/log/sa/sa14
5.3 To get load average stats for the 10th of month, from 7 AM to 3 PM
# sar -q -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

6. Paging Usage

6.1 To get current paging usage
# sar -B 2 10
6.2 To get paging usage for previous date (historical data), consider 14th.
# sar -B -f /var/log/sa/sa14
6.3 To get paging usage for the 10th of month, from 7 AM to 3 PM
# sar -B -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

7. IO Usage

7.1 To get current IO usage
# sar -b 2 10
7.2 To get IO usage for previous date (historical data), consider 14th.
# sar -b -f /var/log/sa/sa14
7.3 To get IO usage for the 10th of month, from 7 AM to 3 PM
# sar -b -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

8. Disk IO Usage

8.1 To get current disk IO usage
# sar -d -p 2 10
8.2 To get disk IO usage for previous date (historical data), consider 14th
# sar -d -p -f /var/log/sa/sa14
8.3 To get disk IO usage for the 10th of month, from 7 AM to 3 PM
# sar -d -p -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

9. Networking stats

9.1 To get current network device stats
# sar -n DEV 2 10
9.2 To get network device stats for previous date (historical data), consider 14th
# sar -n DEV -f /var/log/sa/sa14
9.3 To get network device stats for the 10th of month, from 7 AM to 3 PM
# sar -n DEV -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

10. Power Management Statistics

10.1 To get current power management usage
# sar -m 2 10
10.2 To get power management usage for previous date (historical data), consider 14th
# sar -m -f /var/log/sa/sa14
10.3 To get power management usage for the 10th of month, from 7 AM to 3 PM
# sar -m ALL -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

11. Memory Stats (Page activities)

11.1 To get current memory stats
# sar -R 2 10
11.2 To get memory stats for previous date (historical data), consider 14th
# sar -R -f /var/log/sa/sa14
11.3 To get memory stats for the 10th of month, from 7 AM to 3 PM
# sar -R ALL -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

Other Sysstat Utilities

  • sar : It collects and displays ALL system activities statistics.
  • sadc : It stands for “system activity data collector”. This is the sar backend tool that does the data collection.
  • sa1 : It stores system activities in binaryhttps://images-blogger-this purpose. sa1 runs from cron.
  • sa2 : It creates daily summary of the collected statistics. sa2 runs from cron.
  • sadf : It can generate sar report in CSV, XML, and various other formats. Use this to integrate sar data with other tools.
  • iostat : It generates CPU, I/O statistics
  • mpstat : It displays CPU statistics.
  • pidstat : It reports statistics based on the process id (PID)
  • Memory Stats (Page activities) It displays NFS I/O statistics.
  • cifsiostat : It generates CIFS statistics.

sar command not found

If you get the error message as “sar command not found”, that means your system does not have the “sysstat” utility installed on it. sar command is one of the command provided by the sysstat utility. Hence please install the “sysstat” utility and then check with sar command. For installing the sysstat utility please follow below syntaxt as per your OS version.

OS VersionCommand
RedHat / CentOS / Fedorayum install sysstat
Debian / Ubuntu / Kubuntuapt install sysstat

Leave a Reply

Your email address will not be published. Required fields are marked *