#! @KSH_PATH@

# add_txrx - add two new Data Sources called 'tx' and 'rx' to an RRD file

# $Id: add_txrx.in,v 1.2 2001/02/11 20:43:25 dplonka Exp $
# Dave Plonka <plonka@doit.wisc.edu>

# This utility is used when upgrading from FlowScan-1.005 (or less) to
# FlowScan-1.006 (or greater).  It is used to add two new Data Sources
# to FlowScan ".rrd" files. e.g.:
# 
#   $ make install # install FlowScan-1.006 in your existing FlowScan dir
#   $ cd $prefix/graphs
#   $ add_txrx total.rrd *.*.*.*_*.rrd
#
#   tx - a count of the unique source IP addresses that have transmitted flows
#   rx - a count of the unique destination IP addresses that have received flows
#
# These Data Sources will allows FlowScan users to calculate the average
# number of bytes, packets, and flows per host and to determine the overall
# level of activity (in terms of numbers of individual hosts) for each subnet
# and for the entire campus.
#
# The values will be recorded only if you use the TopN directive with the
# CampusIO and/or SubNetIO reports.  TopN was introduced in FlowScan-1.006.
#
# For instance, if you set CampusIO's TopN to a value greater than zero,
# the tx value in FlowScan's "total.rrd" is the number of unique source
# IP addresses (hosts) from which FlowScan has seen outbound flows.

# { CONFIGURATION SECTION START ################################################

# { external commands used by this script:

typeset head="@HEAD_PATH@"
typeset grep="@GREP_PATH@"
typeset cp="@CP_PATH@"
typeset mv="@MV_PATH@"
typeset rm="@RM_PATH@"

typeset perl="@PERL_PATH@"
typeset rrdtool="@RRDTOOL_PATH@"

typeset add_ds="@prefix@/bin/add_ds.pl"

# }

# } CONFIGURATION SECTION END ##################################################

typeset script=${0##*/}

if (( $# < 1 ))
then
   print -u2 "usage"
   exit 2
fi

for file in "$@"
do
   if ${rrdtool?} fetch ${file?} AVERAGE -s 0 -e 0 \
      | ${head?} -1 | ${grep?} 'tx.*rx' >/dev/null
   then
      print -u2 "${script}: \"${file?}\" appears to have tx and rx already"
      continue
   fi
   if ${rrdtool?} dump ${file?} \
         | ${add_ds?} 2 NaN GAUGE 400 NaN NaN \
            | ${perl?} -pe 's/>\s*ds1\s*</>tx</; s/>\s*ds2\s*</>rx</' \
               > .${file%.rrd}.xml \
      && ${rrdtool?} restore .${file%.rrd}.xml .${file?} \
      && ${cp?} .${file?} ${file?} # cp(1) instead of mv(1) to save permissions
   then
      print -u2 "${script}: \"${file?}\" done"
   else
      exit 1
   fi
   ${rm?} -f .${file?} .${file%.rrd}.xml
done

exit 0
