#!/usr/bin/sh
#
# ident	"@(#)rotatecodlog	1.14	02/10/18 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All rights reserved.
#
# codlogrotate script.
#

###############################################################################
#
# This script archives COD log files (cod.log).  Normally, it should be
# run once per month.  The number of logs to be kept is controlled by
# the shell variable called "MAX" below.
#
###############################################################################
MAX=12
LOGFILES="cod.log"
# Set default textdomain for I18N.
TEXTDOMAIN=%TEXT_DOMAIN% export TEXTDOMAIN
LASTOIDFILE=cod.lastOid
TIMEOUT=60
COMMUNITY=P-private
RETRIES=3
CODSTARTTIMEOID=1.3.6.1.4.1.42.2.70.19.4.0

fatal()
# Print an error message and exit with (bad) status 2.
{
	gettext "ERROR!" >&2
	echo "  $*" >&2
	gettext "ABORTING!" >&2
	exit 2
}


RELOCATIONDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
BASEDIR=$RELOCATIONDIR/SUNWsymon
CODBASEDIR=/var/${BASEDIR}/cod
ACTUAL_SENDER=${BASEDIR}/modules/sbin/pvt_codsendlog
check_osver() {
    if [ -z "$1" ]; then
        OSVERS=`/usr/bin/uname -r`
    else
        OSVERS="$1"
    fi
 
    case $OSVERS in
        5.5.1*) OSVERS=2.5  ;;
        5.6*)   OSVERS=2.6  ;;
        5.7*)   OSVERS=2.7  ;;
        5.8*)   OSVERS=2.8  ;;
        5.9*)   OSVERS=2.9  ;;
        *)      echolog 'Unsupported OS version: $2' "$OSVERS"
                exit 1      ;;
    esac
}

OSVERS=`/usr/bin/uname -r`
check_osver ${OSVERS}

SNMPSET=${BASEDIR}/util/bin/sparc-sun-solaris${OSVERS}/snmpset
SNMPSETCMDLINE="${SNMPSET} -t ${TIMEOUT} -c ${COMMUNITY}"

if [ $# -ne 0 -a $# -ne 1 ]
then
    usage
    exit 1
fi
if [ $# -eq 1 -a "$1" = "-h" ]
then
    usage
    exit 0
fi
if [ $# -eq 1 ]
then
    SSNList=$1
else
    # SSNList is all the subdirectories of ${CODBASEDIR} which has
    # the file cod.lastOid present
    SSNList=""
    currDir=`pwd`
    cd ${CODBASEDIR}
    for subdir in *
    do
	if [ ! -d ${subdir} -o ! -f ${subdir}/${LASTOIDFILE} ]
	then
	    continue
	fi
	SSNList="${SSNList} ${subdir}"
    done
    cd ${currDir}
fi

for SSN in ${SSNList}
do
#
# Send cod.log to Sun
#
    ${ACTUAL_SENDER} monthly ${SSN}

# Now archive the files (actually just cod.log)
    LOGDIR=${CODBASEDIR}/${SSN}
    cd $LOGDIR
    scName=`grep "^ScName: " ${LASTOIDFILE} | sed -e "s/^ScName: //"`
    if [ "${scName}" = "" ]
    then
	fatal "Improper COD Log format"
    fi
    
    for log in $LOGFILES; do
	i=$MAX

	while [ $i -gt 0 ]; do
        	j=`expr $i - 1`
        	test -f $log.$j && mv -f $log.$j $log.$i
        	i=$j
	done

	test -f $log && mv -f $log $log.$i
	touch $log
	chown root $log
	chgrp staff $log
	chmod g-w $log
	chmod o-rw $log
    done
    # After rotating, we need to inform the SC, so that it
    # will start fresh from the beginning.
    # Do this by attempting a snmpset operation on the
    # codStarttime object - the attempt will cause the
    # SC to reset its COD signatures etc.
    retVal=`${SNMPSETCMDLINE} -h ${scName} ${CODSTARTTIMEOID} Integer 12143479 2>/dev/null`
    if [ $? -ne 0 ]
    then
	echo "Could not communicate with SC " ${scName}
    fi
done
