#!/bin/sh
# PCP QA Test No. 555
# pmie syslog(3) changes and parsing -t tag and -p pri arguments in
# a syslog action
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

rm -f $seq.out
case $PCP_PLATFORM
in
    irix|linux)
	ln $seq.$PCP_PLATFORM $seq.out || exit 1
	;;
    darwin|solaris)
	ln $seq.linux $seq.out || exit 1
	;;
    *)
	_notrun "No qualified output exists for $PCP_PLATFORM"
	;;
esac

$sudo rm -f $seq.full /tmp/syslog.full /tmp/syslog.out

signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!

# sometimes don't want the -E variant of sudo, as this fails to run
# service rsyslog correctly (at least on bozo)
sudo_no_E=`which sudo`

update_config=true
systemctl=false
upstart=false
if [ -f /etc/rsyslog.conf -a -f /var/run/*syslogd.pid ]
then
    # smells like rsyslogd
    #
    ( echo "smells like rsyslogd"; ls -l /etc/rsyslog.conf /var/run/*syslogd.pid ) >>/tmp/syslog.full 2>&1
    daemon=rsyslogd
    config=/etc/rsyslog.d/pcpqa.conf
    update_config=false
    # For Ubuntu 9.10 (at least) SIGHUP to rsyslogd does not really
    # restart rsyslogd ... worse, /etc/init.d/rsyslogd may have been
    # converted to use "upstart"
    #
    if initctl version 2>&1 | grep -q upstart
    then
	upstart=true
    elif [ -f /etc/init.d/rsyslog ]
    then
	init=/etc/init.d/rsyslog
    elif [ -f /etc/init.d/syslog ]
    then
	# the SuSE way
	upstart=false
	init=/etc/init.d/syslog
    else
	if `which systemctl >/dev/null 2>&1` && [ -n "$PCP_SYSTEMDUNIT_DIR" -a -f $PCP_SYSTEMDUNIT_DIR/rsyslog.service ]
	then
	    systemctl=true
	else
	    echo "Botch: using rsyslog, but I don't know how to stop/start it"
	    exit 1
	fi
    fi
elif [ -f /etc/syslog.conf ]
then
    # regular old syslogd
    #
    ( echo "smells like old syslogd"; ls -l /etc/rsyslog.conf ) >>/tmp/syslog.full 2>&1
    daemon=syslogd
    config=/etc/syslog.conf
else
    _notrun "No /etc/*syslog.conf -- probably using a different syslog package"
fi

echo "upstart=$upstart" >>/tmp/syslog.full
echo "systemctl=$systemctl" >>/tmp/syslog.full
echo "init=$init" >>/tmp/syslog.full
echo "update_config=$update_config" >>/tmp/syslog.full
echo "daemon=$daemon" >>/tmp/syslog.full

# avoid systemd/systemctl wrapper
#
[ -f /etc/SuSE-release ] && export SYSTEMD_NO_WRAP=1
[ -f /etc/fedora-release ] && export SYSTEMCTL_SKIP_REDIRECT=1

cleanup()
{
    if [ -f $tmp.setup ]; then
	if $update_config
	then
	    [ -f $config.$seq ] && $sudo mv $config.$seq $config
	else
	    $sudo rm -f $config
	fi
	if [ $daemon = rsyslogd ]
	then
	    if $upstart
	    then
		$sudo_no_E service rsyslog restart >>/tmp/syslog.full 2>&1
	    elif $systemctl
	    then
		$sudo systemctl restart rsyslog
	    else
		# kill should be enough, but restart to be sure
		#
		# $sudo kill -HUP `cat /var/run/rsyslogd.pid` || exit
		$sudo $init restart >>/tmp/syslog.full 2>&1
	    fi
	else
	    $sudo $signal -a -s HUP $daemon || exit
	fi
	rm $tmp.setup
    fi
    [ -f /tmp/syslog.full ] && cp /tmp/syslog.full $seq.full
    $sudo rm -f /tmp/syslog.out /tmp/syslog.full
    rm -f $tmp.*
}
trap "cleanup; exit \$status" 0 1 2 3 15

# move aside the current syslog configuration, as we have NFI
# what kind of setup is in use and where our messages will go
#
setup()
{
    # let rsyslog create it, to ensure any security labels setup
    if [ $daemon != rsyslogd ]
    then
	$sudo cp /dev/null /tmp/syslog.out || exit
	$sudo chmod 666 /tmp/syslog.out || exit
    fi
    touch $tmp.setup
    if $update_config
    then
	$sudo mv $config $config.$seq || exit
    fi
    echo '*.*  /tmp/syslog.out' >$tmp.syslog.conf
    $sudo cp $tmp.syslog.conf $config || exit
    echo "config=$config" >>/tmp/syslog.full
    $sudo cat $config >>/tmp/syslog.full
    if [ $daemon = rsyslogd ]
    then
	if $upstart
	then
	    $sudo_no_E service rsyslog restart >>/tmp/syslog.full 2>&1
	elif $systemctl
	then
	    $sudo systemctl restart rsyslog >>/tmp/syslog.full 2>&1
	else
	    # kill should be enough, but restart to be sure
	    #
	    # $sudo kill -HUP `cat /var/run/rsyslogd.pid` || exit
	    $sudo $init restart >>/tmp/syslog.full 2>&1
	fi
    else
	$sudo $signal -a -s HUP $daemon || exit
    fi
}

# real QA test starts here
echo "ps before setup ..." >>/tmp/syslog.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '([P]ID)|([s]yslog)' >>/tmp/syslog.full
setup
echo "ps after setup ..." >>/tmp/syslog.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '([P]ID)|([s]yslog)' >>/tmp/syslog.full

# syslog	logger -p
# 5D		daemon.notice		<--- default
# 6D		daemon.info
# 4D		daemon.warning
# 5B		user.notice
# 6B		user.info
# 4B		user.warning
# 5Q		local0.notice
# 6Q		local0.info
# 4Q		local0.warning

cat <<'End-of-File' >$tmp.conf
// errors
e1 = hinv.ncpu > 0 -> syslog "-t";
e2 = hinv.ncpu > 0 -> syslog "-ttag extra";
e3 = hinv.ncpu > 0 -> syslog "-t" "tag extra";
e4 = hinv.ncpu > 0 -> syslog "-p";
e5 = hinv.ncpu > 0 -> syslog "-pinfo extra" "-pinfo [6D]" "{1}";
e6 = hinv.ncpu > 0 -> syslog "-p" "info extra" "-pinfo [6D]" "{2}";
e7 = hinv.ncpu > 0 -> syslog "-p" "evil" "-t" "bogus" "bad -p evil and -t" "{3}";
e8 = hinv.ncpu > 0 -> syslog "-p" "evil.info" "bad facil for -p evil.info [6D]" "{4}";
e9 = hinv.ncpu > 0 -> syslog "-p" "user.evil" "bad pri for -p user.evil [5B]" "{5}";

// OK
o1 = hinv.ncpu > 0 -> syslog "-t" "foo" "-p" "user.notice" "-t & -pinfo, 4 args [5B]" "{6}";
o2 = hinv.ncpu > 0 -> syslog "-t" "foo" "-puser.notice" "-t & -pinfo, 3 args [5B]" "{7}";
o3 = hinv.ncpu > 0 -> syslog "-tfoo" "-pinfo" "-t & -pinfo, 2 args [6D]" "{8}";
o4 = hinv.ncpu > 0 -> syslog "-p" "user.info" "-p user.info, 2 args [6B]" "{9}";
o5 = hinv.ncpu > 0 -> syslog "-pdaemon.info" "-pdaemon.info, 1 arg [6D]" "{10}";
o6 = hinv.ncpu > 0 -> syslog "-t" "foo" "-t 2 args" "{11}";
o7 = hinv.ncpu > 0 -> syslog "-tfoo" "-t 1 arg" "{12}";
o8 = hinv.ncpu > 0 -> syslog "no -t or -p args" "{13}";


// OK + holdoff
h1 = hinv.ncpu > 0 -> syslog 5 sec "-t" "foo" "-p" "local0.notice" "-t & -pinfo, 4 args [5Q] + holdoff" "{14}";
h2 = hinv.ncpu > 0 -> syslog 5 sec "-t" "foo" "-plocal0.notice" "-t & -pinfo, 3 args [5Q] + holdoff" "{14}";
h3 = hinv.ncpu > 0 -> syslog 5 sec "-tfoo" "-pinfo" "-t & -pinfo, 2 args [6D] + holdoff" "{15}";
h4 = hinv.ncpu > 0 -> syslog 5 sec "-p" "local0.info" "-p user.info, 2 args [6Q] + holdoff" "{16}";
h5 = hinv.ncpu > 0 -> syslog 5 sec "-pdaemon.info" "-pdaemon.info, 1 arg [6D] + holdoff" "{17}";
h6 = hinv.ncpu > 0 -> syslog 5 sec "-t" "foo" "-t 2 args + holdoff" "{18}";
h7 = hinv.ncpu > 0 -> syslog 5 sec "-tfoo" "-t 1 arg + holdoff" "{19}";
h8 = hinv.ncpu > 0 -> syslog 5 sec "no -t or -p args + holdoff" "{20}";

End-of-File

pmie -v -t 2sec -T 7sec >$tmp.log 2>&1 -c $tmp.conf &
wait

sed <$tmp.log \
    -e '/^$/d' \
    -e '/^[a-z][0-9][0-9]*: /d' \
    -e "/Info: evaluator exiting/d" \
    -e "s;$tmp;TMP;;"
echo "=== pmie output ===" >>/tmp/syslog.full
cat $tmp.log >>/tmp/syslog.full

sleep 5
echo

# note - runpriv lines noticed on krelly with SGIconsole 2.0
# note - agetty lines noticed on canary with SLES9 SP1 beta2
# note - DHCPDISCOVER lines noticed on kenj-laptop with Ubuntu 8.10
#
echo "=== all of syslog ===" >>/tmp/syslog.full
ls -l /tmp/syslog.full >>/tmp/syslog.full
$sudo cat /tmp/syslog.out >>/tmp/syslog.full
echo >>/tmp/syslog.full
echo "=== syslog for pid $! ===" >>/tmp/syslog.full
# More recent syslog format seen on Ubuntu 11.04
# (high-precision rsyslog timestamps, traditional-mode no longer enabled)
# 2011-07-09T10:38:40+10:00 HOST pcp-pmie[PID]: 
#
$sudo grep '\['$!']:' /tmp/syslog.out \
| tee -a /tmp/syslog.full \
| sed \
    -e 's/^[A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/DATE/' \
    -e 's/^[0-9][0-9][0-9][0-9]-[0-9][0-9]*-[0-9][0-9]*T[0-9][0-9]:[0-9][0-9]:[0-9][0-9][^ ]* /DATE /' \
    -e "s/\\[$!]/[PID]/" \
    -e "s/`hostname | sed -e 's/\..*//'` /HOST /" \
    -e 's/[0-9][A-Z]:HOST/HOST/' \
    -e '/ sudo: .*syslog.out$/d' \
    | LC_COLLATE=POSIX sort \
    | uniq

# success, all done
status=0
exit
