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
4.2 To get swap usage for previous date (historical data), consider 14th
4.3 To get swap usage for the 10th of month, from 7 AM to 3 PM
|
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
5.3 To get load average stats for the 10th of month, from 7 AM to 3 PM
|
6. Paging Usage
6.1 To get current paging usage
6.2 To get paging usage for previous date (historical data), consider 14th.
6.3 To get paging usage for the 10th of month, from 7 AM to 3 PM
|
7. IO Usage
7.1 To get current IO usage
7.2 To get IO usage for previous date (historical data), consider 14th.
7.3 To get IO usage for the 10th of month, from 7 AM to 3 PM
|
8. Disk IO Usage
8.1 To get current disk IO usage
8.2 To get disk IO usage for previous date (historical data), consider 14th
8.3 To get disk IO usage for the 10th of month, from 7 AM to 3 PM
|
9. Networking stats
9.1 To get current network device stats
9.2 To get network device stats for previous date (historical data), consider 14th
9.3 To get network device stats for the 10th of month, from 7 AM to 3 PM
|
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
10.3 To get power management usage for the 10th of month, from 7 AM to 3 PM
|
11. Memory Stats (Page activities)
11.1 To get current memory stats
11.2 To get memory stats for previous date (historical data), consider 14th
11.3 To get memory stats for the 10th of month, from 7 AM to 3 PM
|
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 Version | Command |
RedHat / CentOS / Fedora | yum install sysstat |
Debian / Ubuntu / Kubuntu | apt install sysstat |