Skip to content

Linux : Get average CPU and Memory utilization from SAR data

Working on some performance related issue today and user requested average CPU/Memory utilization history on previous days, so I came out with quick script to pull the data.

Tested on Oracle Enterprise Linux. Should be working fine on any RHEL based distribution. Sysstat package is required to enable sar report on your server.


# Get Average CPU/Memory Utilization History from sysstat file in /var/log/sa/*
# Author: azwan.ngali[AT]

for file in $(ls -la /var/log/sa/* | grep sa[0-9] | awk '{print $9}')
        sar -f $file | head -n 1
        printf "\n"

        # Get CPU idle average, it's pretty straight forward.

        printf "CPU average: "
        sar -u -f $file | grep Average: | awk -F " " '{sum = (100 - $8) } END { print sum "%" }'

        # Get Average Memory utilization

        # Information being displayed in sar -r command is somewhat misleading.
        # As it is merely calculated by the formula kbmemused/(kbmemused+kbmemfree) * 100
        # But actually that was not the case, in order to get memory calculation, 
        # here's the revised formula to include memory cache/buffer information into account.
        # Formula:
        # (kbmemused-kbbuffers-kbcached) / (kbmemfree + kbmemused) * 100
        # The reason behind this is Linux treats unused memory as a wasted resource and so uses as 
        # much RAM as it can to cache process/kernel information
        printf "Memory Average: "
        sar -r -f $file | grep Average | awk -F " " '{ sum = ($3-$5-$6)/($2+$3) * 100   } END { print sum "%" }'

        printf "\n"

Upon execution, it will search all sa* file in /var/log/sa and perform basic calculation to display CPU/memory average. It may be handy if you’re lazy like me.

[root@ausuovmfmtap3 sa]# ./
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/01/2014
CPU average: 4.76%
Memory Average: 15.6925%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/02/2014
CPU average: 3.4%
Memory Average: 14.3805%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/03/2014
CPU average: 3.35%
Memory Average: 14.576%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/04/2014
CPU average: 3.97%
Memory Average: 17.8241%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/05/2014
CPU average: 4.44%
Memory Average: 20.4096%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/06/2014
CPU average: 4.58%
Memory Average: 20.6211%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/07/2014
CPU average: 4.77%
Memory Average: 18.3188%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/08/2014
CPU average: 3.34%
Memory Average: 14.8783%
Linux 2.6.18-274.el5 (ausuovmfmtap3.xx)        07/09/2014
CPU average: 3.44%
Memory Average: 15.1599%
[root@ausuovmfmtap3 sa]#


Published inLinuxTutorials


  1. Mirza Faisal Baig Mirza Faisal Baig

    what modification will be required for getting max usage of ram and cpu along with average?

  2. Adrian Adrian


    I would like to know if the results can have a total, currently looking for something like this but for a monthly report, its just hard adding / averaging per day results. thanks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.