sar Commands 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.


1. Installing sar command

- To install sar command, you need to have "sysstat" package.
                # yum install sysstat
                # rpm -ivh sysstat-2.3.4

- To configure the sar to retain logs of more than default 7 days.
                # vi /etc/sysconfig/sysstat
                Change the "HISTORY" parameter


2. CPU usage

- To get current CPU usage
                # sar 2 10
                # sar -p 2 10
                # sar  -P ALL 2 10

- To get the CPU usage for previous date, consider 14th
                # sar -P ALL -f /var/log/sa/sa14

- 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  

                                         %user - CPU time used for user processes
                                         %nice - CPU time used for low priority processes
                                         %system - CPU time used for kernel space
                                         %iowait - CPU time spent in disk IO wait
                                         %steal - Percentage of time virtual CPU waits for real CPU
                                         %idle - CPU time in idle state

3. Memory usage

- To get current memory usage
                # sar -r 2 10

- To get memory usage for previous date, consider 14th
                # sar -r -f /var/log/sa/sa14

- 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 

                                         kbmemfree - Memory free in KB
                                         kbmemused - Memory used in KB
                                         %memused - Percentage of memory used
                                         kbbuffers - Size of buffer in KB
                                         kbcached - Size of Cache in KB
                                         kbcommit - Size of memory in commit state
                                         %commit - Percentage of memory in commite state

4. Swap usage

- To get current swap usage
                # sar -S 2 10

- To get swap usage for previous date, consider 14th
                # sar -S -f /var/log/sa/sa14

- 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 

                                         kbswpfree - Swap free in KB
                                         kbswpused - Swap used in KB
                                         %swpused - Percentage of swap used
                                         kbswpcad - Amount  of  cached  swap memory in kilobytes
                                         %swpcad - Percentage of cached swap memory

5. Load Average

- To get current load average stats
                # sar -q 2 10

- To get load average stats for previous date, consider 14th
                # sar -q -f /var/log/sa/sa14

- 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 

                                         runq-sz - number of tasks waiting for run time
                                         plist-sz - Number of tasks in the task list
                                         ldavg-1 - load  average  for  the last minute
                                         ldavg-5 - load  average  for  the last 5 minute
                                         ldavg-15 - load  average  for  the last 15 minute

6. Paging usage

- To get current paging usage
                # sar -B 2 10

- To get paging usage for previous date, consider 14th
                # sar -B -f /var/log/sa/sa14

- 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 

                                         pgpgin/s - Total number of kilobytes the system paged in from disk per second
                                         pgpgout/s - Total  number of kilobytes the system paged out to disk per second
                                         fault/s - Number of page faults
                                         majflt/s - Number  of major faults the system has made per second
                                         pgfree/s - Number of pages placed on the free list by the system per second
                                         pgscank/s - Number of pages scanned by the kswapd daemon per second
                                         pgscand/s - Number of pages scanned directly per second
                                         pgsteal/s - Number of pages the system has reclaimed from cache
                                         %vmeff - Efficiency of page reclaim

7. IO usage

- To get current IO usage
                # sar -b 2 10

- To get IO usage for previous date, consider 14th
                # sar -b -f /var/log/sa/sa14

- 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 

                                         tps - Total  number  of transfers per second
                                         rtps - Total number of read requests per second
                                         wtps - Total number of write requests per second
                                         bread/s - Total  amount of data read from the devices in blocks per second
                                         bwrtn/s - Total amount of data written to devices in blocks per second

8. Disk IO usage

- To get current disk IO usage
                # sar -d -p 2 10

- To get disk IO usage for previous date, consider 14th
                # sar -d -p -f /var/log/sa/sa14

- 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 

                                         tps - number of transfers per second
                                         rd_sec/s - Number of sectors read from the device
                                         wr_sec/s - Number of sectors written to the device
                                         avgrq-sz - The average size (in sectors) of the requests that were issued to the device
                                         avgqu-sz - The average queue length of the requests that were issued to the device
                                         await - The average time (in milliseconds) for I/O requests issued to the device to be served
                                         svctm - The average service time (in milliseconds) for I/O requests
                                         %util - Percentage  of CPU time during which I/O requests

9. Networking stats 

 - To get current network device stats
                # sar -n DEV 2 10

- To get network device stats for previous date, consider 14th
                # sar -n DEV -f /var/log/sa/sa14

- 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

                                         rxpck/s - Total number of packets received per second

                                         txpck/s - Total number of packets transmitted per second
                                         rxkB/s - Total number of kilobytes received per second
                                         txkB/s - Total number of kilobytes transmitted per second
                                         rxcmp/s - Number of compressed packets received per second
                                         txcmp/s - Number of compressed packets transmitted per second
                                         rxmcst/s - Number of multicast packets received per second

10. NFS stats 

 - To get current network device stats
                # sar -n NFS 2 10

- To get network device stats for previous date, consider 14th
                # sar -n NFS -f /var/log/sa/sa14

- To get network device stats for the 10th of month, from 7 AM to 3 PM
                # sar -n NFS -f /var/log/sa/sa10 -s 07:00:00 -e 15:00:00

                                         call/s - Number of RPC requests made per second
                                         retrans/s - Number of RPC requests per second, those which needed  to  be  retransmitted
                                         read/s - Number of âreadâ RPC calls made per second
                                         write/s - Number of âwriteâ RPC calls made per second
                                         access/s - Number of âaccessâ RPC calls made per second
                                         getatt/s - Number of âgetattrâ RPC calls made per second

11. Power Management Stats 

- To get current power management usage
                # sar -m 2 10

- To get power management usage for previous date, consider 14th
                # sar -m -f /var/log/sa/sa14

- 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 

12. Memory Stats (Page activities) 

- To get current memory stats
                # sar -R 2 10

- To get memory stats for previous date, consider 14th
                # sar -R -f /var/log/sa/sa14

- 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

                                         frmpg/s - Number  of memory pages freed by the system per second
                                         bufpg/s - Number  of  additional  memory  pages used as buffers by the system per second
                                         campg/s - Number of additional memory pages cached by the system per second

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)
- nfsiostat   : It displays NFS I/O statistics.
- cifsiostat  : It generates CIFS statistics. 

You May Also Like

Crontab Job Examples                                    elinks Command Examples
Netstat Command Examples                           e2fsck Command Examples
lvmdump Command Examples                        yum Command Examples
lvdisplay Command Examples                         dstat Command Examples
find Command Examples                                Linux Basic Commands

4 comments:

  1. Very nice and good article....

    ReplyDelete
  2. Super article. very very very great one

    ReplyDelete
  3. Thanks a lot. Very nice one.

    ReplyDelete
  4. Thankyou so much its very useful

    ReplyDelete