From 4b8fa1948aa6054931a3a172f8c2690545d9b537 Mon Sep 17 00:00:00 2001 From: Agent Date: Thu, 11 Jun 2026 20:33:11 -0400 Subject: [PATCH] fix: escape runtime vars in unquoted heredoc, eliminate sed dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unquoted EOF heredoc expands all $ at install time. Runtime variables inside functions (CPU, RAM, AGENT_KEY, etc.) must be escaped with $ to prevent premature expansion. Also removed the fragile sed replace step entirely — values are now embedded directly when the heredoc is expanded. --- install/agent-install.sh | 72 ++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/install/agent-install.sh b/install/agent-install.sh index 5a59c55..701d780 100644 --- a/install/agent-install.sh +++ b/install/agent-install.sh @@ -41,70 +41,62 @@ echo "[1/3] Creating directory..." mkdir -p "$(dirname "$AGENT_BIN")" echo "[2/3] Installing agent binary..." -cat > "$AGENT_BIN" << 'AGENTSCRIPT' +cat > "$AGENT_BIN" << EOF #!/bin/bash -SERVER_URL="@SERVER_URL@" -AGENT_KEY="@AGENT_KEY@" -INTERVAL=@INTERVAL@ +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 || true) - [ -z "$CPU" ] && CPU=0 - 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/%//' || true) - [ -z "$DISK" ] && DISK=0 - LOAD=$(cat /proc/loadavg 2>/dev/null | awk '{print $1}' || true) - [ -z "$LOAD" ] && LOAD=0 - UPTIME=$(uptime -p 2>/dev/null | sed 's/^up //' || true) - [ -z "$UPTIME" ] && UPTIME="" + 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}' || true) + [ -z "\$RAM" ] && RAM=0 + 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}' || true) + [ -z "\$LOAD" ] && LOAD=0 + UPTIME=\$(uptime -p 2>/dev/null | sed 's/^up //' || true) + [ -z "\$UPTIME" ] && UPTIME="" } push_metrics() { - payload=$(cat </dev/null) || code=0 - echo "$code" + echo "\$code" } echo "ServerManager Agent started" echo " Server: $SERVER_URL/api/metrics/push" -echo " Interval: ${INTERVAL}s" -echo " Host: $(hostname)" +echo " Interval: \${INTERVAL}s" +echo " Host: \$(hostname)" sleep 2 while true; do collect_metrics - http_code=$(push_metrics) - echo "[$(date)] HTTP $http_code" - if [ "$http_code" = "200" ]; then - true - elif [ "$http_code" = "0" ]; then + http_code=\$(push_metrics) + echo "[\$(date)] HTTP \$http_code" + if [ "\$http_code" = "0" ]; then echo " curl failed — check SERVER_URL=$SERVER_URL" fi - sleep "$INTERVAL" + sleep "\$INTERVAL" done -AGENTSCRIPT - -sed -i \ - "s|@SERVER_URL@|$SERVER_URL|g" \ - -e "s|@AGENT_KEY@|$AGENT_KEY|g" \ - -e "s|@INTERVAL@|$INTERVAL|g" \ - "$AGENT_BIN" +EOF chmod +x "$AGENT_BIN"