diff --git a/backend/agent/bin/aiops-agent-linux-amd64 b/backend/agent/bin/aiops-agent-linux-amd64 new file mode 100755 index 0000000..26f94fe Binary files /dev/null and b/backend/agent/bin/aiops-agent-linux-amd64 differ diff --git a/backend/agent/bin/aiops-agent-linux-arm64 b/backend/agent/bin/aiops-agent-linux-arm64 new file mode 100755 index 0000000..c8d9efb Binary files /dev/null and b/backend/agent/bin/aiops-agent-linux-arm64 differ diff --git a/backend/agent/deploy/install.sh b/backend/agent/deploy/install.sh new file mode 100755 index 0000000..e74e6b0 --- /dev/null +++ b/backend/agent/deploy/install.sh @@ -0,0 +1,135 @@ +#!/bin/bash +# AIOps Agent 一键安装脚本 — 支持 RHEL/CentOS/Rocky/Alma/Amazon/Aliyun/Debian/Ubuntu 全系列 +# 用法: curl -fsSL http://YOUR_SERVER/agent/install.sh | bash -s -- --server http://YOUR_SERVER:8080 --token TOKEN +# 署名 Logan.Li + +set -e + +# 默认参数 +AIOPS_SERVER="" +AGENT_TOKEN="" +INSTALL_DIR="/usr/local/bin" +SERVICE_NAME="aiops-agent" +ARCH=$(uname -m) +OS=$(cat /etc/os-release 2>/dev/null | grep '^ID=' | cut -d'"' -f2 || echo "unknown") + +# 解析命令行参数 +while [[ $# -gt 0 ]]; do + case $1 in + --server) AIOPS_SERVER="$2"; shift 2;; + --token) AGENT_TOKEN="$2"; shift 2;; + --version) AGENT_VERSION="$2"; shift 2;; + *) echo "未知参数: $1"; exit 1;; + esac +done + +AGENT_VERSION="${AGENT_VERSION:-v1.0.0}" + +echo "" +echo " ╔══════════════════════════════════════════╗" +echo " ║ AIOps Agent 一键安装 ║" +echo " ╚══════════════════════════════════════════╝" +echo "" +echo " 系统: ${OS} (${ARCH})" +echo " 版本: ${AGENT_VERSION}" +echo " 后端: ${AIOPS_SERVER:-未指定}" +echo "" + +# 检查 root 权限 +if [ "$EUID" -ne 0 ]; then + echo "❌ 请使用 root 用户或 sudo 执行此脚本" + exit 1 +fi + +# 确定下载架构 +case "$ARCH" in + x86_64|amd64) DOWNLOAD_ARCH="amd64";; + aarch64|arm64) DOWNLOAD_ARCH="arm64";; + *) + echo "❌ 不支持的 CPU 架构: $ARCH (仅支持 x86_64/amd64 和 aarch64/arm64)" + exit 1 + ;; +esac +echo " 匹配: linux/${DOWNLOAD_ARCH}" +echo "" + +# 安装依赖工具(curl 在大多数发行版自带,这里确保有) +if command -v curl &> /dev/null; then + echo "✓ curl 已安装" +else + echo "→ 安装 curl..." + if command -v yum &> /dev/null; then + yum install -y curl + elif command -v dnf &> /dev/null; then + dnf install -y curl + elif command -v apt-get &> /dev/null; then + apt-get update -qq && apt-get install -y curl + else + echo "❌ 无法确定包管理器,请手动安装 curl" + exit 1 + fi +fi + +# 下载二进制 +if [ -z "$AIOPS_SERVER" ]; then + echo "❌ 请通过 --server 指定 AIOps 后端地址" + exit 1 +fi + +BINARY_URL="${AIOPS_SERVER}/agent/aiops-agent-linux-${DOWNLOAD_ARCH}" +echo "→ 下载 Agent: ${BINARY_URL}" + +curl -fSL -o /tmp/aiops-agent "${BINARY_URL}" || { + echo "❌ 下载失败,请检查后端地址和网络" + exit 1 +} +chmod +x /tmp/aiops-agent +mv /tmp/aiops-agent "${INSTALL_DIR}/aiops-agent" + +echo "✓ 安装到 ${INSTALL_DIR}/aiops-agent" + +# 配置 systemd 服务 +if [ -z "$AGENT_TOKEN" ]; then + echo "⚠ 未提供 --token,使用空令牌" + AGENT_TOKEN="none" +fi + +cat > /etc/systemd/system/${SERVICE_NAME}.service << EOF +[Unit] +Description=AIOps Monitoring Agent +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +ExecStart=${INSTALL_DIR}/aiops-agent -server ${AIOPS_SERVER} -interval 10 -token ${AGENT_TOKEN} +Restart=always +RestartSec=5 +LimitNOFILE=65536 + +[Install] +WantedBy=multi-user.target +EOF + +echo "✓ 创建 systemd 服务" + +# 启动服务 +systemctl daemon-reload +systemctl enable ${SERVICE_NAME} +systemctl restart ${SERVICE_NAME} + +sleep 2 + +if systemctl is-active --quiet ${SERVICE_NAME}; then + echo "" + echo "✅ 安装成功!Agent 已启动并设置为开机自启" + echo "" + echo " 查看状态: systemctl status ${SERVICE_NAME}" + echo " 查看日志: journalctl -u ${SERVICE_NAME} -f" + echo " 卸载: ${0} --uninstall || systemctl stop ${SERVICE_NAME} && rm -f ${INSTALL_DIR}/aiops-agent /etc/systemd/system/${SERVICE_NAME}.service && systemctl daemon-reload" + echo "" +else + echo "❌ Agent 启动失败,请检查日志:" + echo " journalctl -u ${SERVICE_NAME} -n 20" + exit 1 +fi diff --git a/backend/internal/api/routes/routes.go b/backend/internal/api/routes/routes.go index dfde2b7..7dc8465 100644 --- a/backend/internal/api/routes/routes.go +++ b/backend/internal/api/routes/routes.go @@ -20,6 +20,22 @@ func Register(r *gin.Engine, db *gorm.DB, log *zap.Logger, cfg *config.Config) { c.JSON(200, gin.H{"status": "ok"}) }) + // Agent 二进制下载 + 一键安装脚本(无需鉴权,方便 curl|bash) + r.Static("/agent/bin", "./agent/bin") + r.StaticFile("/agent/install.sh", "./agent/deploy/install.sh") + r.StaticFile("/agent/aiops-agent-linux-amd64", "./agent/bin/aiops-agent-linux-amd64") + r.StaticFile("/agent/aiops-agent-linux-arm64", "./agent/bin/aiops-agent-linux-arm64") + r.GET("/agent/arch", func(c *gin.Context) { + c.JSON(200, gin.H{ + "version": "1.0.0", + "downloads": gin.H{ + "amd64": "/agent/aiops-agent-linux-amd64", + "arm64": "/agent/aiops-agent-linux-arm64", + }, + "install": "curl -fsSL http://:8080/agent/install.sh | bash -s -- --server http://:8080 --token ", + }) + }) + // WebSocket终端 (token via query param) webShellHandler := handlers.NewWebShellHandler(db) r.GET("/ws/terminal", webShellHandler.Terminal)