#!/bin/bash # Argus Agent 健康检查脚本 # 输出 JSON 格式结果 set -e # 检查 Argus Agent 健康状态 check_health() { local name="argus-agent" local status="unhealth" local reason="" local install_record="/opt/argus-metric/current/.install_record" # 首先尝试通过安装记录文件检查进程 if [[ -f "$install_record" ]]; then # 尝试使用jq解析JSON格式的安装记录文件 local pid="" if command -v jq &> /dev/null; then pid=$(jq -r '.components."argus-agent".pid // empty' "$install_record" 2>/dev/null || echo "") else # 如果没有jq,使用简单的文本解析方法 pid=$(grep -A 10 '"argus-agent"' "$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 # 进程存在且运行正常 status="health" reason="进程运行正常 (PID: $pid)" echo "{\"name\": \"$name\", \"status\": \"$status\", \"reason\": \"$reason\"}" exit 0 else reason="安装记录中的 PID $pid 进程不存在" echo "{\"name\": \"$name\", \"status\": \"$status\", \"reason\": \"$reason\"}" exit 1 fi else reason="安装记录文件中未找到有效的 argus-agent PID" echo "{\"name\": \"$name\", \"status\": \"$status\", \"reason\": \"$reason\"}" exit 1 fi else # 如果安装记录文件不存在,尝试查找 argus-agent 进程 local pids=$(pgrep -f "argus-agent" 2>/dev/null || true) if [[ -n "$pids" ]]; then # 取第一个找到的 PID local pid=$(echo "$pids" | head -1) status="health" reason="发现 argus-agent 进程运行 (PID: $pid),但未找到安装记录" echo "{\"name\": \"$name\", \"status\": \"$status\", \"reason\": \"$reason\"}" exit 0 else reason="未找到 argus-agent 进程,且安装记录文件不存在" echo "{\"name\": \"$name\", \"status\": \"$status\", \"reason\": \"$reason\"}" exit 1 fi fi } # 主函数 main() { check_health } # 脚本入口 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi