If you are a system administrator/Tester/Developer it's important for you to monitor your system health. If you are a Tester/Developer you need to understand how much memory or CPU is getting used by the application over a period of time. But it is difficult to monitor the system stats throughout the day or days. There is a simple tool/application named SAR (System Activity Report) available for collecting CPU, Memory, I/O stats and analyze, Of-course this can be used only for linux, unix or ubuntu machines.
Why SAR?
If you are working on linux machine you will know that you need to use different commands/tools to get different usage reports, such as: -top for CPU usage, -free for memory usage etc.
But SAR will provide you all the resource usage data which can be fetched from different tools and also this reports will generate in daily basis which can be used for historical data analysis at any time.
Advantages of using SAR
- Cumulative stats data will be available.
- Continuous monitoring of system stats.
- Able to fetch system stats at any point of time.
- Daily System performance will get captured. For 30 days
- No need of manual intervention for collecting stats.
- Quick stats overview at any given time.
- 24 hours system stats will get collected.
SAR Installation
We can install SAR stats pretty easily. Use the following commands to install.
Can be installed using yum install. [Note:- make sure yum repository configured]
1 |
# yum install sysstat |
Or it can be installed using apt get [Mostly this can be used for ubuntu]
1 |
# sudo apt-get install sysstat |
Or by downloading SAR rpm package. [It’s recommended that, install latest version or the SAR package version which supported to your RHEL or CentOS version]
1 |
# rpm -ivh sysstat-10.1.5-13.e17.x86_64.rpm |
You can check the version of SAR package installed or already available in you system by below command.
1 |
# sar -V |
Collecting SAR reports using cron job
Open the sysstat file available at /etc/cron.d/ :
1 |
# vi etc/cron.d/sysstat |
and configure the cron job as shown below:
1 2 3 4 5 |
# Run system activity accounting tool every 10 minutes */10 * * * * root /usr/lib64/sa/sa1 1 1 # Generate a daily summary of process accounting at 23:53 53 23 * * * root /usr/lib64/sa/sa2 -A |
For specific duration, first cron configuration will be used as shown in example. As shown in above example cron will run every 10 min and collect the stats and dump into a log file named sa followed by current day under /var/log/sa location. [ex:- today 20/06/2018, SAR output file will be sa20].
For daily summary of STATS report will get captured using second cron configuration, based on cron configuration every day at 11:23 PM [i.e at 23:53] summary of that day STATS report will get captured and dump into a log file named sar followed by current day under /var/log/sa location. [ex:- today 20/06/2018, SAR output file will be sar20].
If we want to collect the stats for every one min, cron job should be configured as shown below.
[Note:- min value to get the stats is 1 min, we cannot go min value then this as cron limits us to configure to min value as 1]
1 2 |
# Run system activity accounting tool every 10 minutes */1 * * * * root /usr/lib64/sa/sa1 1 1 |
In case of Ubuntu, For enabling SAR we need to enable the sadc (system activity data collector) under sysstat file, present under following location /etc/default/ change the property ENABLE to true.
1 |
# sudo vi /etc/default/sysstat |
1 2 |
# Should SADC collect system activity informations? TRUE or FALSE ENABLED="true" |
Configurations for SAR
We can configure our custom configurations in sysstat file under /etc/sysconfig/ like below.
[These configurations are optional].
- How long to keep log files (in days).
- Compress sa and sar files older than (in days).
- Parameters for the system activity data collector for the generation of log files.
- Compression program to use. [either gzip or bzip2]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# How long to keep log files (in days). # If the given value is greater than 28 then log files will be kept in multiple folders, # one for each month. HISTORY=28 # Compress (uzing zip or bzip2) sa and sar files older than (in days): COMPRESSAFTER=31 # Parameters for the system activity data collector (see sadc manual page) # which are used for the generation of log files. SADC_OPTIONS="-5 DISK" # Compression algorythm to use ZIP="bzip2" |
SAR commands to get the performance report
Before going to sar command usage, Lets see what are the SAR command options available. All option may not be used all the time, but we can see usage of those option with some description.
Retrieving the stats using SAR
If you want to see the current CPU statistics here is the below example.
In below example we are trying to retrieve the current CPU usage of ALL CPUs.
Explanation
- sar –P provides us the CPU usage, if we add ALL to this command we can get the ALL CPU usage. [Above example we have 4 CPUs]
- Option 2 means we need the CPU data every 2 seconds.
- Option 3 means instructing that we need all CPU data every 2 seconds print for 3 iterations/ 3 times.
SAR options with explanation:
So far we saw about the options used with SAR command, we can use the combination of all the above options please find the sample examples as below:
That's about it: hope you'll find this reference guide useful enough!