#!/bin/bash
# plot sysinfo from logfile
# Copyright (C) HITACHI,LTD. 2005
# WRITTEN BY HITACHI SYSTEMS DEVELOPMENT LABORATORY,
# Created by M. Hiramatsu <hiramatu@sdl.hitachi.co.jp>
#            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="sysinfo"
TITLE="system information"
NR_ARGS=8
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_sysinfo2 <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 y2label "counts"
set format x "%g"
set xtics axis rotate
set xrange [0:]
set y2tics
set terminal postscript landscape color
set output "${NAME}.ps"
set key box

plot \
"$PDATA" index 0 using 3:4 title "total" with lines,\
"$PDATA" index 1 using 3:4 title "free" with lines,\
"$PDATA" index 2 using 3:4 title "buffer" with lines,\
"$PDATA" index 3 using 3:4 title "shared" with lines,\
"$PDATA" index 4 using 3:4 axes x1y2 title "freefiles" with lines,\
"$PDATA" index 5 using 3:4 axes x1y2 title "files" with lines,\
"$PDATA" index 6 using 3:4 axes x1y2 title "unusedinodes" with lines,\
"$PDATA" index 7 using 3:4 axes x1y2 title "inodes" with lines
EOF

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


exit 0;
