masterHost=""
zkHost=""

TSDB_CONF="/etc/opentsdb/opentsdb.conf"
PIDFILE="/etc/opentsdb/tsdb.pid"

OPENTSDB_RPM="opentsdb-2.3.0.rpm"
MRS_VERSION=`cat /opt/VERSION`
regex="^MRS [2-9](\.[0-9]){2}"

if [[ "$MRS_VERSION" =~ $regex ]];then
  OPENTSDB_RPM="opentsdb-2.4.0.noarch.rpm"
else
  OPENTSDB_RPM="opentsdb-2.3.0.rpm"
fi

function log()
{
	echo $1
}

function prepareHBaseScript()
{
	echo "exec hbase shell <<EOF" > /tmp/hbase_create_tsdb.sh
	echo "create 'tsdb-uid',{NAME => 'id', COMPRESSION => 'NONE', BLOOMFILTER => 'ROW'},{NAME => 'name', COMPRESSION => 'NONE', BLOOMFILTER => 'ROW'}" >> /tmp/hbase_create_tsdb.sh
	echo "create 'tsdb', {NAME => 't', VERSIONS => 1, COMPRESSION => 'NONE', BLOOMFILTER => 'ROW'}" >> /tmp/hbase_create_tsdb.sh
	echo "create 'tsdb-tree', {NAME => 't', VERSIONS => 1, COMPRESSION => 'NONE', BLOOMFILTER => 'ROW'}" >> /tmp/hbase_create_tsdb.sh
	echo "create 'tsdb-meta', {NAME => 'name', COMPRESSION => 'NONE', BLOOMFILTER => 'ROW'}" >> /tmp/hbase_create_tsdb.sh
	echo "EOF" >> /tmp/hbase_create_tsdb.sh
}

# create tsdb tables in hbase
# 1. check is security mode or not
# 2. kinit with hbase.keytab if needed
# 3. use hbase shell to create tables
# note: this function execute on master node.
function createTsdb() 
{
	source /opt/client/bigdata_env
	mode=`grep -1 "<name>fi.mode</name>" /opt/executor/webapps/executor/WEB-INF/classes/servicebroker.xml | grep "value"`
	if [ $mode == "<value>1</value>" ];then
		principal=`klist -k /opt/Bigdata/MRS/etc/*_HMaster/hbase.keytab | grep "hbase/hadoop" | awk 'NR==1 {print $2}'`
		kinit -kt /opt/Bigdata/MRS/etc/*_HMaster/hbase.keytab $principal
	fi
	prepareHBaseScript
	sh /tmp/hbase_create_tsdb.sh
}

function getZkHost()
{
	zkHost="$masterHost:2181"
	master2=`cat /etc/hosts | grep master2 | awk '{print $2}'`
	if [ "X$master2" != "X" ];then
		zkHost="$zkHost,$master2:2181"
	fi
}

function installTsdb()
{
	log "start to install gnuplot"
	yum -y install gnuplot
	log "install gnuplot finished."
	
	wget --no-check-certificate https://mrs-samples-bootstrap-ap-southeast-2.obs.ap-southeast-2.myhwclouds.com/opentsdb/$OPENTSDB_RPM -O /tmp/$OPENTSDB_RPM
	log "wget opentsdb rpm finished, start to install rpm."
	rpm -i /tmp/$OPENTSDB_RPM
	log "install rpm finished."
	
	getZkHost
	log "zkHost is $zkHost"
	sed -i "s/#tsd.storage.hbase.zk_quorum = localhost/tsd.storage.hbase.zk_quorum = $zkHost/g" $TSDB_CONF
	sed -i "s/#tsd.core.auto_create_metrics = false/tsd.core.auto_create_metrics = true/g" $TSDB_CONF
	log "sed zkHost ok."
	su - omm -c "scp $masterHost:/opt/executor/webapps/executor/WEB-INF/classes/servicebroker.xml /tmp/"
	mode=`grep -1 "<name>fi.mode</name>" /tmp/servicebroker.xml | grep "value"`
	if [ $mode == "<value>1</value>" ];then
		log "current mode is sec mode."
		su - omm -c "scp $masterHost:/opt/Bigdata/MRS/etc/*_HMaster/hbase.keytab /tmp/"
		principalStr=`su - omm -c "klist -k /tmp/hbase.keytab | grep 'hbase/hadoop' | awk 'NR==1{print $2}'"`
		principal=`echo $principalStr | awk '{print $2}'`
		log "principal is $principal"
		
		mkdir -p /etc/opentsdb/conf/
		mv /tmp/hbase.keytab /etc/opentsdb/conf/
		
		echo "
Client { 
com.sun.security.auth.module.Krb5LoginModule required 
useKeyTab=true 
keyTab=\"/etc/opentsdb/conf/hbase.keytab\" 
principal=\""$principal"\" 
useTicketCache=false 
storeKey=true 
debug=true; 
};" > /etc/opentsdb/conf/jaas.conf
		
		echo "" >> $TSDB_CONF
		echo "hbase.security.auth.enable = true" >> $TSDB_CONF
		echo "hbase.security.authentication = kerberos" >> $TSDB_CONF
		echo "hbase.sasl.clientconfig = Client" >> $TSDB_CONF
		echo "hbase.kerberos.regionserver.principal = $principal" >> $TSDB_CONF
		
		log "config opentsdb finished."
		
		# delete old zk jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*.jar 
		cp /opt/share/zookeeper-3.5.1-mrs-*/zookeeper-3.5.1-mrs-*.jar /usr/share/opentsdb/lib/
		rm -rf /usr/share/opentsdb/lib/zookeeper*fatjar.jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*loggraph.jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*recipes-election.jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*recipes-lock.jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*recipes-queue.jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*tests.jar
		rm -rf /usr/share/opentsdb/lib/zookeeper*ZooInspector.jar
		log "replace zk jars finished."
		echo "yes" > /etc/opentsdb/conf/security_mode
		export JVMARGS=-Djava.security.auth.login.config=/etc/opentsdb/conf/jaas.conf 
	fi
	
	source /opt/client/bigdata_env
	mkdir -p /var/log/opentsdb/
	
	if [ -f $PIDFILE ];then
		pid=`cat $PIDFILE`
		if [ -n "$pid" ];then
			log "pid file content is $pid"
			curpid=`ps -efww | grep $pid | grep "TSDMain" | grep -v "grep"`
			if [ -n "$curpid" ];then
				log "tsdb process exists, kill it firstly."
				kill -9 $pid
			fi
		fi
	fi
	
	log "opentsdb going to start now."
	nohup tsdb tsd --config=/etc/opentsdb/opentsdb.conf >> /var/log/opentsdb/opentsdb.out 2>&1 & echo $! > $PIDFILE
	log "opentsdb start success, add healthcheck now."
	wget --no-check-certificate https://mrs-samples-bootstrap-ap-southeast-2.obs.ap-southeast-2.myhwclouds.com/opentsdb/opentsdb-healthcheck.sh -O /etc/opentsdb/opentsdb-healthcheck.sh
	check_command="/etc/opentsdb/opentsdb-healthcheck.sh"
	cronfile="/var/spool/cron/root"
	grep "opentsdb-healthcheck.sh" $cronfile >>/dev/null 2>&1
	if [ $? -ne 0 ];then
		echo "*/1 * * * * sh $check_command > /dev/null 2>&1" >> $cronfile
	fi
}

function main()
{
	log "start to install opentsdb."
	mode=$1
	
	bin=`dirname "$0"`
	bin=`cd "$bin"; pwd`
	
	script="opentsdb_install.sh"
	log "current script is $script"
	cp $bin/$script /tmp/create_tsdb_sh.sh
	chown omm:wheel /tmp/create_tsdb_sh.sh

	if [ "X$mode" == "Xmaster" ];then
		log "exectute on master node."
		createTsdb
	else
		log "execute on current node."
		masterHost=`cat /etc/hosts | grep master1 | awk '{print $2}'`
		log "master node is $masterHost"
		su - omm -c "scp /tmp/create_tsdb_sh.sh $masterHost:/tmp/create_tsdb_sh.sh"
		su - omm -c "ssh $masterHost 'sh /tmp/create_tsdb_sh.sh master'"
		log "finished execute on master node."
		
		# setup opentsdb
		installTsdb
	fi
	log "opentsdb install done."
}

main $@