#!/bin/bash
# This is the script to start and stop the db2snapshot profiler.
#
# reqparam value db2 - path to the db2 program
# reqparam value dbuser - Name of the database user for the snapshots to be made using
# reqparam value interval - Collection interval
# reqparam value db - Name of the database for the snapshots to be made of
#

# adapted from oprofile script, Karl Rister

. /etc/autobench.conf || . functions

DB2=$1
DBUSER=$2
INTERVAL=$3

# figure out the list of databases
shift; shift; shift
DBS=""
until [ -z "$1" ]
do
  DBS="$DBS $1"
  shift
done

for i in $DBS
do
  REPORT_DEST="$LOGDIR/analysis/db2snapshot.$i.$RUN_NUMBER"
  mkdir -p $REPORT_DEST
  chmod 777 $REPORT_DEST
  touch $AUTODIR/var/tmp/db2snapshot.$i.running

  su - $DBUSER -c "$DB2 connect to $i; \
                   OUTNUM=001; \
                   while [ -e $AUTODIR/var/tmp/db2snapshot.$i.running ]; do \
                     $DB2 get snapshot for database on $i > $REPORT_DEST/snapshot.\$OUTNUM; \
                     OUTNUM=\$(echo \$OUTNUM | awk '{printf (\"%03d\", \$1 + 1)}'); \
                     sleep $INTERVAL; \
                   done; \
                   $DB2 connect reset" &
done
