#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# igmp_accounting_collect - Collect and tar accounting logs
#
# January 2011, Thomas Dalton
#
# Copyright (c) 2011 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

_display_usage() {
    echo "Usage: igmp_accounting_collect <options>"
    echo "    d: Date in YYMMDD format"
    echo "       if none specified all logs collected"
}

__cwd=`pwd`;
__dir="harddisk:/usr/data/igmp/";
__fileout="accounting.tar";
__date="*";   # Match any date
__tarcreated="0";
__tar_args="-cvf"; # Create the tarfile first

# Parse the arguments to the script.
while [[ "$#" -gt "0" ]]; do
    case "$1" in
        -d) __date="$2"; shift 2;;
        *) _display_usage; exit;;
    esac
done

# Tar the log files.
_archive() {
    tar $__tar_args /$__dir$__fileout accounting.dat*.${__date};
    if [[ "$?" = "0" ]]; then
        echo ":: --> Added log files to $__dir$__fileout";
        __tarcreated="1";
        __tar_args="-rvf"; # Append further logs to archive
    else
        echo ":: --> No log files added to $__dir$__fileout";
    fi
}

echo ":: Gathering IGMP accounting log files";
# Loop through all nodes adding the logfiles to the archive
node_list=$(find /net -maxdepth 1 -name "node*");
for node in $node_list 
do
    cd $node/$__dir;
    if [[ "$?" = "0" ]]; then
        echo ":: --> Adding accounting data from `pwd` to archive";
        _archive;
    else
        echo ":: --> No accounting data in $node";
    fi
done

# Zip the tar file.
cd /;
if [[ "$__tarcreated" = "0" ]]; then
    echo ":: No log file archive to compress";
else
    echo ":: Compressing log files";
    gzip /$__dir$__fileout;
    if [[ "$?" = "0" ]]; then
        echo ":: ${__dir}${__fileout}.gz successfully written, ready to copy";
    else 
        echo ":: Failed to write ${__dir}${__fileout}.gz";
    fi
fi

# Put us back in the working directory.
cd $__cwd;
