consul_exporter 安装部署

Prometheus   2025-01-12 07:42   284   0  

1、下载安装包解压

cd /usr/local/prometheus
wget https://github.com/prometheus/consul_exporter/releases/download/v0.8.0/consul_exporter-0.8.0.linux-amd64.tar.gz
tar -zxvf consul_exporter-0.8.0.linux-amd64.tar.gz
mv -f consul_exporter-0.8.0.linux-amd64 consul_exporter

2、启动服务

nohup /usr/local/prometheus/consul_exporter/consul_exporter --log.level=info >/usr/local/prometheus/consul_exporter/consul_exporter.log 2>&1 &

3、开启防火墙端口

systemctl status firewalld
firewall-cmd --zone=public --add-port=9107/tcp --permanent 
firewall-cmd --reload
firewall-cmd --zone=public --query-port=9107/tcp

4、测试验证

浏览器访问

http://IP:9107/

image.png
把consul_exporter注册到consul
注册服务(重启失效)

curl --request PUT --data '{"ID":"consul_exporter","Name":"consul_exporter","Tags":["primary","v1"],"address":"localhost","port":9107,"check":{"http":"http://localhost:9107/metrics","interval":"10s"}}' http://localhost:8500/v1/agent/service/register

删除服务(url最后面为服务ID)

curl --request PUT http://localhost:8500/v1/agent/service/deregister/consul_exporter

image.png

5、Shell 脚本一键安装

touch consul_exporter.sh && chmod +x consul_exporter.sh
#!/bin/bash
#用于安装consul_exporter

#版本号
CONSUL_EXPORTER_VER=0.8.0
#安装位置
CONSUL_EXPORTER_DIR=/app/consul_exporter
#安装包存放路径
INSTALL_PACKAGE=/app/install
#日志文件
LOG_FILE=/tmp/consul_exporter.log

# CONSUL_EXPORTER的用户名
CONSUL_EXPORTER_USER=prometheus
# CONSUL_EXPORTER监听端口
CONSUL_EXPORTER_PORT=9107

check_err()
{
    if [ $? -ne 0 ]; then
        echo "$1"
        exit 1
    else
        echo "$2"
    fi
}

start() {
    [ ! -d /app ] && mkdir -p /app

    #检查是否存在wget命令,没有则安装
    type wget &> /dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
        check_err "\033[31myum安装依赖包 wget 失败,请检查\033[0m" "\033[36myum安装依赖包 wget 成功\033[0m"
    fi

    #检查是否存在netstat命令,没有则安装
    type netstat &> /dev/null
    if [ $? -ne 0 ]; then
        yum install net-tools -y
        check_err "\033[31myum安装依赖包 net-tools 失败,请检查\033[0m" "\033[36myum安装依赖包 net-tools 成功\033[0m"
    fi

    netstat -tln | grep ":$CONSUL_EXPORTER_PORT"
    CHECK_RESULT=$?
    if [ $CHECK_RESULT -eq 0 ];then
      echo "CONSUL_EXPORTER 已启动,请勿重复启动应用!"
      exit 1
    fi
    echo "正在启动 CONSUL_EXPORTER....."

    [ ! -d $INSTALL_PACKAGE ] && mkdir -p $INSTALL_PACKAGE

    [ ! -f $INSTALL_PACKAGE/consul_exporter-${CONSUL_EXPORTER_VER}.linux-amd64.tar.gz ] && wget -O $INSTALL_PACKAGE/consul_exporter-${CONSUL_EXPORTER_VER}.linux-amd64.tar.gz https://github.com/prometheus/consul_exporter/releases/download/v${CONSUL_EXPORTER_VER}/consul_exporter-${CONSUL_EXPORTER_VER}.linux-amd64.tar.gz

    [ ! -d $CONSUL_EXPORTER_DIR ] && mkdir -p $CONSUL_EXPORTER_DIR
    count=`ls $CONSUL_EXPORTER_DIR | wc -l`
    if [ $count -gt 0 ]; then
        echo "目录 $CONSUL_EXPORTER_DIR 不为空,不再重复解压压缩包"
    else
        tar -xvzf $INSTALL_PACKAGE/consul_exporter-${CONSUL_EXPORTER_VER}.linux-amd64.tar.gz --strip-components 1 -C $CONSUL_EXPORTER_DIR
    fi

    cat /etc/group | grep $CONSUL_EXPORTER_USER
    if [ $? -ne 0 ]; then
        groupadd -g 10002 $CONSUL_EXPORTER_USER
    fi
    cat /etc/passwd | grep $CONSUL_EXPORTER_USER
    if [ $? -ne 0 ]; then
        useradd -u 10002 -g $CONSUL_EXPORTER_USER -d /home/$CONSUL_EXPORTER_USER -m $CONSUL_EXPORTER_USER
    fi

    chown -R $CONSUL_EXPORTER_USER:$CONSUL_EXPORTER_USER $CONSUL_EXPORTER_DIR

    [ ! -f $LOG_FILE ] && touch $LOG_FILE
    chown -R $CONSUL_EXPORTER_USER:$CONSUL_EXPORTER_USER $LOG_FILE

    # 切换用户
su - $CONSUL_EXPORTER_USER << EOF
    # 不加反斜杠,取的值是切换用户前的
    nohup $CONSUL_EXPORTER_DIR/consul_exporter --log.level=info >> $LOG_FILE 2>&1 &
    #每2s检查一次监听端口是否启动,总共检查60次共2分钟,超时未启动则退出
    CHECK_RESULT=1
    CHECK_COUNT=60
    while [ \$CHECK_RESULT -ne 0 ];do
        if [ \$CHECK_COUNT -gt 0 ];then
          echo "等待监听端口启动..."
          sleep 2
          netstat -tln | grep ":$CONSUL_EXPORTER_PORT"
          CHECK_RESULT=\$?
          CHECK_COUNT=\$((\$CHECK_COUNT-1))
        else
          echo "ERROR: 启动$CONSUL_EXPORTER_PORT端口超时,请检查!"
          exit 1
        fi
    done
echo "CONSUL_EXPORTER 启动成功!"
EOF
#检查防火墙是否已启动,如果启动则开放端口
systemctl status firewalld > /dev/null
if [ $? -eq 0 ];then
    firewall-cmd --add-port=$CONSUL_EXPORTER_PORT/tcp --permanent
    firewall-cmd --reload
fi
}

stop(){

netstat -tln | grep ":$CONSUL_EXPORTER_PORT"
check_err "CONSUL_EXPORTER 未启动!" "CONSUL_EXPORTER 正在运行,开始停止应用....."

su - $CONSUL_EXPORTER_USER << EOF
PROCESS=`ps aux | grep $CONSUL_EXPORTER_DIR/consul_exporter | grep -v "grep" | awk '{print $2}'`
for i in \${PROCESS}
do
  echo "Kill the CONSUL_EXPORTER process [ \$i ]"
  kill -9 \$i
done
EOF
    CHECK_RESULT=0
    CHECK_COUNT=30
    while [ $CHECK_RESULT -eq 0 ];do
      if [ $CHECK_COUNT -gt 0 ];then
        echo "等待监听端口关闭..."
        sleep 2
        netstat -tln | grep ":$CONSUL_EXPORTER_PORT"
        CHECK_RESULT=$?
        CHECK_COUNT=$(($CHECK_COUNT-1))
      else
        echo "ERROR: 关闭$CONSUL_EXPORTER_PORT端口超时,请检查!"
        exit 1
      fi
    done
    echo "关闭 CONSUL_EXPORTER 成功!"
}

restart(){
    stop
    start
}

status(){
    su $CONSUL_EXPORTER_USER -c "ps aux | grep $CONSUL_EXPORTER_DIR/consul_exporter | grep -v \"grep\" | grep -v \"su $CONSUL_EXPORTER_USER\""
}

remove(){
    rm -rf $LOG_FILE
    rm -rf $CONSUL_EXPORTER_DIR
    userdel -r $CONSUL_EXPORTER_USER
}

usage ()
{
    echo " "
    echo "Please input start to start CONSUL_EXPORTER."
    echo "Please input stop to stop CONSUL_EXPORTER."
    echo "Please input status to get CONSUL_EXPORTER status."
    echo "Please input restart to restart CONSUL_EXPORTER."
    echo "Please input remove to remove CONSUL_EXPORTER."
    echo " "
}

INPUT_ACTIVE=$1
ACTIVE=${INPUT_ACTIVE:=start}
case ${ACTIVE} in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    remove)
        remove
        ;;
    *)
        usage
        ;;
esac


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。