Dead Simple Templating with Sed (Gnuplot, ipvs, lvs)

Dead simple templating with sed (gnuplot, ipvs, lvs)

I thought I’d share my simple templating technique with sed and bash I use to generate gnuplots. The example below is used to generate plot from data collected from a LVS-DR loadbalancer, using the following command:

printf "$(date +'%Y-%m-%d %T'),%s,%s,%s,%s,%s\n" $(/sbin/ipvsadm -Lnc |
egrep -v "entries|state"|
awk '{print $3}' | sort | uniq -c | sed 's/[A-Z_]//g') >> output.file

Here’s the gnuplot template used:

set terminal pngcairo size 800,600 enhanced font 'Verdana,10'
set datafile separator ","
set title 'IPVS connections'
set output 'title-DATE.png'
set ylabel 'No. conns'
set xdata time
set autoscale x
set format y "%.f"
set timefmt "%Y-%m-%d %H:%M:%S"
set xrange ["DSTART":"DEND"]
set format x "%m/%d %H:%M"
set xtics "DSTART", INT, "DEND" rotate font ",8"
set style line 11 lc rgb '#808080' lt 1
set border 3 ls 11
set key box opaque vert right top
set style line 12 lc rgb'#808080' lt 0 lw 1
set grid back ls 12
set style line 1 lc rgb '#FF5200' lt 1 lw 2 # Orange
set style line 2 lc rgb '#0115F8' lt 1 lw 2 # Blue
set style line 3 lc rgb '#FF00E2' lt 1 lw 2 # Purple
set style line 4 lc rgb '#02FF00' lt 1 lw 2 # Green
set style line 5 lc rgb '#FF000A' lt 1 lw 2 # Red
set style fill transparent solid 0.5 noborder
set tics
plot "conns.csv" using 1:2 title 'CLOSE' w lines ls 1,'' using 1:3 title 'ESTABLISHED' w lines ls 2,'' using 1:4 title 'FIN\_WAIT' w lines ls 3,'' using 1:5 title 'NONE (HTTPS)' w lines ls 4,'' using 1:6 title 'SYN\_RECV' w lines ls 5

The CAPS are the template placeholders.

And the simple script used to generate the actual plots, which is where the sed is used for templating:

date=$(echo $dend | awk '{print $1}')
sed 's/DSTART/'"$dstart"'/g;
     s/DATE/'"$date"'/g' $plottpl > $plotwrk
gnuplot $plotwrk