监控服务部署:

services:
  nezha-panel:
    image: ghcr.io/nezhahq/nezha:v1.14.9   # 或用 :latest
    container_name: nezha-panel
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    # 官方默认面板端口 8008(容器内);不映射到主机,只在容器网络里暴露
    # expose:
    #  - "8008"
    ports:
      - "8008:8008"
    networks:
      - proxy
    healthcheck:
      test: ["CMD", "sh", "-c", "wget -qO- http://127.0.0.1:8008/ || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 5

networks:
  proxy:
    external: true   # 预先:docker network create proxy

Agent部署(被监控机)

查看Agent配置:

sudo cat /opt/nezha/agent/config.yml

# 修改配置
sudo sed -i 's#^server: .*#server: nz.soooooor.cn:8008#' /opt/nezha/agent/config.yml
sudo sed -i 's#^tls: .*#tls: false#' /opt/nezha/agent/config.yml

# 重启Agent
sudo systemctl restart nezha-agent
sudo journalctl -u nezha-agent -n 100 --no-pager

nc -vz nz.soooooor.fans 8008
# 没有nc就安装
sudo apt-get update && sudo apt-get install -y netcat-openbsd

卸载:

# 1. 停掉全部哪吒相关服务
sudo systemctl stop nezha-agent*
# 2. 禁用自启
sudo systemctl disable nezha-agent*
# 3. 删除所有 service 文件
sudo rm -f /etc/systemd/system/nezha-agent*.service
# 4. 删除程序文件与配置
sudo rm -rf /opt/nezha/agent
# 5. 刷新 systemd
sudo systemctl daemon-reload
# 最后运行以下命令,没有输出证明成功
systemctl list-units | grep nezha

NAS部署

管理员权限: sudo -i

编辑监控脚本:vi /share/Public/nezha/start.sh

#!/bin/sh
# Nezha Agent helper for QNAP/Synology - start/stop/restart/status/tail
# Path layout:
#   /share/Public/nezha/agent/nezha-agent
#   /share/Public/nezha/agent/config.yml
#   /share/Public/nezha/logs/agent.log
#   /share/Public/nezha/nezha-agent.pid

set -eu

BASE="/share/Public/nezha"
AGENT_DIR="$BASE/agent"
LOG_DIR="$BASE/logs"
BIN="$AGENT_DIR/nezha-agent"
CFG="$AGENT_DIR/config.yml"
LOG="$LOG_DIR/agent.log"
PID="$BASE/nezha-agent.pid"

# ======= 需要你改的三项 =======
SERVER="10.10.10.188:8008"  # 面板对接地址(域名:端口)
TLS="false"                   # 面板没开TLS就填 false;若走443+TLS改为 true
SECRET="GsBFUDNfB5H55ygE27dsFhkYnNfaN4Pt"  # 面板该NAS对应的 Secret
# ======= 以上三项改好再运行 =======

# 自动识别架构 -> 选择正确压缩包名
detect_asset() {
  ARCH="$(uname -m)"
  case "$ARCH" in
    x86_64)  echo "amd64" ;;
    aarch64) echo "arm64" ;;
    armv7l)  echo "armv7" ;;
    *) echo "不支持的架构: $ARCH"; exit 1 ;;
  esac
}

ensure_agent() {
  mkdir -p "$AGENT_DIR" "$LOG_DIR"
  if [ -x "$BIN" ]; then
    return
  fi
  A="$(detect_asset)"
  ZIP="nezha-agent_linux_${A}.zip"
  URL="https://github.com/nezhahq/agent/releases/latest/download/${ZIP}"
  echo "[INFO] 下载Agent: $URL"
  cd "$AGENT_DIR"
  # 有些固件 unzip 不在 PATH,用 busybox 自带的
  if ! command -v unzip >/dev/null 2>&1; then
    echo "[WARN] 系统缺少 unzip,尝试使用 busybox unzip"
    if ! busybox unzip >/dev/null 2>&1; then
      echo "[ERROR] 找不到 unzip,请安装 unzip 或在PC解压后上传到 $AGENT_DIR"
      exit 2
    fi
    DOWN="curl -fSL --retry 3 -o agent.zip \"$URL\" && busybox unzip -o agent.zip && rm -f agent.zip"
  else
    DOWN="curl -fSL --retry 3 -o agent.zip \"$URL\" && unzip -o agent.zip && rm -f agent.zip"
  fi
  sh -c "$DOWN"
  chmod +x "$BIN"
}

write_config() {
  if [ ! -f "$CFG" ]; then
    cat >"$CFG" <<EOF
server: ${SERVER}
tls: ${TLS}
client_secret: "${SECRET}"
debug: false
EOF
    echo "[INFO] 已生成配置 $CFG"
  fi
}

is_running() {
  [ -f "$PID" ] || return 1
  kill -0 "$(cat "$PID")" 2>/dev/null
}

start_agent() {
  ensure_agent
  write_config
  if is_running; then
    echo "[INFO] nezha-agent 已在运行 (pid $(cat "$PID"))"
    return 0
  fi
  echo "[INFO] 启动 nezha-agent ..."
  "$BIN" -c "$CFG" >>"$LOG" 2>&1 &
  echo $! >"$PID"
  sleep 1
  if is_running; then
    echo "[OK] 已启动,日志: $LOG"
  else
    echo "[ERROR] 启动失败,查看日志: $LOG"
    exit 3
  fi
}

stop_agent() {
  if is_running; then
    kill "$(cat "$PID")" 2>/dev/null || true
    sleep 1
  fi
  rm -f "$PID"
  # 兜底
  killall -q nezha-agent 2>/dev/null || true
  echo "[OK] 已停止"
}

status_agent() {
  if is_running; then
    echo "[RUNNING] pid $(cat "$PID")"
  else
    echo "[STOPPED]"
  fi
}

tail_log() {
  [ -f "$LOG" ] || { echo "日志不存在:$LOG"; exit 0; }
  tail -n 200 -f "$LOG"
}

cmd="${1:-help}"
case "$cmd" in
  start)   start_agent ;;
  stop)    stop_agent ;;
  restart) stop_agent; start_agent ;;
  status)  status_agent ;;
  tail)    tail_log ;;
  help|*)  echo "用法: $0 {start|stop|restart|status|tail}"; exit 0 ;;
esac

授权执行权限:chmod +x /share/Public/nezha/start.sh

/share/Public/nezha/start.sh restart
/share/Public/nezha/start.sh status
/share/Public/nezha/start.sh tail

OpenWRT部署

如果没有opkg的情况:

  • 创建安装路径 mkdir -p /volume1/@entware
  • 进入目录 cd /volume1/@entware
  • 下载对应架构的安装脚本

    • Intel/AMD 架构(x86_64):

      • wget http://bin.entware.net/x64-k3.2/installer/generic.sh -O entware-install.sh
    • ARM64 架构(aarch64):

      • wget http://bin.entware.net/aarch64-k3.10/installer/generic.sh -O entware-install.sh
  • 添加执行权限并安装

    • chmod +x entware-install.sh
    • sh entware-install.sh

安装完后,执行:

echo 'export PATH=/opt/bin:/opt/sbin:$PATH' >> /etc/profile
source /etc/profile

验证:

opkg update

opkg install unzip

unzip -v

最后修改:2025 年 11 月 13 日
如果觉得我的文章对你有用,请随意赞赏