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
This commit is contained in:
2026-06-11 20:21:24 -04:00
parent 1a72b3ba50
commit 1c98d7aa35

View File

@@ -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)"