#!/bin/bash

##主目录路径
script_dir=$(dirname "$(readlink -f "$0")")
node_dir=$(dirname "$script_dir")
node_logs="${node_dir}/logs/node_exporter.log"
node_parament=`cat ${node_dir}/script/running_paraments 2>/dev/null`
user='exporter'

##判断进程是否存在
ps -ef | grep -w "${node_dir}/node_exporter" | grep -v 'grep' &>/dev/null
if [ $? -eq 0 ];then
  echo "error: Node_exporter server runing"
  exit 10
fi

##判断执行用户
if [ "`whoami`" != "${user}" ];then
  echo "error: !!!excute user not ${user}!!!"
  exit 10
fi

##判断执行文件是否存在
if [ ! -f "${node_dir}/node_exporter" ];then
  echo "error: !!!Node_exporter binary file not exist!!!"
  exit 10
fi

cd ${node_dir}

##执行启动进程的命令
if [ -n "${node_parament}" ];then
  nohup ${node_dir}/node_exporter \
  ${node_parament} >> ${node_logs} 2>&1 &
else
  nohup ${node_dir}/node_exporter >> ${node_logs} 2>&1 &
fi

sleep 2

##启动命令执行后,判断进程是否存在
ps -ef | grep -w "${node_dir}/node_exporter" | grep -v 'grep' &>/dev/null
if [ $? -eq 0 ];then
  echo "success: Node_exporter server start"
else
  echo "error: Node_exporter server start failed"
  exit 10
fi


