#! /bin/sh

#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>

##################################################################
# case 8: Test all the facilities at a particular level.         #
#                                                                #
#         Facilities available are: LOG_KERN, LOG_USER, LOG_MAIL #
#         LOG_DAEMON, LOG_AUTH, LOG_LPR.                         #
#         Don't know how to send kernel messages from syslog()   #
#                                                                #
#         o Create seperate entries in config file for each      #
#            facility.                                           #
#         o Send the message and grep for the entry in log file. #
##################################################################

syslog_case8()
{
        #set the trap to handle signals.
        trap '
                echo Testing is terminating due to a signal...
                exit 1
        ' 1 2 3 6 11 15

        echo
        echo "syslog: Testing all the facilities...."

        facility_no=1
        facility="user mail daemon auth lpr"
        for current in $facility
        do
                echo
                echo "Doing facility: $current..."

                # Create the configuration file specific to this facility
                # Level is fixed at info.
                echo "$current.info    /var/log/messages" > /etc/syslog.conf
                echo "$current.info    /var/log/maillog" >> /etc/syslog.conf

		#Restart syslog
		kill -1 `ps -ef | grep syslogd | grep -v grep | awk '{ print $2 }'`

                # Grepping pattern is as follows:
                # syslogtst: $current info test.
                oldvalue=`grep -c "syslogtst: $current info test." /var/log/messages`
                old_mail_check=`grep -c "syslogtst: $current info test." /var/log/maillog`

                # syslogtst has to be called with one more additional facility argument(1-6)
                syslogtst 8 $facility_no 2>/dev/null
                if [[ $? -ne 0 ]]; then
                        status_flag=1
                        return
                fi
                sleep 2
                new_mail_check=`grep -c "syslogtst: $current info test." /var/log/maillog`
                newvalue=`grep -c "syslogtst: $current info test." /var/log/messages`

                (( diff=$newvalue - $oldvalue ))
                (( mail_check=$new_mail_check - $old_mail_check ))
                if [[ $current == "mail" ]]; then
                        if [[ $mail_check -ne 1 ]]; then
                                status_flag=1
                                echo "****--- Facility $current failed ---****"
                        elif [[ $mail_check -eq 1 ]]; then
                                echo "****--- Facility $current passed ---****"
                        fi
                elif [[ $diff -ne 1 ]]; then
                                status_flag=1
                                echo "****--- Facility $current failed ---****"
                        else
                                echo "****--- Facility $current passed ---****"
                fi
                # Increment the facility_no for next...
                facility_no=$(($facility_no+1))
        done
}

echo start $0
echo "----------------------------------------------------------------"
status_flag=0

#Back up /etc/syslog.conf
cp /etc/syslog.conf /etc/syslog.conf.bak08
syslog_case8

if [ $status_flag -eq 0 ]
then
	echo
	echo $0 "PASSED"
else
	echo
	echo $0 "FAILED"
fi
echo "----------------------------------------------------------------"

#Restore syslog.conf
mv /etc/syslog.conf.bak08 /etc/syslog.conf

#Restart syslog
kill -1 `ps -ef | grep syslogd | grep -v grep | awk '{ print $2 }'`

exit $status_flag
