#!/bin/bash
# plot vminfo from logfile
# Copyright (C) HITACHI,LTD. 2005
# WRITTEN BY HITACHI SYSTEMS DEVELOPMENT LABORATORY,
# Created by H. Kawai <h-kawai@sdl.hitachi.co.jp>

# WARNING: if an arg description contains space character,
#          this script will not work correctly.

LKSTLA=/usr/bin/lkstla
NAME="vminfo"
TITLE="vm information"
NR_ARGS=6
LOGFILES=$*

function mkplotdata()
{
    local -i i
    local basetime
    
    basetime=`$LKSTLA $NAME -A1 -l $LOGFILES | head -n3 | tail -n+3 | \
              awk '{ print $3 }'`

    for ((i = 1; i <= NR_ARGS; i++)); do
	$LKSTLA $NAME -A $i -l$basetime $LOGFILES | tail -n+3
	echo -e "\n\n"
    done
}

if [ "$PDF_VIEWER" ] ; then
  VPDF=$PDF_VIEWER
else
  VPDF=`which acroread 2> /dev/null`;
  if [ -z "$VPDF" ]; then
   VPDF=`which xpdf 2> /dev/null`;
  fi
  if [ -z "$VPDF" ]; then
   VPDF=`which gpdf 2> /dev/null`;
  fi
  if [ -z "$VPDF" ]; then
   VPDF=echo
  fi
fi

if [ $# -lt 1 ] ;then 
    echo "lkst_plot_vminfo <log_files>"
    exit 1;
fi
for LOGFILE in $LOGFILES ; do
if [ ! -f "$LOGFILE" ] ;then
    echo "$LOGFILE is not found"
    exit 1;
fi
done

PDATA=`mktemp /tmp/tmp$$.XXXXXX`
mkplotdata > $PDATA

gnuplot << EOF
set title "$TITLE"
set xlabel "sec"
set ylabel "pages"
set format x "%g"
set xtics axis rotate
set xrange [0:]
set terminal postscript landscape color
set output "${NAME}.ps"
set key box

plot \
"$PDATA" index 1 using 3:4 title "free_pages" with lines,\
"$PDATA" index 2 using 3:4 title "nr_scan_active" with lines,\
"$PDATA" index 3 using 3:4 title "nr_scan_inactive" with lines,\
"$PDATA" index 4 using 3:4 title "nr_active" with lines,\
"$PDATA" index 5 using 3:4 title "nr_inactive" with lines
EOF

rm $PDATA
ps2pdf $NAME.ps
$VPDF $NAME.pdf

exit 0;
