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

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

# The Main-server, Standalone should not log to syslog.intern.

if echo "$PROFILE" | grep -q Main-Server ; then
    if grep -q '^\*.\*.*@syslog' /etc/syslog.conf ; 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
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

port=syslog
proto=udp

if netstat -a 2>&1 | grep ":$port " | grep -q "^$proto"; then
    echo "success: $0: syslog server is listening on $port/$proto."
else
    echo "error: $0: syslog server is not listening on $port/$proto."
    exit 1
fi

exit 0
