#!/bin/bash
# 
# Usage: resource-exporter [OPTIONS]
# 
# Options:
#       --listen_addr <LISTEN_ADDR>        Application listen address [default: 0.0.0.0:9100]
#       --allow_addr <ALLOW_ADDR>          Application allow address
#       --amqp_user <AMQP_USER>            Rabbitmq user [default: admin]
#       --amqp_passwd <AMQP_PASSWD>        Rabbitmq password
#       --amqp_host <AMQP_HOST>            Rabbitmq host [default: 127.0.0.1]
#       --amqp_port <AMQP_PORT>            Rabbitmq port [default: 5672]
#       --amqp_vhost <AMQP_VHOST>          Rabbitmq virtual host
#       --amqp_retry_max <AMQP_RETRY_MAX>            Rabbitmq connect max retry
#       --amqp_retry_interval <AMQP_RETRY_INTERVAL>  Rabbitmq connect retry interval secs
#       --libvirt_ticker <LIBVIRT_TICKER>  Libvirt ticker [default: 3]
#       --libvirt_driver <LIBVIRT_DRIVER>  Libvirt driver [default: qemu]
#       --libvirt_user <LIBVIRT_USER>      Libvirt remote user [default: root]
#       --libvirt_addr <LIBVIRT_ADDR>      Libvirt remote addr [default: 127.0.0.1]
#       --libvirt_port <LIBVIRT_PORT>      Libvirt remote port [default: 16509]
#       --libvirt_path <LIBVIRT_PATH>      Libvirt remote path [default: system]
#   -h, --help                             Print help

liste_addr="0.0.0.0:9100"
allow_addr="127.0.0.1,192.168.189.253,192.168.188.51,192.168.189.251"
amqp_user="admin"
amqp_passwd="admin"
amqp_host="103.8.215.108"
amqp_port="30008"
amqp_vhost="exporter"
amqp_retry_max=30
amqp_retry_interval=10
libvirt_ticker="30"
libvirt_driver="qemu"
old_proc=$(ps -ef | grep resource-exporter |awk /listen/'{print $2}')
 
# startup resource exporter
if [[ -z $old_proc ]];then
   cd `dirname $0` && ./resource-exporter --listen_addr $liste_addr --allow_addr $allow_addr --amqp_user $amqp_user --amqp_passwd $amqp_passwd --amqp_host $amqp_host --amqp_port $amqp_port --amqp_vhost $amqp_vhost --amqp_retry_max $amqp_retry_max --amqp_retry_interval $amqp_retry_interval --libvirt_ticker $libvirt_ticker --libvirt_driver $libvirt_driver &
else
    echo "Service is running"
fi
