The primary function of the SAR command is to collect system performance data every day and store it in log files for later display, it collects sar statistics every 10 minutes. It is a binary file that can’t be opened by a text editor.
Most Linux administrators check the system’s historical performance data using the SAR report, because it collects performance data for a week or a month or more than a month depending on the configuration of the sar report.
Each file contain a daily average but there is no feasibility to check the average data of a week, that’s why we wrote this article to get the average of all SAR files at once.
We’ve included three bash scripts in this article that will help you to easily view each data file average in one place.
These scripts are very simple and straightforward. For testing purpose, we have included only two performance metrics like CPU and Memory, but you can add other performance metrics in the script as per your needs.
Make a Note:
Debian-based users should change the path to ‘/var/log/sysstat/sa’ and sar configuration file can be found in ‘/etc/sysstat/config’.
Script-1: Checking Average CPU Utilization from SAR Reports
This bash script collects the CPU average from each sar data file and display it one page.
# vi /opt/scripts/sar-cpu-avg.sh #!/bin/sh echo "+----------------------------------------------------------------------------------+" echo "|Average: CPU %user %nice %system %iowait %steal %idle |" echo "+----------------------------------------------------------------------------------+" for file in `ls -tr /var/log/sa/sa* | grep -v sar` do dat=`sar -f $file | head -n 1 | awk '{print $4}'` echo -n $dat sar -f $file | grep -i Average | sed "s/Average://" done echo "+----------------------------------------------------------------------------------+"
Once you run the script, you will get an output like the one below, but this value might differ system to system.
# sh /opt/scripts/sar-cpu-avg.sh +----------------------------------------------------------------------------------+ |Average: CPU %user %nice %system %iowait %steal %idle | +----------------------------------------------------------------------------------+ 08/01/2023 all 0.70 0.00 1.19 0.00 0.00 98.10 08/02/2023 all 1.73 0.00 3.16 0.01 0.00 95.10 08/03/2023 all 1.73 0.00 3.16 0.01 0.00 95.11 08/04/2023 all 1.02 0.00 1.80 0.00 0.00 97.18 08/05/2023 all 0.68 0.00 1.08 0.01 0.00 98.24 08/06/2023 all 0.71 0.00 1.17 0.00 0.00 98.12 08/07/2023 all 1.79 0.00 3.17 0.01 0.00 95.03 08/08/2023 all 1.78 0.00 3.14 0.01 0.00 95.08 08/09/2023 all 1.07 0.00 1.82 0.00 0.00 97.10 08/10/2023 all 0.38 0.00 0.50 0.00 0.00 99.12 . . . 08/29/2023 all 1.50 0.00 2.33 0.00 0.00 96.17 08/30/2023 all 2.32 0.00 3.47 0.01 0.00 94.20 +----------------------------------------------------------------------------------+
Script-2: Finding Average Memory Utilization from SAR Reports
This bash script will collect memory averages from each data file and display it an one page.
# vi /opt/scripts/sar-memory-avg.sh #!/bin/sh echo "+-------------------------------------------------------------------------------------------------------------------+" echo "|Average: kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty |" echo "+-------------------------------------------------------------------------------------------------------------------+" for file in `ls -tr /var/log/sa/sa* | grep -v sar` do dat=`sar -f $file | head -n 1 | awk '{print $4}'` echo -n $dat sar -r -f $file | grep -i Average | sed "s/Average://" done echo "+-------------------------------------------------------------------------------------------------------------------+"
Once you run the script, you will get an output similar to the below.
# sh /opt/scripts/sar-memory-avg.sh +--------------------------------------------------------------------------------------------------------------------+ |Average: kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty | +--------------------------------------------------------------------------------------------------------------------+ 08/01/2023 1492331 2388461 61.55 29888 1152142 1560615 12.72 1693031 380472 6 08/02/2023 1493126 2387666 61.53 29888 1147811 1569624 12.79 1696387 373346 3 08/03/2023 1489582 2391210 61.62 29888 1147076 1581711 12.89 1701480 370325 3 08/04/2023 1490403 2390389 61.60 29888 1148206 1569671 12.79 1697654 373484 4 08/05/2023 1484506 2396286 61.75 29888 1152409 1563804 12.75 1702424 374628 4 08/06/2023 1473593 2407199 62.03 29888 1151137 1577491 12.86 1715426 371000 8 08/07/2023 1467150 2413642 62.19 29888 1155639 1596653 13.01 1716900 372574 13 08/08/2023 1451366 2429426 62.60 29888 1162253 1604672 13.08 1725931 376998 5 08/09/2023 1451191 2429601 62.61 29888 1158696 1582192 12.90 1728819 371025 4 08/10/2023 1450050 2430742 62.64 29888 1160916 1579888 12.88 1729975 370844 5 . . . 08/29/2023 1365699 2515093 64.81 29888 1198832 1593567 12.99 1781733 376157 15 08/30/2023 1361920 2518872 64.91 29888 1200785 1595105 13.00 1784556 375641 8 +-------------------------------------------------------------------------------------------------------------------+
Script-3: Displaying Average CPU & Memory Utilization from SAR Reports
This bash script collects the CPU & memory averages from each data file and display both of them on a page.
This bash script is slightly different compared to the above scripts. It shows the average of both (CPU & Memory) in one page, not the other data.
# vi /opt/scripts/sar-cpu-mem-avg.sh #!/bin/bash for file in `ls -tr /var/log/sa/sa* | grep -v sar` do sar -f $file | head -n 1 | awk '{print $4}' echo "-----------" sar -u -f $file | awk '/Average:/{printf("CPU Average: %.2f%\n"), 100 - $8}' sar -r -f $file | awk '/Average:/{printf("Memory Average: %.2f%\n"),(($3-$5-$6)/($2+$3)) * 100 }' printf "\n" done
Once you run the script, you will get an output like the one below.
# sh /opt/scripts/sar-cpu-mem-avg.sh 08/01/2023 ----------- CPU Average: 1.90% Memory Average: 31.09% 08/02/2023 ----------- CPU Average: 4.90% Memory Average: 31.18% 08/03/2023 ----------- CPU Average: 4.89% Memory Average: 31.29% 08/04/2023 ----------- CPU Average: 2.82% Memory Average: 31.24% 08/05/2023 ----------- CPU Average: 1.76% Memory Average: 31.28% . . . 08/29/2023 ----------- CPU Average: 3.83% Memory Average: 33.15% 08/30/2023 ----------- CPU Average: 5.80% Memory Average: 33.19%
How to get the average of the 15 days sar report for avg cpu of 15 days or avg mem of 15 days?
To achieve this, the script needs to be modified to take only 15 days of average data from the SAR report. We will check this and let you know asap.
hello team ,
Needed script for cpu,memory,io report in linux it should run once in daily basis and save in specific file and remove the files older that month. Please send such script if anyone have on mail [email protected].
Will really appreciate the help.
Hi Yogesh,
We will try to publish the script ASAP.
sh /opt/scripts/sar-cpu-mem-avg.sh how can we get last 2 hours average, and how can we check min .max avg of cpu and memory of last 4 hours
This can be done by getting start and end time input in the script.