node_exporter一键部署脚本

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.node_exporter一键部署脚本

1.脚本内容展示

[root@prometheus-server31 ~]# cat install-node-exporter.sh 
#!/bin/bash
# auther: 尹正杰
# blog: https://www.cnblogs.com/yinzhengjie


VERSION=1.8.2
SOFTWARE=node_exporter-${VERSION}.linux-amd64.tar.gz
URL=https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/${SOFTWARE}
DOWNLOAD=./download
INSTALLDIR=/yinzhengjie/softwares
BASEDIR=${INSTALLDIR}/node_exporter-${VERSION}.linux-amd64
HOST="0.0.0.0"
PORT=9100
hostname=`hostname`


function prepare() {
   # 判断目录是否存在,若不存在则创建
   [ -d $INSTALLDIR ] || mkdir -pv ${INSTALLDIR}
   [ -d $DOWNLOAD ] || mkdir -pv ${DOWNLOAD}
   
   if [ "$ID" == "centos" ];then
     # 判断系统是否安装curl
     [ -f /usr/bin/wget ] || yum -y install wget
   fi

   # 判断文件是否存在,若不存在则下载
   [ -s ${DOWNLOAD}/${SOFTWARE} ] || wget $URL -O ${DOWNLOAD}/${SOFTWARE}
}

function install() {
  # 检查环境
  prepare

  # 解压文件软件包
  tar xf ${DOWNLOAD}/${SOFTWARE} -C ${INSTALLDIR}

  # 生成启动脚本
  cat > /etc/systemd/system/node-exporter.service <<EOF
[Unit]
Description=yinzhengjie Linux Node Exporter
Documentation=https://www.cnblogs.com/yinzhengjie
After=network.target

[Service]
ExecStart=${BASEDIR}/node_exporter --web.telemetry-path="/metrics" \
   --web.listen-address=${HOST}:${PORT}


[Install]
WantedBy=multi-user.target
EOF

     # 将服务设置为开机自启动
     systemctl daemon-reload
     systemctl enable --now node-exporter.service
     systemctl status node-exporter.service
     ss -ntl | grep 9100
}

function remove(){
  systemctl disable --now node-exporter.service
  rm -rf /etc/systemd/system/node-exporter.service $BASEDIR
}


function main() {
  case $1 in 
    install|i)
      install
      echo "尹正杰脚本: ${hostname} 的node-exporter 已经部署成功![successfully]"
      ;;
    remove|r)
      remove
      echo "尹正杰脚本: ${hostname} 的node-exporter 已经卸载成功,期待下次使用~"
      ;;
    *)
      echo "Usage: $0 install[i]|remove[r]"
      ;;
  esac
}


main $1
[root@prometheus-server31 ~]#  

2.访问node_exporter的WebUI

如上图所示,输入node_exporter的地址就可以成功访问啦~

二.测试验证

1.安装node_exporter

[root@prometheus-server31 ~]# ./install-node-exporter.sh i
Created symlink /etc/systemd/system/multi-user.target.wants/node-exporter.service → /etc/systemd/system/node-exporter.service.
● node-exporter.service - yinzhengjie Linux Node Exporter
     Loaded: loaded (/etc/systemd/system/node-exporter.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-11-07 10:10:47 CST; 8ms ago
       Docs: https://www.cnblogs.com/yinzhengjie
   Main PID: 49349 (node_exporter)
      Tasks: 4 (limit: 4514)
     Memory: 1.1M
        CPU: 2ms
     CGroup: /system.slice/node-exporter.service
             └─49349 /yinzhengjie/softwares/node_exporter-1.8.2.linux-amd64/node_exporter --web.telemetry-path=/metrics --web.listen-address=0.0.0.0:9100

Nov 07 10:10:47 prometheus-server31 systemd[1]: Started yinzhengjie Linux Node Exporter.
尹正杰脚本: prometheus-server31 的node-exporter 已经部署成功![successfully]
[root@prometheus-server31 ~]# 

2.卸载node_exporter

[root@prometheus-server31 ~]# ./install-node-exporter.sh r
Removed /etc/systemd/system/multi-user.target.wants/node-exporter.service.
尹正杰脚本: prometheus-server31 的node-exporter 已经卸载成功,期待下次使用~
[root@prometheus-server31 ~]# 

posted @ 2024-09-25 23:43  尹正杰  阅读(42)  评论(0编辑  收藏  举报