#!/bin/sh -e
#
# Test if the syslog server is working.

. /usr/share/debian-edu-config/testsuite-lib.sh

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

# Standalone profile currently has no syslog configuration ready
# and therefore exit gracefully if we are installing one
if echo $PROFILE | grep -q Standalone ; then
	exit 0
fi

# Test if syslog server is reachable
if ping -c1 syslog > /dev/null 2>&1 ; then
    echo "success: $0: Host 'syslog' is pingable."
else
    echo "error: $0: Host 'syslog' is not pingable."
    RESULT=1
fi

if grep -q '^\*.\*.*@syslog' /etc/rsyslog.d/* ; then
    forwarding=true
else
    forwarding=false
fi
# The Main-server, Standalone should not log to syslog.intern.
if echo "$PROFILE" | grep -q Main-Server ; then
    if [ true = $forwarding ] ; then
        echo "error: $0: Main-Server is forwarding syslog messages to @syslog"
    else
        echo "success: $0: Main-Server is not forwarding syslog messages to @syslog"
    fi
else # not Main-server or Standalone, should forward
    check_file_perm /etc/rsyslog.d/debian-edu-client.conf 644
    if [ true = $forwarding ] ; then
        echo "success: $0: rsyslog is forwarding syslog messages to @syslog"
    else
        echo "error: $0: rsyslog is not forwarding syslog messages to @syslog"
    fi
fi

# Only Main-Server and Thin-Client-Server should accept syslog
# messages from the net.

if echo "$PROFILE" | grep -q Main-Server ; then
    enabled=true
fi
if echo "$PROFILE" | grep -q Thin-Client-Server ; then
    enabled=true
fi

if [ true != "$enabled" ] ; then
    exit 0
fi

netstat_check syslog udp "syslog" || exit 1

exit 0
