#!/bin/bash set -euo pipefail echo "[INFO] Starting Fluent Bit uninstallation..." # 检查是否为 root 用户 if [[ $EUID -ne 0 ]]; then echo "[ERROR] This script requires root privileges" echo "[INFO] Please use: sudo $0" exit 1 fi echo "[WARNING] This operation will completely uninstall Fluent Bit" read -p "Confirm to continue? (y/N): " confirm if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then echo "[INFO] Uninstallation cancelled" exit 0 fi # 停止运行中的进程 echo "[INFO] Stopping Fluent Bit processes..." install_record="/opt/argus-metric/current/.install_record" stopped=false # 首先尝试通过安装记录文件停止服务 if [[ -f "$install_record" ]]; then # 尝试使用jq解析JSON格式的安装记录文件 pid="" if command -v jq &> /dev/null; then pid=$(jq -r '.components."fluent-bit".pid // empty' "$install_record" 2>/dev/null || echo "") else # 如果没有jq,使用简单的文本解析方法 pid=$(grep -A 10 '"fluent-bit"' "$install_record" | grep '"pid"' | cut -d'"' -f4 | head -1) fi if [[ -n "$pid" && "$pid" =~ ^[0-9]+$ ]]; then if kill -0 "$pid" 2>/dev/null; then echo "[INFO] Stopping service via installation record (PID: $pid)..." kill "$pid" sleep 3 # 检查进程是否已停止 if kill -0 "$pid" 2>/dev/null; then echo "[WARNING] Process unresponsive, force killing..." kill -9 "$pid" 2>/dev/null || true fi echo "[SUCCESS] Fluent Bit process stopped" stopped=true else echo "[WARNING] PID in installation record no longer exists" fi fi fi # 查找并杀死所有 fluent-bit 进程 pids=$(pgrep -f "fluent-bit" 2>/dev/null || true) if [[ -n "$pids" ]]; then echo "[INFO] Found fluent-bit processes, stopping..." for pid in $pids; do echo "[INFO] Stopping process PID: $pid" kill "$pid" 2>/dev/null || true done sleep 2 # 检查是否还有进程在运行,如果有则强制终止 remaining_pids=$(pgrep -f "fluent-bit" 2>/dev/null || true) if [[ -n "$remaining_pids" ]]; then echo "[WARNING] Processes unresponsive, force killing..." for pid in $remaining_pids; do echo "[INFO] Force killing process PID: $pid" kill -9 "$pid" 2>/dev/null || true done sleep 1 fi # 最终检查 if pgrep -f "fluent-bit" > /dev/null; then echo "[ERROR] Unable to stop all fluent-bit processes" else echo "[SUCCESS] All Fluent Bit processes stopped" stopped=true fi else echo "[INFO] No Fluent Bit processes running" fi if [[ "$stopped" == "false" ]]; then echo "[WARNING] No Fluent Bit processes found to stop" fi # 卸载 Fluent Bit 包 echo "[INFO] Uninstalling Fluent Bit package..." if dpkg -l | grep -q "fluent-bit"; then echo "[INFO] Found fluent-bit package installed via dpkg, uninstalling..." dpkg --remove --force-remove-reinstreq fluent-bit || true echo "[SUCCESS] Fluent Bit package uninstalled" else echo "[INFO] No fluent-bit package found via package manager" fi # 删除二进制文件 echo "[INFO] Removing Fluent Bit binary files..." binary_dir="/opt/fluent-bit" if [[ -d "$binary_dir" ]]; then rm -rf "$binary_dir" echo "[SUCCESS] Binary directory removed: $binary_dir" else echo "[INFO] Binary directory does not exist" fi # 删除配置文件 echo "[INFO] Removing configuration files..." config_dir="/etc/fluent-bit" if [[ -d "$config_dir" ]]; then rm -rf "$config_dir" echo "[SUCCESS] Configuration directory removed" else echo "[INFO] Configuration directory does not exist" fi # 删除数据目录 echo "[INFO] Removing data directories..." data_dirs=("/logs" "/buffers") deleted=false for data_dir in "${data_dirs[@]}"; do if [[ -d "$data_dir" ]]; then rm -rf "$data_dir" echo "[SUCCESS] Data directory removed: $data_dir" deleted=true fi done if [[ "$deleted" == "false" ]]; then echo "[INFO] No data directories found" fi # 清理安装记录 echo "[INFO] Cleaning up installation record..." if [[ -f "$install_record" ]]; then # 从安装记录中移除 fluent-bit 条目 sed -i '/^fluent-bit:/d' "$install_record" echo "[SUCCESS] Installation record cleaned" else echo "[INFO] Installation record file does not exist" fi # 检查用户状态 echo "[INFO] Checking fluent-bit user status..." if id "fluent-bit" &>/dev/null; then echo "[INFO] fluent-bit user exists" echo "[WARNING] fluent-bit is a system user, may be used by other services" echo "[INFO] fluent-bit user will be preserved for system stability" echo "[INFO] To manually remove, run: sudo userdel fluent-bit" else echo "[INFO] fluent-bit user does not exist" fi echo "[SUCCESS] Fluent Bit uninstallation completed!" echo echo "Removed content:" echo " - Binary directory: /opt/fluent-bit" echo " - Configuration directory: /etc/fluent-bit" echo " - Application log directory: /logs" echo " - Buffer directory: /buffers" echo echo "Note:" echo " - fluent-bit user preserved (system user, may be used by other services)" echo " - For complete cleanup, manually check and remove related files"