shell安装node-exporter+cadvisor+process-exporter

晋西岭
2023-12-01

安装node-exporter监控系统:sh monitor.sh node-exporter
安装cadvisor监控容器:sh monitor.sh cadvisor
安装process-exporter监控进程(本文件仅监控nginx,按需添加):sh monitor.sh process-exporter

[root@iZ2ze1827514pkm26tdgrxZ ~]# cat monitor.sh
#! /bin/sh
if [ $1 = "node-exporter" ];then
  yum -y install lrzsz
  #node-exporter install
  wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
  #rz -bye
  tar xf node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local/
  mv /usr/local/node_exporter-1.0.1.linux-amd64/ /usr/local/node_exporter
  cd /usr/local/node_exporter/
  node_port=`netstat -lntp | grep 7999`
  if [ ! "$node_port" ];then
    nohup ./node_exporter --web.listen-address=":7999" &
    result_node=`ps -ef | grep node_exporter | grep -v grep`
    if [ ! "$result_node" ];then
      echo "failed to install node-exporter!"
    else
      echo "successfully installed node-exporter"
    fi
  else
    echo "install node-exporter failed,port is already in use!"
  fi
  exit
elif [ $1 = "cadvisor" ];then
  cadvisor_port=`netstat -lntp | grep 7998`
  if [ ! "$cadvisor_port" ];then
    docker run -d -p 7998:8080 --name cadvisor -v /:/rootfs:ro -v /var/run:/var/run:rw -v /sys:/sys:ro -v /var/lib/docker/:/var/lib/docker:ro google/cadvisor:latest
    result_docker=`docker ps | grep cadvisor`
    if [ ! "$result_docker" ];then
      echo "failed to install cadvisor!"
    else
      echo "successfully installed cadvisor"
    fi
  else
    echo "install cadvisor failed,port is already in use!"
  fi
  exit
else
  [ $1 = "process-exporter" ]
  wget  https://github.com/ncabatoff/process-exporter/releases/download/v0.7.5/process-exporter-0.7.5.linux-amd64.tar.gz
  #rz -bye
  tar xf process-exporter-0.7.2.linux-amd64.tar.gz -C /usr/local/
  mv /usr/local/process-exporter-0.7.2.linux-amd64/ /usr/local/process-exporter
  cd /usr/local/process-exporter
  touch pro.yml
  exit
  cat <<EOF >pro.yml
process_names:
  - name: "{{.Matches}}"
    cmdline:
    - '/www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf'
EOF
  process_port=`netstat -lntp | grep 7997`
  if [ ! "$process_port" ];then
    ./process-exporter -config.path process.yml -web.listen-address=:7997 &
    result_process=`ps -ef | grep process-exporter | grep -v grep`
    if [ ! "$result_process" ];then
      echo "failed to install process-exporter!"
    else
      echo "successfully installed process-exporter"
    fi
  else
    echo "install process-exporter failed ,port is already in use!"
  fi
  exit
fi

process.yml需要新建[参考]

(base) [root@xxxx process-exporter]# cat process.yml
process_names:
  - name: "{{.Matches}}"
    cmdline:
    - '/root/anaconda3/envs/mage/bin/python'
  - name: "{{.Matches}}"
    cmdline:
    - '/www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf'
  - name: "{{.Matches}}"
    cmdline:
    - '/usr/bin/docker'

 类似资料: