#!/bin/bash
#
# Copyright (c) 2019, AT&T Intellectual Property.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only
#

# 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
