#!/bin/bash

script_dir=$(dirname "$(readlink -f "$0")")
alert_dir=$(dirname "$script_dir")
user='alertmanager'
log_dir="logs/alertmanager.log"

ps -ef | grep -w "${alert_dir}/alertmanager" | grep -v 'grep' &>/dev/null
if [ $? -eq 0 ];then
  echo "error: !!!Alertmanager server runing!!!"
  exit 10
fi


if [ "`whoami`" != "${user}" ];then
  echo "error: !!!Excute user not ${user}!!!"
  exit 10
fi

if [ ! -f "${alert_dir}/alertmanager" ];then
  echo "error: !!!Alertmanager binary file not exist!!!"
  exit 10
fi

cd ${alert_dir}

start_para=`cat ${alert_dir}/script/running_paraments`

nohup ${alert_dir}/alertmanager \
--config.file=config/alertmanager.yml \
${start_para} >> ${log_dir} 2>&1 &

sleep 2

ps -ef | grep -w "${alert_dir}/alertmanager" | grep -v 'grep' &>/dev/null
if [ $? -eq 0 ];then
  echo "success: Alertmanager server start"
else
  echo "error: Alertmanager server start failed"
  exit 10
fi
