#!/bin/bash
#Verifying Necessary Commands
type curl tar openssl uuidgen wget >/dev/null
shname="unmanagedagent_setup_sample.sh"
vrops_ip="192.168.129.6"
cloudproxy_ip="192.168.129.8"
vrops_user="xuwen"
vrops_password="Passw0rd@123"
Telegraf_dir="/opt/telegraf"
#Download script
wget --no-check-certificate https://${cloudproxy_ip}/downloads/salt/unmanagedagent_setup_sample.sh -O /tmp/${shname} >/dev/null 2>&1
#Grant execution permission
/usr/bin/chmod +x /tmp/${shname}
# Create the certificates needed to establish a secure connection with vRealize Operations
#./unmanagedagent_setup_sample.sh -v <vrops_ip_or_fqdn> -a <vrops_user> -b <vrops_password> -c <cloudproxy_ip> -d <Telegraf_Download_Directory>
/usr/bin/sh /tmp/${shname} -v ${vrops_ip} -a ${vrops_user} -b ${vrops_password} -c ${cloudproxy_ip} -d ${Telegraf_dir}
#Specify a directory for logs
sed -i '/# logfile =/alogfile = "/opt/telegraf/telegraf/var/log/telegraf/telegraf.log"' ${Telegraf_dir}/telegraf/etc/telegraf/telegraf.conf
#Generating a service file
cat >/usr/lib/systemd/system/telegraf.service<<EOF
[Unit]
Description=The plugin-driven server agent for reporting metrics into InfluxDB
Documentation=https://github.com/influxdata/telegraf
After=network.target

[Service]
EnvironmentFile=-/etc/default/telegraf
#User=root
ExecStart=/opt/telegraf/telegraf/usr/bin/telegraf -config /opt/telegraf/telegraf/etc/telegraf/telegraf.conf -config-directory /opt/telegraf/telegraf/etc/telegraf/telegraf.d $TELEGRAF_OPTS
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=control-group

[Install]
WantedBy=multi-user.target
EOF

# Start telegraf.service and set it to start on
systemctl start telegraf.service
systemctl enable telegraf.service >>/dev/null