From cc0a0c06449d7a67187238f3ede4f444d18f0b6f Mon Sep 17 00:00:00 2001 From: Agent Date: Thu, 11 Jun 2026 20:21:24 -0400 Subject: [PATCH] fix: remove set -e from agent, guard all commands with || true - Removed 'set -e' so a failing metric collection command doesn't kill the agent process - Added '|| true' to every command in collect_metrics() to prevent pipeline failures - Fixed push_metrics() to capture curl exit code with 'local code; code=$(...) || code=0' - The agent now keeps running even if individual metrics or push fail --- install/agent-install.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/install/agent-install.sh b/install/agent-install.sh index 19448bc..2c8c023 100644 --- a/install/agent-install.sh +++ b/install/agent-install.sh @@ -43,22 +43,21 @@ mkdir -p "$(dirname "$AGENT_BIN")" echo "[2/3] Installing agent binary..." cat > "$AGENT_BIN" << 'AGENTSCRIPT' #!/bin/bash -set -e SERVER_URL="@SERVER_URL@" AGENT_KEY="@AGENT_KEY@" INTERVAL=@INTERVAL@ collect_metrics() { - CPU=$(top -bn1 2>/dev/null | grep 'Cpu(s)' | awk '{print $2}' | cut -d'%' -f1) + CPU=$(top -bn1 2>/dev/null | grep 'Cpu(s)' | awk '{print $2}' | cut -d'%' -f1 || true) [ -z "$CPU" ] && CPU=0 - RAM=$(free 2>/dev/null | grep Mem | awk '{printf "%.1f", $3/$2 * 100}') + RAM=$(free 2>/dev/null | grep Mem | awk '{printf "%.1f", $3/$2 * 100}' || true) [ -z "$RAM" ] && RAM=0 - DISK=$(df / 2>/dev/null | tail -1 | awk '{print $5}' | sed 's/%//') + DISK=$(df / 2>/dev/null | tail -1 | awk '{print $5}' | sed 's/%//' || true) [ -z "$DISK" ] && DISK=0 - LOAD=$(cat /proc/loadavg 2>/dev/null | awk '{print $1}') + LOAD=$(cat /proc/loadavg 2>/dev/null | awk '{print $1}' || true) [ -z "$LOAD" ] && LOAD=0 - UPTIME=$(uptime -p 2>/dev/null | sed 's/^up //') + UPTIME=$(uptime -p 2>/dev/null | sed 's/^up //' || true) [ -z "$UPTIME" ] && UPTIME="" } @@ -74,11 +73,13 @@ push_metrics() { } EOF ) - curl -s -X POST "$SERVER_URL/api/metrics/push" \ + local code + code=$(curl -s -X POST "$SERVER_URL/api/metrics/push" \ -H "Content-Type: application/json" \ -d "$payload" \ --connect-timeout 10 --max-time 30 \ - -o /dev/null -w "%{http_code}" 2>/dev/null || echo "000" + -o /dev/null -w "%{http_code}" 2>/dev/null) || code=0 + echo "$code" } echo "ServerManager Agent started (interval: ${INTERVAL}s)"