#!/bin/bash

# If a log port is configured, set up an rsyslog conf to send all
# dataplane syslogs to rsyslog on the controller.

CONF_FILE=/etc/vyatta/dataplane.conf
OUTPUT=/etc/rsyslog.d/dist-vplane.conf

LOGPORT=$(awk -F "=" '/log/ {print $2}' $CONF_FILE | xargs)

if [ "$LOGPORT" = "" ]; then
   exit 0;
fi

# Get the controller ip address to use as the target address
TARGET=$(awk -F "=" '/ip/ {print $2;exit}' $CONF_FILE | xargs)

echo "# Autogenerated by $0" > $OUTPUT
echo "if \$programname == 'dataplane' then {" >>$OUTPUT
echo -n "    action(type=\"omfwd\" Target=\"$TARGET\" " >> $OUTPUT
echo "port=\"$LOGPORT\" Protocol=\"udp\")" >> $OUTPUT
echo "}" >> $OUTPUT

service rsyslog restart
