#!/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>


LOGFILE=$1

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

if [ $# -ne 1 ] ;then 
echo "lkst_plot_sysinfo <sysinfo_logfile>"
exit 1;
fi

if [ ! -f $LOGFILE ] ;then
echo "$LOGFILE is not found"
exit 1;
fi

function mkplotdata()
{
local i=1
local TM A1 A2 A3 A4 A5 A6 A7 A8
read -s NAME
read -s TM A1 A2 A3 A4 A5 A6 A7 A8
OTM=
NAME=`echo $NAME | tr -d " "` 
XTICS=
while true; do
read -s TM A1 A2 A3 A4 A5 A6 A7 A8
[ -z "$OTM" ] && OTM=$TM
[ -z "$TM" ] && break
echo `echo $TM - $OTM | bc` $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8
done
}

PDATA=`mktemp -p ./`
mkplotdata < $LOGFILE > $PDATA
NAME="sysinfo"

TITLE="system information"

gnuplot << EOF | sed -e "s/^currentpoint gsave translate 90 rotate 0 0 M/\
currentpoint gsave translate 45 rotate 0 0 M/g" \
-e s@"^(pages) Cshow"@\
"currentpoint gsave translate 45 rotate 0 0 M (pages) Cshow grestore"@g \
-e s@"^(counts) Cshow"@\
"currentpoint gsave translate 45 rotate 0 0 M (counts) Cshow grestore"@g \
>  $NAME.ps
set title "$TITLE"
set xlabel "sec"
set ylabel "pages"
set y2label "counts"
set format x "%g"
set xtics axis rotate
set y2tics
set terminal postscript color
set key box
#set key outside
plot "$PDATA" using 1:2 title "free" with lines,\
     "$PDATA" using 1:3 title "total" with lines, \
     "$PDATA" using 1:4 title "buffer" with lines, \
     "$PDATA" using 1:5 title "shared" with lines, \
     "$PDATA" using 1:6 axes x1y2 title "freefiles" with lines, \
     "$PDATA" using 1:7 axes x1y2 title "files" with lines, \
     "$PDATA" using 1:8 axes x1y2 title "unusedinodes" with lines, \
     "$PDATA" using 1:9 axes x1y2 title "inodes" with lines
EOF

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


exit 0;
