#!/bin/bash print_green "###################" print_green "### nezha_agent ###" print_green "###################" declare -r service_file="/etc/systemd/system/nezha-agent.service" NZ_uuid="" uninstall_nezha_agent() { echo "卸载哪吒监控代理" systemctl stop nezha-agent || true systemctl disable nezha-agent || true rm -rf /etc/systemd/system/nezha-agent.service rm -rf /opt/nezha/agent/* } set_uuid() { # 首先尝试从 config.yml 获取 UUID if [ -f "/opt/nezha/agent/config.yml" ]; then config_uuid=$(grep "uuid:" "/opt/nezha/agent/config.yml" | awk '{print $2}') if [ ! -z "$config_uuid" ]; then NZ_uuid=$config_uuid echo "从 config.yml 获取到 UUID: $NZ_uuid" return fi fi # 尝试从服务文件获取 UUID if [ -f "/etc/systemd/system/nezha-agent.service" ]; then existing_uuid=$(grep "NZ_uuid=" "/etc/systemd/system/nezha-agent.service" | cut -d'=' -f2 | tr -d '"') if [ ! -z "$existing_uuid" ]; then NZ_uuid=$existing_uuid echo "从服务文件获取到 UUID: $NZ_uuid" return fi fi # 如果无法从服务文件获取,则生成新的 UUID new_uuid=$(cat /proc/sys/kernel/random/uuid) if [ ! -z "$new_uuid" ]; then NZ_uuid=$new_uuid echo "生成新的 UUID: $NZ_uuid" else echo "无法生成 UUID" exit 1 fi } start_nezha_agent() { echo "启动哪吒监控代理..." if ! id "nezha" &>/dev/null; then useradd -r -s /sbin/nologin nezha fi chown -R nezha:nezha /opt/nezha chmod -R u+rx /opt/nezha/agent/nezha-agent systemctl daemon-reload systemctl enable nezha-agent systemctl start nezha-agent sleep 1 systemctl status nezha-agent } detect_arch() { arch=$(uname -m) case $arch in amd64|x86_64) os_arch="amd64" ;; aarch64|arm64) os_arch="arm64" ;; *) echo "不支持的系统架构: $arch" exit 1 ;; esac echo "检测到系统架构: $os_arch" } down_nezha_agent() { os="linux" GITHUB_URL="gh-cf.oo1.dev/github.com" NZ_AGENT_URL="https://${GITHUB_URL}/nezhahq/agent/releases/latest/download/nezha-agent_${os}_${os_arch}.zip" echo "正在下载哪吒监控代理: $NZ_AGENT_URL" curl -L -o /tmp/nezha-agent.zip $NZ_AGENT_URL mkdir -p /opt/nezha/agent unzip -o /tmp/nezha-agent.zip -d /opt/nezha/agent rm -f /tmp/nezha-agent.zip } write_service_file() { echo "正在写入系统服务配置文件..." cat >"${service_file}" <