Install and configure sar and ksar for daily monitoring on Linux and generate PDF reports

This tutorial focuses on the installation and configuration of the powerful utilities sar and ksar on CentOS, Debian and Ubuntu and shows how to automatically create PDF reports by using a simple shell script for easy daily monitoring of server resource usage.

Part 1: Installing SAR Monitoring Software

SAR means system activity reports. SAR is the lightweight, real-time system monitoring tool. It can give the server's resource usage report as per our requirement. It gives output on a terminal by defaults and It also stores information day wise, So it will be a very handy and powerful utility for System Administrators.

SAR can be installed in any Linux flavors, It is a part of the sysstat packages.

Install and Configure SAR

For Debian/Ubuntu

# sudo apt-get install sysstat

For RedHat/CentOS

# sudo yum install sysstat

Install the latest sysstat from the source.

wget http://pagesperso-orange.fr/sebastien.godard/sysstat-12.1.4.tar.bz2
tar -xvf sysstat-12.1.4.tar.bz2
cd sysstat-12.1.4/
./configure --enable-install-cron
make
make install

If you install sar from source then it will install systat under /usr/local/bin/

After installation verify SAR version

#[email protected]:~$ sar -V
 sysstat version 11.2.0

After that make sure to verify SAR data collection is enabled.

In Ubuntu

#sudo vi /etc/default/sysstat 
ENABLED="true"

then restart sar service and enable it on system startup.

# systemctl start sysstat
# systemctl enable sysstat

Now check sar working properly in the system by running following command. Here 2 second time interval and 4 times output.

[[email protected] ~]$ sar 2 4 
Linux 5.0.16-200.fc29.x86_64 (redhat)     05/22/2019     _x86_64_    (4 CPU)

12:18:13 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:18:15 AM     all      1.25      0.00      0.50      0.00      0.00     98.24
12:18:17 AM     all      2.50      0.00      0.38      0.00      0.00     97.12
12:18:19 AM     all      2.12      0.00      0.62      0.12      0.00     97.12
12:18:21 AM     all      1.75      0.00      0.50      0.00      0.00     97.75
 Average:       all      1.91      0.00      0.50      0.03      0.00     97.56

We can simply run sar command to check resource usages.

[[email protected]at script]# sar
Linux 5.0.16-200.fc29.x86_64 (redhat)     05/24/2019     _x86_64_    (4 CPU)

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all      4.02      0.05      1.45      1.40      0.00     93.09
12:20:01 AM     all      6.27      0.02      1.82      0.56      0.00     91.33
12:30:01 AM     all     10.61      0.03      2.69      0.64      0.00     86.03
12:40:01 AM     all      9.26      0.05      2.45      0.59      0.00     87.65

By default sar interval set to every 10 minutes. We can modify this value by editing following files. 

vi /etc/cron.d/sysstat
# Run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib64/sa/sa1 1 1
# 0 * * * * root /usr/lib64/sa/sa1 600 6 &
# Generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib64/sa/sa2 -A

In above file, sa1 is the shell script for collecting and storing binary data  in the system activity daily data file and sa2 is the shell script which write daily report in /var/log/sa at end of the day (23:53 in above cronjob).

Part 2: Installing KSAR

KSAR generates a graph from SAR value which is very easy for analysis. KSAR is base on JAVA so we must have JDK 8 or above installed on our system. KSAR in not part of the system repository we have to download KSAR manually.

First, install java on redhat/centos

sudo yum install java-1.8.0-OpenJDK.x86_64

Now Download and install KSar on the system.

wget https://excellmedia.dl.sourceforge.net/project/ksar/ksar/5.0.6/kSar-5.0.6.zip
unzip kSar-5.0.6.zip
cd kSar-5.0.6/

Now collect sar command statistics using the following command.

LC_ALL=C sar -A -f /var/log/sa/sa21 > ~/Desktop/sar21
sudo chmod u+x run.sh
sh run.sh &

we should execute run.sh script as a normal user, do not run KSAR script as a root user.

now KSAR will display simple user-friendly GUI.

Ksar

Now Click on Data -> Load from text file -> select ~/Desktop/sar21 file .

Load file

Now our sar file loaded in KSAR.

Now we can export the graph in pdf, jpg, png, csv, txt format. Here we export the graph in pdf and select all value during export pdf.

Export PDF

Now open that pdf, it will show all useful server resources.

Here we can CPU load for a day on the server.

CPU Load Monitoring

This is memory usage over a day

Memory Usage graph

This way we can generate CPU, memory, processor, I/O, swap, sockets and many other useful system resources graph very easily for daily monitoring task.

Part 3: The Monitoring SCRIPT

KSAR GUI is very user-friendly. But the script will generate daily KSAR graph more effectively and save time.

In this part, we will create two scripts.

First script will generate a graph for a day daily.

vi sar_script.sh
#!/bin/bash
i=`date --date="1 days ago" +%d`
LC_ALL=C sar -A -f /var/log/sa/sa$i > /home/admin/sar_report/sardaily_report$i
cd /home/soham/Downloads/kSar-5.0.6/
java -jar kSar.jar -input /home/admin/sar_report/sardaily_report$i -outputPDF /home/admin/sar_report/sardaily$(date --date="1 days ago" +%b)$i.pdf
echo "Ksar graph successfully generated"
sudo chmod u+x sar_script.sh

Above script will generate single day system resources report. We can set this script in cronjob for automate generation of sar graph in pdf format.

crontab -e
* 10 * * * /bin/sh /home/admin/script/sar_script.sh

This cronjob generate everyday pdf graph from sar value at 10 am.

Next, we will generate sar statistics report for the whole month.

#!/bin/bash

function sar_value(){
#This is set for 31 possible days.
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31; do
LC_ALL=C sar -A -f /var/log/sa/sa$i >> /home/admin/sar_report/sarmonthly$(date --date="1 days ago" +%b).txt
done
}

Function for script
sar_value

Now we take that text file as an input for generating a pdf graph.

java -jar kSar.jar -input /home/admin/sar_report/sarmonthly$(date --date="1 days ago" +%b).txt -outputPDF /home/admin/sar_report/sar_monthly.pdf

This will generate the whole month report in one pdf file.

We can set up cron for automation generation of the report as shown above. If we want direct kSar graph report from server to local pc then we can set passwordless ssh between server and local pc and setup automation by adding rsync into the above script.

This way we can use very powerful sar utility very effective way for system daily monitoring task.

Share this page:

Suggested articles

0 Comment(s)

Add comment