#!/bin/ksh
#
# Author		Jane Curry
# Creation date		13/3/98
# Modified		4/1/99
#
# Copyright Skills 1st Limited
#
# This script works best with a separate filesystem 
# 	/usr/OV/databases/snmpCollect.old
# The "live" /usr/OV/databases/snmpCollect/* files should first be copied
# 	to /usr/OV/databases/snmpCollect.old
#
. /home/jane/.profile
MONTH=`/bin/date +%m`
YEAR=`/bin/date +%Y`
# Note that this script should be run at the beginning of the new month,
# so the filename should reflect LAST month.  Check for month 12.
#
if [ $MONTH -eq 1 ]
then 
   MONTH=12
   ((YEAR-=1))
else
   ((MONTH-=1))
fi 

DATE=$MONTH$YEAR
#
# Check for filesystem space
# This works if .old directory is separate filespace or no
# du command deliberately doesn't have -k as we probably need twice the space
# in snmpCollect.old for temporary storage
#
DU=`/bin/du -s /usr/OV/databases/snmpCollect | /bin/cut -f1`
DF=`/bin/df -k /usr/OV/databases/snmpCollect.old | /bin/awk ' {print $3}' | /bin/grep -v Free`
if [[ $DF -lt $DU ]]
then
   echo Not enough space to copy filesystem - Archive??
   exit 0
#
# End here if insufficient space
#
else

#
#Need to do some juggling to get ! at end of filename
# Always got non ! file first from ls list
#
   for i in `/bin/ls /usr/OV/databases/snmpCollect`
   do
     if [[ `echo $i | /bin/awk ' /!$/ ' ` = "" ]]
     then
        /bin/cp /usr/OV/databases/snmpCollect/$i /usr/OV/databases/snmpCollect.old/$i.$DATE
        /bin/cp /usr/OV/databases/snmpCollect/"$i"! /usr/OV/databases/snmpCollect.old/"$i"."$DATE"!
     fi
   done
#
# Get data in format date time node value start end IP_addr
# Need to change to snmpCollect.old or snmpColDump can't find ! file
#
   cd /usr/OV/databases/snmpCollect.old
   for i in `/bin/ls /usr/OV/databases/snmpCollect.old`
   do
     if [[ `echo $i | awk ' /!$/ ' ` = "" ]]
     then
        /usr/OV/bin/snmpColDump -TtI $i | \
#
# Create archive file for last month
#
# Select only those that are NOT from the current month
# Output start, end, IP_addr, value
#
# Pipe into snmpColDump to overwrite last_month_file
#
      /bin/awk ' $1 !~ /^'"`/bin/date +%m`"'/ {print $5, $6, $7, $4} ' | /usr/OV/bin/snmpColDump -r - /usr/OV/databases/snmpCollect.old/$i
      fi
   done
#
# Need to do similar thing to truncate this months data in snmpCollect
#
  cd /usr/OV/databases/snmpCollect
   for i in `/bin/ls /usr/OV/databases/snmpCollect`
   do
     if [[ `echo $i | /bin/awk ' /!$/ ' ` = "" ]]
     then
        /usr/OV/bin/snmpColDump -TtI $i | \
#
# Create truncated file for this month
#
# Select only those that ARE from the current month
# Output start, end, IP_addr, value
#
# Pipe into snmpColDump to overwrite this_month_file
#
      /bin/awk ' $1 ~ /^'"`date +%m`"'/ {print $5, $6, $7, $4} ' | /usr/OV/bin/snmpColDump -r - /usr/OV/databases/snmpCollect/$i
      fi
   done
# Clean up ~ files left over
   /bin/rm /usr/OV/databases/snmpCollect.old/*~
   /bin/rm /usr/OV/databases/snmpCollect/*~
fi

