#!/bin/bash

##主目录路径
script_dir=$(dirname "$(readlink -f "$0")")
node_dir=$(dirname "$script_dir")
user='exporter'

##判断进程是否存在
pid=`ps -ef | grep -w "${node_dir}/node_exporter" | grep -v 'grep' | awk '{print $2}'`
if [ ! -n "$pid" ];then
  echo "error: Node_exporter server not runing"
  exit 10
fi

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

##结束进程
kill $pid

sleep 2

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