ksar command examples

In the final article in this series, we’ll focus on generating a graphical view of sar data using ksar command because we’ve all heard the phrase a picture speaks a thousand words.

Steps to install ksar in Linux

  • Check and fix Java application
  • Download ksar from SourceForge site
  • Extract and install ksar application

Working on prerequisites of KSAR application

Ksar is a java based application which can be used to create a graphical representation of data collected by the sar command. ksar allows graphs to be collected in a variety of formats.

It is easy to install and easy to use. The only major dependency is the presence of java on the system. Although JDK is usually installed on Linux systems by default but let’s verify just to be sure.

Doing a quick search for jdk (java development kit) in the rpm -qa output gives a couple of results.

[root@linuxnix ~]# rpm -qa | grep jdk

We can verify that the java binary is available by using the which command to look for it.

[root@linuxnix ~]# which java

Now, let’s verify the rpm which actually provides this binary.

[root@linuxnix ~]# ls -l /usr/bin/java
lrwxrwxrwx. 1 root root 22 Sep 26 01:05 /usr/bin/java -> /etc/alternatives/java
[root@linuxnix ~]# ls -l /etc/alternatives/java
lrwxrwxrwx. 1 root root 46 Sep 26 01:05 /etc/alternatives/java -> /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
[root@linuxnix ~]# ls -l /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
-rwxr-xr-x. 1 root root 9904 May 11 2016 /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
[root@linuxnix ~]# rpm -qf /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

What transpired above was that the java binary was found to be a soft link. So we traced the original file and then used the rpm command with the -qf option (query file) to obtain the source rpm which provided the binary.

With this critical check complete, let’s download ksar from source forge and get started.

Download and extract ksar Linux package

Use wget command to download ksar package.

[root@linuxnix ~]# wget https://downloads.sourceforge.net/project/ksar/ksar/5.0.6/kSar-5.0.6.zip
--2017-10-21 11:08:41--  https://downloads.sourceforge.net/project/ksar/ksar/5.0.6/kSar-5.0.6.zip
Resolving downloads.sourceforge.net...
Connecting to downloads.sourceforge.net||:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://nchc.dl.sourceforge.net/project/ksar/ksar/5.0.6/kSar-5.0.6.zip [following]
--2017-10-21 11:08:58--  https://nchc.dl.sourceforge.net/project/ksar/ksar/5.0.6/kSar-5.0.6.zip
Resolving nchc.dl.sourceforge.net..., 2001:e10:ffff:1f02::17
Connecting to nchc.dl.sourceforge.net||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6039985 (5.8M) [application/octet-stream]
Saving to: ‘kSar-5.0.6.zip’
kSar-5.0.6.zip                                      100%[================================================================================================================>]   5.76M   201KB/s    in 58s     
2017-10-21 11:10:00 (101 KB/s) - ‘kSar-5.0.6.zip’ saved [6039985/6039985]

Ksar will be downloaded as a zip file. Copy this file to your system and extract it.

[root@linuxnix ~]# unzip kSar-5.0.6.zip
Archive:  kSar-5.0.6.zip
   creating: kSar-5.0.6/
   creating: kSar-5.0.6/contrib/
   creating: kSar-5.0.6/docs/
   creating: kSar-5.0.6/lib/
  inflating: kSar-5.0.6/LICENCE
  inflating: kSar-5.0.6/contrib/hpux-sar-A.sh
  inflating: kSar-5.0.6/contrib/macsar.sh
  inflating: kSar-5.0.6/contrib/sarl
  inflating: kSar-5.0.6/docs/CHANGELOG
  inflating: kSar-5.0.6/docs/README
  inflating: kSar-5.0.6/docs/Shortcut.xml
  inflating: kSar-5.0.6/kSar.jar
  inflating: kSar-5.0.6/kSar.jnlp
  inflating: kSar-5.0.6/lib/iText-2.1.3.jar
  inflating: kSar-5.0.6/lib/jcommon-1.0.13.jar
  inflating: kSar-5.0.6/lib/jfreechart-1.0.11.jar
  inflating: kSar-5.0.6/lib/jsch-0.1.40.jar
  inflating: kSar-5.0.6/lib/swing-layout-1.0.3.jar
  inflating: kSar-5.0.6/run.sh
replace kSar-5.0.6/LICENCE? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: kSar-5.0.6/LICENCE

Install ksar package in Linux

With the files extracted, go to the kSar-5.0.6/ directory and execute the run.sh script.

The ksar command

The ksar command

This brings up the ksar startup up implying that everything is working correctly. A few seconds later the menu based window will launch as shown below.

The ksar command

The ksar command


To be able to use the ksar graphical user interface we must be using the system in GUI mode.

Ksar will require an input file to generate the graphs. So let’s create one with the below command.

LC_ALL=C sar -d -p -f /var/log/sa/sa14 > /tmp/sar-$(hostname).txt

This creates an ASCII text file containing the reports for disk activity collected on the 14th day of the current month (/var/log/sa14) and stores it in /tmp.

Feed the file to Ksar by clicking on Data and then selecting load from text file.

The ksar command

The ksar command


Once ksar loads the file, it generates the graphs almost instantaneously and populates the left pane of the window with the available graph options. We can view a graph for an entity by just clicking one of the selections in the left pane.

The ksar command

The ksar command

The first link shows some summary information about the input file being used to generate the graphs. As I mentioned viewing the graphs is just a matter of a mouse click. for example, to view block transfer statistics for disk /dev/sda just click on the entry that says Block Transfer under Disk>sda.

The ksar command

The ksar command


Saving the graphs is fairly easy. Click on Data pane and from there you can select the option to export the graphs in the desired format.

The ksar command

The ksar command


We can view help and available options provided by ksar by running the following command:

java -jar kSar.jar -help
kSar version: 5.0.6
-version: show kSar version number
-help: show this help
-input <arg>: argument must be either ssh://user@host/command or cmd://command or file://path/to/file or just /path/to/file
-graph <graph list>: space separated list of graph you want to be outputed
-showCPUstacked: will make the CPU used graph as stacked
-showMEMstacked: will make the Memory graph as stacked (linux only)
-cpuFixedAxis:  will graph CPU used with fixed axis from 0% to 100%
-showIntrListstacked : will make the Interrupt List graph as stacked
-showTrigger:  will show trigger on graph (disabled by default)
-noEmptyDisk: will not export disk with no data
-tile: will tile window
-userPrefs: will use the userPrefs for outputing graphs (last export of this host)
-showOnlygraphName: will only print graph name available for that data (to be use for -graph)
-addHTML: will create an html page with PNG/JPG image
-outputPDF <pdf file> : output the pdf report to the pdf file
-outputPNG <base filename> : output the graphs to PNG file using argument as base filename
-outputJPG <base filename> : output the graphs to JPG file using argument as base filename
-outputCSV <CSV file> : output the CSV file
-width <size> : make JPG/PNG with specified width size (default: 800)
-height <size> : make JPG/PNG with specified height size (default: 600)
-startdate <date> : will graph the range begining at that time
-enddate <date> : will graph the range until that date
-solarisPagesize <pagesize in B>: will set solaris pagesize
-wizard: open with unified login popup
-replaceShortcut <xml file>: replace all shortcut with those in the xml file
-addShortcut <xml file>: add shortcut from the xml file
-startup: open window marked for opening at startup

The ksar.jar file was part of the zip file we extracted earlier.

[root@linuxnix kSar-5.0.6]# pwd
[root@linuxnix kSar-5.0.6]# ls kSar.jar

Using ksar with its GUI interface is great but as system administrators, we generally spend most of our time on the command line so in the next few examples I’ll demonstrate how we use the command line interface.

Let’s generate an input file.

LC_ALL=C sar -r -f /var/log/sa/sa14 > /tmp/sar-$(hostname)_mem.txt

This uses the same sar log file /var/log/sa/sa14 that we used earlier but this time we’ll be extracting memory metrics instead of disk metrics.

Here’s the output of the text file we just created.

[root@linuxnix tmp]# file sar-linuxnix_mem.txt
sar-linuxnix_mem.txt: ASCII text
[root@linuxnix tmp]#
[root@linuxnix tmp]# cat sar-linuxnix_mem.txt
Linux 2.6.32-642.el6.x86_64 (linuxnix) 10/14/17 _x86_64_ (1 CPU)

00:00:01 kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit
00:10:01 668048 336088 33.47 20964 121812 316168 10.41
00:20:01 668188 335948 33.46 20964 121820 316168 10.41
00:30:02 656820 347316 34.59 21312 123888 335328 11.05
00:40:01 581176 422960 42.12 22292 198052 335328 11.05
00:50:01 577356 426780 42.50 22924 201084 335328 11.05
Average: 630318 373818 37.23 21691 153331 327664 10.79

Now that we have our input file, let’s create some graphs using ksar on the command line.

The first example uses the sar-linuxnix_mem.txt as input, generates graphs from that input and writes them to a pdf file named mem.pdf.

[root@linuxnix kSar-5.0.6]# java -jar kSar.jar -input 'file:///tmp/sar-linuxnix_mem.txt' -outputPDF mem.pdf
time to parse: 1274ms number of line: 9 line/msec: 0.0

Note: You must be in the directory where you extracted ksar to run this command because that contains the kSar.jar file.

This will create a pdf file named mem.pdf and add the graphs generated from the input file to this pdf. Here’s a screenshot of one of the graphs from the pdf file:

The ksar command

The ksar command


In the next example, we’ll write the data to jpg image files instead of a pdf file.

[root@linuxnix kSar-5.0.6]# java -jar kSar.jar -input 'file:///tmp/sar-linuxnix_mem.txt' -outputJPG linuxnix_test
time to parse: 202ms number of line: 9 line/msec: 0.0
[root@linuxnix kSar-5.0.6]# ls -l *.jpg
-rw-r--r--. 1 root root 111804 Oct 16 12:19 linuxnix_test_LinuxkbmemSar.jpg
-rw-r--r--. 1 root root 91524 Oct 16 12:19 linuxnix_test_LinuxkbmiscSar.jpg

Two jpg images were generated from the input file we provided. The name we provide after the -outputJPG option is used as a basename as does not constitute the entire file name as shown by the actual jpg image file names listed in the ls -l output.

Here is a screenshot of the image file linuxnix_test_LinuxkbmemSar.jpg

The ksar command

The ksar command


This concludes our exploration of sar and it’s associated sysstat utilities.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.