Files
Linux/nezha-v1.sh

150 lines
3.9 KiB
Bash

#!/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}" <<EOF
[Unit]
Description=哪吒监控 Agent
After=network.target
ConditionFileIsExecutable=/opt/nezha/agent/nezha-agent
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/opt/nezha/agent/nezha-agent
WorkingDirectory=/opt/nezha/agent
Restart=always
RestartSec=60
EnvironmentFile=-/etc/sysconfig/nezha-agent
Environment="NZ_client_secret=${NZ_client_secret}"
Environment="NZ_server=nezha-v1-grpc.oo1.dev:38443"
Environment="NZ_insecure_tls=true"
Environment="NZ_tls=true"
Environment="NZ_uuid=${NZ_uuid}"
User=nezha
Group=nezha
[Install]
WantedBy=multi-user.target
EOF
# 重新加载 systemd 配置
systemctl daemon-reload
echo "服务配置文件创建成功"
}
uninstall_all() {
systemctl list-units | grep nezha-agent
systemctl stop nezha-agent-7fcd6df.service
systemctl disable nezha-agent-7fcd6df.service
rm /etc/systemd/system/nezha-agent-7fcd6df.service
systemctl daemon-reload
systemctl list-units | grep nezha-agent
}
# 检查 NZ_client_secret 环境变量
if [ -z "${NZ_client_secret}" ]; then
print_red "未设置 NZ_client_secret 环境变量"
exit 1
else
detect_arch
set_uuid
uninstall_nezha_agent
down_nezha_agent
write_service_file
start_nezha_agent
fi
# curl -L https://raw.githubusercontent.com/nezhahq/scripts/main/agent/install.sh -o agent.sh
# chmod +x agent.sh
# env NZ_SERVER=nezha-v1-grpc.oo1.dev:38443 NZ_TLS=true NZ_insecure_tls=true NZ_CLIENT_SECRET=XXX ./agent.sh