#!/bin/sh

# A simple shell script to format the logs similar to the Linksys
# logviewer format.

# Copyright (C) 2002 Marc Niegowski - Connectivity, Inc.
# All rights reserved.

# 23 W. Fourth Street
# Media, PA 19063-2805
# USA

# Phone: (610) 566-0227
# Fax:   (610) 566-0641
# Email: Marc@Tech-Center.com
# Web:   http://www.marcsweb.com
# Ftp:   ftp://ftp.marcsweb.com

# Notes:
# ------
# You can pipe the output of this script into sed to extract specific
# log records.

# Examples:
# ---------
#
# 1. Print only the entries containing 216.71.30.122
#
# ./formatlog | sed -n /216.71.30.122/p
#
# 2. Print only the outgoing messages on February 1
#
# ./formatlog | sed -n /"Feb 1.* out "/p

if test -z "$1"
   then L=/var/log/snmplog/snmplog.messages
   else L=$1
fi

if test -f $L
   then  C=`sed -n \$= $L` || \
         {
         echo -e "\n"
         echo Error trying to open/read log file $L.
         echo -e "\n"
         exit -1
         }
         if test -z $C
            then echo -e "\n"
                 echo Log file $L is empty.
                 echo -e "\n"
                 exit -1
         fi

         sed -n -e s/" in "/" in  "/                     \
                -e s/"[io][nu]..[A-Za-z0-9._-]* "/\&\[/  \
                -e s/" \["/\[/                           \
                -e s/"\[[0-9]*"/"&] ==>"/                \
                -e s/" ==> [A-Za-z0-9._-]* "/\&\[/       \
                -e s/" \["/\[/                           \
                -e s/"==> [A-Za-z0-9._-]*\[[0-9]*"/\&\]/p\
                   $L
	echo
	echo `sed -n \$= $L` Records in log.
	echo
	exit 0
   else echo -e "\n"
        echo Log file $L doesn\'t exist.
        echo -e "\n"
        exit -1
fi

exit 0 
