#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: misclog_get.sh,v $
# Revision 1.1.1.1  2003/12/11 15:40:53  ajay
# Importing Evm sources.
#
# Revision 1.1.1.1  2002/09/12 15:43:59  lsn
# EVM source - Linux initial version
#
# Revision 1.1.12.1  2001/09/26  18:58:42  Anthony_Hoffman
# 	redo of wildcatos submits
#
# Revision 1.1.10.1  2001/07/30  18:08:41  Anthony_Hoffman
# 	wc.evm.002.portability
#
# Revision 1.1.8.5  2000/02/25  22:06:11  Jem_Treadwell
# 	Removed sulog retrieval code - not a supported log
#
# Revision 1.1.8.4  2000/01/21  22:54:22  Jem_Treadwell
# 	Added cronlog and sulog
#
# Revision 1.1.8.3  2000/01/13  21:43:34  Jem_Treadwell
# 	Move merge program earlier in pipe to enable filtering on priority
#
# Revision 1.1.8.2  2000/01/04  15:54:31  Jem_Treadwell
# 	Merge from zinc BL3
#
# Revision 1.1.8.1  1999/12/17  17:57:47  Jem_Treadwell
# 	QAR 75908 - prevent wildcard expansion of star in filter string
#
# Revision 1.1.6.1  1999/12/15  18:33:43  Peter_Wolfe
# 	Fix for qar 75505. /var/adm/sysman is now a directory
# 	and the smsd logs are now in a sysman_station/logs
# 	like they always should have been
#
# Revision 1.1.4.3  1999/03/30  21:40:19  Jem_Treadwell
# 	Final drop for BL23 from EVM shared sandbox
# 	[1999/03/29  21:31:24  Jem_Treadwell]
#
# Revision 1.1.2.3  1999/03/29  20:03:59  Jem_Treadwell
# 	QAR 69538: Added smsd message file to misclog channel's cleanup and
# 	retrieval functions.
# 	[1999/03/29  20:03:21  Jem_Treadwell]
# 
# Revision 1.1.2.2  1999/03/16  19:28:30  Jem_Treadwell
# 	Initial submit of misclog channel - QAR 69538
# 	[1999/03/16  19:27:45  Jem_Treadwell]
# 
# $EndLog$
# 
# @(#)$RCSfile: misclog_get.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:53 $
# 

# This event retrieval script retrieves log lines from
# selected logfiles, converts them to EVM events, 
# and writes them to stdout.  If a filter argunment is
# supplied only those events matching the supplied
# filter are output.

#===================================================================
# Function: EVM message retriever
#
# This function converts messages produced by the EVM daemon
# and its family (logger & channel manager)into EVM events,
# and writes them to stdout.
#
# The input log format is:
#    yyyy/mm/dd hh:mm:ss log-message
#===================================================================
get_evm_messages()
{
    awk -v host=$HOST -v user=$USER -v evname=$EVENT_NAME \
    '/^[12][09][0-9][0-9]-/ {
		date = $1
		time = $2

		$1 = $2 = ""
		sub("^ *","",$0)

		printf("%s %s %s %s %s %s\n",evname,date,time,host,user,$0);
	}' | $CONVTOOL 
}



#===================================================================
# Main processing
#===================================================================

. /usr/sbin/evmcommandset

USAGE="Usage: $0 [-f filter-string]"

DIR=`${DIRNAME} $0`
CONVTOOL=$DIR/../bin/text2evm
MRGTOOL=$DIR/../bin/merge_template   # Merges template info into EVM event 

VP=`evminfo -vp`
EVENT_NAME_PREFIX=$VP.misclog

HOST=`hostname`

FILTER=

# Inhibit filename expansion while we handle the arguments, to avoid
# wildcards in the filter arg being expanded into filenames:
set -f

#=======================
# Handle start options:
#=======================
while [ $# -gt 0 ]
do
	case $1 in
	-f)	if [ $# -lt 2 ]
		then
			${ECHO} $USAGE >&2
			exit $EXIT_USAGE
		fi
		FILTER="-f \"$2\""
		shift 2
		;;

	*)	${ECHO} $USAGE >&2
		exit $EXIT_USAGE
		;;
	esac
done

# Re-enable filename expansion:
set +f

#====================
# EVM message files:
#====================
EVM_LOGDIR=/var/evm/adm/logfiles
EVM_LOGFILES="evmdaemon evmlogger evmchmgr"
EVM_LOGFILE_EXTENSIONS="log log.old"

for logfile in $EVM_LOGFILES
do 
    for ext in $EVM_LOGFILE_EXTENSIONS
    do
        thislog=$EVM_LOGDIR/$logfile.$ext
        if [ -r $thislog ]
        then
	    # Set the user-name to the owner of the file:
	    USER=`${LS} -l $thislog | awk '{print $3}'`
	    EVENT_NAME=$EVENT_NAME_PREFIX.$logfile
	    set -f
	    get_evm_messages < $thislog  | $MRGTOOL |
			eval /usr/bin/evmshow -r $FILTER
	    set +f
        fi
    done
done

#================================
# Sysman Station message files:
#================================
SMSD_LOGDIR=/var/adm/sysman/sysman_station/logs
SMSD_COMPONENT_NAME=smsd

EVENT_NAME=$EVENT_NAME_PREFIX.$SMSD_COMPONENT_NAME

if [ -d $SMSD_LOGDIR -a -x $SMSD_LOGDIR ]
then
    HERE=`pwd`
    cd $SMSD_LOGDIR

    # The filename pattern is smsd_yyyy-mm-dd_hh:mm:ss.log_nnn
    SMSD_LOGFILES="smsd_[12][0-9\-_:]*.log_[0-9][0-9][0-9]"
    for logfile in $SMSD_LOGFILES
    do 
        if [ -r $logfile ]
        then
	    # Set the user-name to the owner of the file:
	    USER=`${LS} -l $logfile | awk '{print $3}'`
	    # The format is the same as an EVM log entry, so reuse the function:
	    set -f
	    get_evm_messages < $logfile | $MRGTOOL |
			eval /usr/bin/evmshow -r $FILTER
	    set +f
        fi
    done 

    cd $HERE
fi

#================================
# Cron log:
#================================
CRONLOG_DIR=/var/adm/cron
CRONLOG_COMPONENT_NAME=cronlog
CRONLOG_FILE=log

EVENT_NAME=$EVENT_NAME_PREFIX.$CRONLOG_COMPONENT_NAME

if [ -d $CRONLOG_DIR -a -x $CRONLOG_DIR -a -r $CRONLOG_DIR/$CRONLOG_FILE ]
then
    HERE=`pwd`
    cd $CRONLOG_DIR

    # Set the user-name to the owner of the file:
    USER=`${LS} -l $CRONLOG_FILE | awk '{print $3}'`
    set -f
    cat $CRONLOG_FILE | grep '^! ' | 
	awk -v host=$HOST -v user=$USER -v evname=$EVENT_NAME '
	{ date = $(NF-0) "/" $(NF-3) "/" $(NF-2)
	  time = $(NF-1)

	  $(NF-0) = $(NF-1) = $(NF-2) = $(NF-3) = $(NF-4) = ""
	  sub("^! *","",$0)

	  printf("%s %s %s %s %s cronlog: %s\n",evname,date,time,host,user,$0)
	}' | $CONVTOOL | $MRGTOOL | eval /usr/bin/evmshow -r $FILTER
    set +f

    cd $HERE
fi

exit
