#!/bin/sh
#
# Plot time-series data from LFW stats script
# data lines are formatted: yymmdd@hh:mm:ss value ... value
#
# Corey Satten, corey @ cac.washington.edu, 3/19/02 - version 0.5

COLORS="xffffff x000000 x404040 x00bf00 x0000ff xff0000 xcc00ff xd56826 xffb500 x10d9c7 xbda5bd x558ac2"
for i in $*; do
  case "$1" in
    -p) TERMSET='postscript color solid "Courier" 12';	EXT="ps";  SIZE=1.0,1;;
    -n) TERMSET="png color";				EXT="png"; SIZE=1.6,1;;
    -g) TERMSET="gif $COLORS";				EXT="gif"; SIZE=1.6,1;;
    -t) TERMSET=tek410x;;
    -y) Y2RANGE="$2"; shift;;
    -s) SIZE="$2,1"; shift;;
    -)  ;;
    *)  break;;
  esac; shift
done

case "$1" in *.gz)
    TMP=/tmp/`basename $1 .gz`.$$
    trap "rm -f $TMP" 0 1 2 13 15
    zcat < $1 > $TMP
    set $TMP
    ;;
esac
case $# in 0)
    TMP=/tmp/stats.$$
    trap "rm -f $TMP" 0 1 2 13 15
    cat > $TMP
    set $TMP
    ;;
esac

FILE=$1
case "$EXT" in '');; *)
    OUTPUT="`echo $FILE|sed 's|.*/||; s/\..*$//'`.$EXT";;
esac

echo "
set title \"Firewall Usage Stats\"
set ylabel \"Memory, Filesystem, CPU Percentage Utilized\"
set y2label \"Packets/Sec, Connected States          \"
set key outside ${EXT+samplen -9999}
set ytics nomirror 0, 20, 100; set y2tics nomirror; set tics out
#set nomxtics
set xdata time
set timefmt \"%y%m%d@%H:%M:%S\"
set format x \"%m/%d\\n%R\"
set yrange [-30:100]
set y2range [-10:]
${SIZE+set size $SIZE}
${Y2RANGE+set y2range [-10:$Y2RANGE]}
set term ${TERMSET-x11}
${EXT+set output \"$OUTPUT\"}
# \"$FILE\" u 1:8  axis x1y1 title \"% etc\" w l
plot \
  \"$FILE\" u 1:13 axis x1y2 title \"# states \" w l, \
  \"$FILE\" u 1:16 axis x1y2 title \"# pk fwd \" w l, \
  \"$FILE\" u 1:15 axis x1y2 title \"# pk!fwd \" w l, \
  \"$FILE\" u 1:3  axis x1y1 title \"% cpu    \" w l, \
  \"$FILE\" u 1:5  axis x1y1 title \"% mem use\" w l, \
  \"$FILE\" u 1:6  axis x1y1 title \"% mem act\" w l, \
  \"$FILE\" u 1:9  axis x1y1 title \"% tmp use\" w l, \
  \"$FILE\" u 1:10 axis x1y1 title \"% var use\" w l, \
  \"$FILE\" u 1:11 axis x1y1 title \"% log use\" w l
" | gnuplot -persist

case "$EXT" in "gif")	# gnuplot generates uncompressed GIFs, try to compress
    (giftopnm|ppmtogif) 2>&- <$OUTPUT >$OUTPUT.$$ && mv $OUTPUT.$$ $OUTPUT
    rm -f $OUTPUT.$$ ;;
esac
