Monday, February 28, 2011

Graphing sar output

In Linux, sysstat package installs tools: sar, iostat .. , in the mean time, setups  a cron job to run sar periodically. The sar binary output  files are in /var/log/sa or /var/log/sysstat.
The files are very useful  for troubleshooting performance issues, if you don’t have monitoring solution in place.
To visualize the data into graph, you can use generic plotting tool: gnuplot or special tool designed for sar: ksar.

Visualize sar output  by  gnuplot

gnuplot can be directly installed online  in most Linux distributions.
file saved by sar cron job is binary, convert it to ascii format. The following example output CPU usage

$LC_ALL=C;sar -u -f /var/log/sa/sa27  | egrep  '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'    | sed  '1s/^/#/' >sar-cpu.log

LC_ALL=C to ensure time format is H:M:S
Sed  is used to add comment line to the first line: the header.
Create gnuplot script to show user CPU (3th column)  and system cpu  (5th column) usage
$cat cpu.p
set title 'HOST  CPU usage'
set xdata time
set timefmt '%H:%M:%S'
set xlabel 'time'
set ylabel ' CPU Usage'
set style data lines
plot 'sar-cpu.log' using 1:3 title 'User' ,\
'sar-cpu.log' using 1:5 title 'System'

Type ‘gnuplot’ to enter interactive shell then run the script.
gnuplot> load ‘cpu.p
$gnuplot -persist cpu.p


#Other advanced operations
#Zoom in, display a set period of data only
gnuplot> set xrange ['01:51:01':'03:51:01']
gnuplot> replot
#Save the output to image 
gnuplot> set terminal png         
gnuplot> set output "cpu.png"  
gnuplot> replot
Visualize sar output by ksar
The generic graphing tool, gnuplot, can process any data, it is not designed for sar. As a trade-off, it needs lots of customization.
ksar is specifically desgined for sar and understand Linux, Mac and Solaris sar output.
Kar can be downloaded at, Ksar is written in Java, so Java executables are prerequisite for ksar.
#Convert sar binary output to ascii for ksar, “-A” means include all counters
$LC_ALL=C;sar -A -f /var/log/sa/sa27 >sar-all.log
It is very easy to view any counter, once sar output files are imported into ksar.
[root@ kSar-5.0.6]$./ -help
[root@ kSar-5.0.6]$./  -input  /tmp/sar-all.log


1 comment: