SAR — Tool To Monitor Historic CPU Usage
In day to day Linux server administration, we often come across situations where many times we have to compare or check OS related parameters to find out root cause of an incident or to check behaviour or capacity of our server against certain amount of load generated by an application for capacity planning or for reporting purpose. One such parameter is CPU usage.
One such tool is available which can provide us with vital statistics for CPU usage known as SAR. It is a system activity generator tool. SAR may not be by default installed in your system.
To install it on your system(Redhat/CentOS) use below command.

It generates all the activity logs in folder known as /var/log under sa folder.

To view sa files we cannot see it using less or cat as they are in binary format. You can see below message when we try to viewit using less command.

To view it we can use sar command by providing the filename to open using -f flag
sar -f /var/log/sa/sa31

Output will show CPU usage by various participants(user, system is of our interest generally in % format followed by % idle at the end). It will also list the average at the bottom for a particular time interval(say from the start of the hour to the point in time when you view the file as per last record. Here we are viewing average for 50 minutes of data).
Is it the only thing is offers ? No, It also offers different activity logs such as network packets in/out, swap memory usage and other disk related stats.
For instance to view swap related stats you can use below flag with sar command.

the value 0 indicates that our system is healthy and there is no swapping taking place.
How To Check Historic Memory Usage ?
Well you can make a small script and run it in a crontab on intervals you like. Currently there is no tool offering reports for historic memory usage. Below is the simple script. You can also use logrotate if required for rotating the log files.
log=/tmp/memory_stats
echo “##########” >> $log
echo “`date`” >> $log
echo “`free -m`” >> $log
For more options in sar you can always execute man sar.
I hope it would be helpful for beginners who are new to SAR.