#!/bin/bash
BASE_PATH=/usr/local/cloudmonitor
SERVICE_NAME=cloudmonitor
SERVICE_FILE=${BASE_PATH}/${SERVICE_NAME}.usr
SYSTEMD_SERVICE_FILE=${BASE_PATH}/${SERVICE_NAME}.service.usr
SYSTEMD_SERVICE_NAME=${SERVICE_NAME}.service

if [[ ! -z "`egrep -i coreos /etc/os-release`" ]];then
	BASE_PATH="/opt/cloudmonitor"
	SERVICE_FILE=${BASE_PATH}/${SERVICE_NAME}.opt
  SYSTEMD_SERVICE_FILE=${BASE_PATH}/${SERVICE_NAME}.service.opt
fi
ARGUSAGENT_CMD_LINE=$BASE_PATH/bin/argusagent
ARGUSAGENTD_CMD_LINE="$ARGUSAGENT_CMD_LINE -d"
ARGUSAGENT_D_PID_FILE=/var/run/argusagentd-cloudmonitor.pid 
ARGUSAGENT_PID_FILE=/var/run/argusagent-cloudmonitor.pid 
if [[ `uname -m` = "x86_64" ]]; then
    ARCH="amd64"
    AEGIS_ARCH="linux64"
else
    ARCH="386"
    AEGIS_ARCH="linux32"
fi
case `uname -s` in
  Linux)
    CMS_OS="linux"
    ;;
  *)
    echo "Unsupported OS: $(uname -s)"
    exit 1
    ;;
esac
stop_cms_agent()
{
	CMS_GO_AGENT_CMDLINE=$BASE_PATH/CmsGoAgent.$CMS_OS-$ARCH
	if [ -f $CMS_GO_AGENT_CMDLINE ];then 
		$CMS_GO_AGENT_CMDLINE stop
	fi
	CMS_GO_AGENT_PID=`ps aux|grep "$CMS_GO_AGENT_CMDLINE"|grep -v grep |awk '{print $2}'`
	if [ -n "$CMS_GO_AGENT_PID" ];then
		kill -9 $CMS_GO_AGENT_PID
	fi
    CMS_JAVA_AGENT_CMDLINE=$BASE_PATH/wrapper/bin/cloudmonitor.sh
	if [ -f  $CMS_JAVA_AGENT_CMDLINE ];then
		$CMS_JAVA_AGENT_CMDLINE remove
	fi
}
start()
{
	stop_cms_agent
	$ARGUSAGENTD_CMD_LINE  1>/dev/null 2>/dev/null
	sleep 2
	if [ -f $ARGUSAGENT_D_PID_FILE ];then 
		argusagentdPid=`cat $ARGUSAGENT_D_PID_FILE`
		check_process $argusagentdPid
		if [ ! $COUNT -eq 0 ];then 
			echo "argusagent start success!"
		else 
			echo "failed to start argusagent!"
		fi
	else
		echo "failed to start argusagent!"
	fi 
}
stop()
{
	#stop argusagentd
	if [ -f $ARGUSAGENT_D_PID_FILE ];then
		argusagentdPid=`cat $ARGUSAGENT_D_PID_FILE`
	else 
		argusagentdPid=`ps aux |grep "$ARGUSAGENTD_CMD_LINE"|grep -v grep|awk '{print $2}'`
	fi
	check_process $argusagentdPid
	if [ ! $COUNT -eq 0 ];then 
		kill -3 $argusagentdPid
		sleep 2
		check_process $argusagentdPid
		if [ ! $COUNT -eq 0 ];then 
			kill -9 $argusagentdPid
			sleep 2
		fi 
	fi 

	if [ -f $ARGUSAGENT_PID_FILE ];then 
		argusagentPid=`cat $ARGUSAGENT_PID_FILE`
	else 
		argusagentPid=`ps aux |grep "$ARGUSAGENT_CMD_LINE"|grep -v  "$ARGUSAGENTD_CMD_LINE"|grep -v "grep"|awk '{print $2}'`
	fi 
	check_process $argusagentPid
	if [ ! $COUNT -eq 0 ];then 
		kill -3 $argusagentPid
		sleep 2
		check_process $argusagentPid
		if [ ! $COUNT -eq 0 ];then 
			kill -9 $argusagentPid
			sleep 2
		fi 
	fi 
	if [ -f $ARGUSAGENT_D_PID_FILE ];then
		rm $ARGUSAGENT_D_PID_FILE
	fi 
	if [ -f $ARGUSAGENT_PID_FILE ];then
		rm $ARGUSAGENT_PID_FILE
	fi
}

check_process()
{
	COUNT=0
	if [ -n "$1" ];then
		if [ -d /proc/$1 ];then 
			cmdline=`readlink /proc/$1/exe`
			if [[ $cmdline =~ $ARGUSAGENT_CMD_LINE ]];then
			let COUNT+=1
		fi
		fi 
	fi 
}

restart()
{
	stop
	start
}
install()
{
if [ -f /bin/systemctl ];then
  #new os
  SYSTEMD_PATH=/usr/lib/systemd/system
  if [ -d /etc/systemd/system ];then
    SYSTEMD_PATH=/etc/systemd/system
  fi
  if [ ! -d ${SYSTEMD_PATH} ];then
    mkdir -p ${SYSTEMD_PATH}
  fi 
  cp -f ${SYSTEMD_SERVICE_FILE} ${SYSTEMD_PATH}/${SYSTEMD_SERVICE_NAME}
  systemctl enable ${SYSTEMD_SERVICE_NAME}
  systemctl daemon-reload 
elif [ -f /usr/sbin/update-rc.d ];then
    #old ubuntu
    cp -f ${SERVICE_FILE} /etc/init.d/${SERVICE_NAME}
    chmod +x /etc/init.d/${SERVICE_NAME}
    update-rc.d -f ${SERVICE_NAME} start 20 2 3 4 5 . stop 20 0 1 6 .
elif [ -f /sbin/chkconfig ];then
    #old centos
    cp -f ${SERVICE_FILE} /etc/init.d/${SERVICE_NAME}
    chmod +x /etc/init.d/${SERVICE_NAME}
    chkconfig --add ${SERVICE_NAME}
    chkconfig --level 2345 ${SERVICE_NAME} on
else
    echo "unknow os!"
fi
}
uninstall()
{
   if [ -f /bin/systemctl ];then
      systemctl disable  ${SYSTEMD_SERVICE_NAME}
   elif [ -f /usr/sbin/update-rc.d -a -f /etc/init.d/${SERVICE_NAME} ];then
    #old ubuntu
    update-rc.d -f ${SERVICE_NAME} remove
  elif [ -f /sbin/chkconfig -a -f /etc/init.d/${SERVICE_NAME} ];then
    #old centos
    chkconfig --del ${SERVICE_NAME}
  else
      echo "unknow os!"
   fi 
}
remove()
{
	stop
	uninstall
}

case "$1" in
	start)
	start
	;;
	stop)
	stop
	;;
	restart)
	restart
	;;
	install)
	install
	;;
	uninstall)
	uninstall
	;;
	remove)
	remove
	;;
	*)
	echo $"Usage: $0 {start|stop|restart|install|uninstall|remove}"
	CODE=1
	;;
esac
exit $CODE
