#!/bin/bash
#/////////////////////////////////////////////////////////////////////////
#//
#// (c) University of Southampton IT Innovation Centre, 2017
#//
#// Copyright in this software belongs to University of Southampton
#// IT Innovation Centre of Gamma House, Enterprise Road,
#// Chilworth Science Park, Southampton, SO16 7NS, UK.
#//
#// This software may not be used, sold, licensed, transferred, copied
#// or reproduced in whole or in part in any manner or form or in or
#// on any media by any person other than in accordance with the terms
#// of the Licence Agreement supplied with the software, or otherwise
#// without the prior written consent of the copyright owners.
#//
#// This software is distributed WITHOUT ANY WARRANTY, without even the
#// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#// PURPOSE, except where stated in the Licence Agreement supplied with
#// the software.
#//
#//      Created By :            Michael Boniface
#//      Created Date :          13/12/2017
#//      Created for Project :   FLAME
#//
#/////////////////////////////////////////////////////////////////////////

# Install telegraf
if [ "$#" -ne 9 ]; then
    echo "Error: illegal number of arguments: "$#
      echo "Usage: install-clmc-agent.sh TELEGRAF_CONF_FILE LOCATION SFC_ID SFC_ID_INSTANCE SF_ID SF_ID_INSTANCE IP_ENDPOINT_ID INFLUXDB_URL DATABASE_NAME"
      exit 
fi

TELEGRAF_CONF_FILE=$1
LOCATION=$2
SFC_ID=$3
SFC_ID_INSTANCE=$4
SF_ID=$5
SF_ID_INSTANCE=$6
IP_ENDPOINT_ID=$7
INFLUXDB_URL=$8
DATABASE_NAME=$9

if [ ! -f $TELEGRAF_CONF_FILE]; then
    echo "Error: Telegraf conf template file not found: "$TELEGRAF_CONF_FILE
    exit
fi

wget https://dl.influxdata.com/telegraf/releases/telegraf_1.3.2-1_amd64.deb
dpkg -i telegraf_1.3.2-1_amd64.deb

# Copy configuration
echo "Telegraf config file: " $TELEGRAF_CONF_FILE
cp $TELEGRAF_CONF_FILE /etc/telegraf/telegraf.conf

echo "INFLUXDB_URL: " $INFLUXDB_URL
echo "DATABASE_NAME: " $DATABASE_NAME

# Replace template parameters
sed -i 's/{{LOCATION}}/'$LOCATION'/g' /etc/telegraf/telegraf.conf
sed -i 's/{{SFC_ID}}/'$SFC_ID'/g' /etc/telegraf/telegraf.conf
sed -i 's/{{SFC_ID_INSTANCE}}/'$SFC_ID_INSTANCE'/g' /etc/telegraf/telegraf.conf
sed -i 's/{{SF_ID}}/'$SF_ID'/g' /etc/telegraf/telegraf.conf
sed -i 's/{{SF_ID_INSTANCE}}/'$SF_ID_INSTANCE'/g' /etc/telegraf/telegraf.conf
sed -i 's/{{IP_ENDPOINT_ID}}/'$IP_ENDPOINT_ID'/g' /etc/telegraf/telegraf.conf
sed -i 's|{{INFLUXDB_URL}}|'$INFLUXDB_URL'|g' /etc/telegraf/telegraf.conf
sed -i 's/{{DATABASE_NAME}}/'$DATABASE_NAME'/g' /etc/telegraf/telegraf.conf

# Start telegraf
systemctl start telegraf