Public IP Server

# vim /data/docker/monitor/docker-compose.yaml
-----
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - "9090:9090"
    volumes:
      - prometheus-storage:/prometheus
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--web.enable-admin-api'
    restart: unless-stopped
 
  pushgateway:
    image: prom/pushgateway:latest
    container_name: pushgateway
    ports:
      - "9091:9091"
    command:
      # - '--web.enable-admin-api'
    restart: unless-stopped
 
  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    volumes:
      - ./grafana.ini:/etc/grafana/grafana.ini
      - grafana-storage:/var/lib/grafana
    restart: unless-stopped
 
volumes:
  grafana-storage:
  prometheus-storage:
-----

Tip

You should change your-grafana-server.com to yourselfs ↓

# vim /data/docker/monitor/grafana.ini
-----
[server]
domain = your-grafana-server.com
-----
# vim /data/docker/monitor/prometheus.yml
-----
global:
  scrape_interval: 5s
 
scrape_configs:
  - job_name: 'pushgateway'
    honor_labels: true
    static_configs:
      - targets: ['pushgateway:9091']
 
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-1.com:9100']
        labels:
          instance: 'node-1'
      - targets: ['node-2.com:9100']
        labels:
          instance: 'node-2'
      - targets: ['node-3.com:9100']
        labels:
          instance: 'node-3'
-----

Monitored Node

Binary Installation

Tip

You should change your-pushgateway-server.com to yourselfs ↓

# node-exporter.sh
-----
#!/bin/bash
 
# 参数检查
if [ -z "$1" ]; then
  echo "错误:请提供设备名称作为参数。"
  echo "用法: $0 <设备名称>"
  exit 1
fi
 
# 设置变量
INSTANCE_NAME=$1
NODE_EXPORTER_VERSION="1.8.2"
NODE_EXPORTER_URL="http://127.0.0.1:9100/metrics"
PUSHGATEWAY_URL="http://your-pushgateway-server.com:9091/metrics/job/node-exporter/instance/${INSTANCE_NAME}"
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
INSTALL_DIR="/usr/bin"
SERVICE_NAME="node-exporter"
 
# 检查设备是否具有公网 IP
read -p "该设备是否具有公网 IP?(y/n): " HAS_PUBLIC_IP
 
# 下载并安装 Node Exporter
echo "正在下载 Node Exporter..."
wget -O /tmp/node_exporter.tar.gz $DOWNLOAD_URL
if [ $? -ne 0 ]; then
  echo "错误:下载 Node Exporter 失败。"
  exit 1
fi
 
echo "正在解压 Node Exporter..."
tar -xzf /tmp/node_exporter.tar.gz -C /tmp
if [ $? -ne 0 ]; then
  echo "错误:解压 Node Exporter 失败。"
  exit 1
fi
 
echo "正在安装 Node Exporter..."
mv /tmp/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter $INSTALL_DIR/
if [ $? -ne 0 ]; then
  echo "错误:移动 Node Exporter 到安装目录失败。"
  exit 1
fi
 
# 清理临时文件
rm -rf /tmp/node_exporter.tar.gz /tmp/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64
 
# 创建 systemd 服务
echo "正在创建 systemd 服务..."
cat <<EOF | tee /etc/systemd/system/${SERVICE_NAME}.service > /dev/null
[Unit]
Description=Node Exporter
After=network.target
 
[Service]
User=root
ExecStart=${INSTALL_DIR}/node_exporter
Restart=always
 
[Install]
WantedBy=multi-user.target
EOF
 
# 启动并启用服务
echo "正在启动 Node Exporter 服务..."
systemctl daemon-reload
systemctl start ${SERVICE_NAME}
systemctl enable ${SERVICE_NAME}
 
if [ $? -eq 0 ]; then
  echo "Node Exporter 服务已启动并启用。"
else
  echo "错误:启动 Node Exporter 服务失败。"
  exit 1
fi
 
# 对于 NAT 下的设备,设置推送指标到 Pushgateway 的任务
if [[ "$HAS_PUBLIC_IP" =~ ^[nN]$ ]]; then
  echo "该设备处于 NAT 下,正在设置推送指标到 Pushgateway 的任务..."
 
  # 创建推送脚本
  PUSH_SCRIPT="${INSTALL_DIR}/push-node-exporter-metrics.sh"
  cat <<EOF | tee $PUSH_SCRIPT > /dev/null
#!/bin/bash
while true; do
  curl --data-binary @<(curl -s $NODE_EXPORTER_URL) $PUSHGATEWAY_URL
  sleep 5
done
EOF
 
  # 赋予脚本执行权限
  chmod +x $PUSH_SCRIPT
 
  # 创建 systemd 服务管理推送任务
  PUSH_SERVICE_NAME="push-node-exporter-metrics"
  cat <<EOF | tee /etc/systemd/system/${PUSH_SERVICE_NAME}.service > /dev/null
[Unit]
Description=Push Node Exporter Metrics to Pushgateway
After=network.target
 
[Service]
User=root
ExecStart=${PUSH_SCRIPT}
Restart=always
 
[Install]
WantedBy=multi-user.target
EOF
 
  # 启动并启用推送服务
  systemctl daemon-reload
  systemctl start ${PUSH_SERVICE_NAME}
  systemctl enable ${PUSH_SERVICE_NAME}
 
  if [ $? -eq 0 ]; then
    echo "推送指标到 Pushgateway 的任务已启动并启用。"
  else
    echo "错误:启动推送任务失败。"
    exit 1
  fi
fi
 
echo "安装完成!"
-----

Docker-Compose Installation

Tip

You should change node-4 & your-pushgateway-server.com to yourselfs ↓

# vim docker-compose.yml
-----
version: '3.7'
 
services:
  node-exporter:
    image: prom/node-exporter:latest
    container_name: node-exporter
    hostname: node-4
    ports:
      - "9100:9100"
    restart: unless-stopped
 
  push-script:
    image: curlimages/curl:latest
    container_name: push-script
    entrypoint: >
      /bin/sh -c "while true; do
        curl -s --data-binary @<(curl -s http://node-exporter:9100/metrics) http://your-pushgateway-server.com:9091/metrics/job/node-exporter/instance/node-4;
        sleep 5;
      done"
    restart: unless-stopped
-----