Dashboard showing 0 metrics after agent auto-upgrade due to missing environment variables (SOCAT_BIN, STATS_SOCKET_PATH) when agent restarts in daemon mode.
Implemented lazy initialization in both get_haproxy_stats_csv() and get_server_statuses() functions. These functions now initialize their dependencies on first call, making them completely independent of global variable initialization.
# Pipeline is currently running after git push
# Check status: https://[your-azure-devops]/pipeline
# Wait for deployment to complete (~3-5 minutes)# Check backend logs for successful deployment
kubectl logs -f deployment/backend -n haproxy-manager | head -20
# Expected: New pod started with latest codeOption A: Automatic Script Sync (Recommended)
# Get admin token
TOKEN=$(curl -k -s -X POST "https://haproxy-manager.example.com/api/auth/login" \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin123"}' | jq -r '.access_token')
# Sync scripts from files to database (creates version 1.0.0)
curl -k -X POST "https://haproxy-manager.example.com/api/agents/sync-scripts-from-files" \
-H "Authorization: Bearer $TOKEN" | jq .
# Expected: {"status": "success", "synced": ["linux", "macos"]}Option B: Manual UI Update
- Go to Agent Management page
- Click Settings → Agent Versions
- For Linux platform:
- Click Edit Script
- Version: Keep current or increment (e.g., 1.0.3)
- Changelog: Add "Fixed stats collection after upgrade"
- Click Save (this syncs file content to database)
- Repeat for macOS platform
Option A: UI (Single Agent)
- Go to Agent Management page
- Select agent (e.g.,
demo-agent) - Click Upgrade button
- Wait 30 seconds for agent to restart
Option B: API (Batch Upgrade)
# Get all agents
curl -k -s -X GET "https://haproxy-manager.example.com/api/agents" \
-H "Authorization: Bearer $TOKEN" | jq '.agents[] | {id, name, version}'
# Upgrade specific agent (replace {agent_id})
curl -k -X POST "https://haproxy-manager.example.com/api/agents/{agent_id}/upgrade" \
-H "Authorization: Bearer $TOKEN" | jq .Backend Logs (30 seconds after upgrade):
kubectl logs -f deployment/backend -n haproxy-manager | grep -E "haproxy_stats_csv|demo-agent"
# Expected logs:
# ✅ "Has haproxy_stats_csv: True"
# ✅ "CSV preview: IyBweG..." (base64 data)
# ✅ "STATS: Parsed 15 rows for cluster demo-cluster1"Agent Logs (on agent server):
sudo tail -f /var/log/haproxy-agent/agent.log | grep STATS
# Expected logs:
# ✅ "STATS: Initialized socat: /usr/bin/socat"
# ✅ "STATS: Using default socket path: /var/run/haproxy/admin.sock"
# ✅ "STATS: Fetched CSV: 2121 bytes, 9 lines, base64: 2828 chars"Dashboard UI:
- Open Dashboard page
- Refresh page (F5)
- Check metrics:
- ✅ Frontend/Backend filters populated
- ✅ Overview metrics showing real data (not 0)
- ✅ Charts showing data points
- ✅ "Waiting for Agent Data" warning gone
Cause: Agent hasn't upgraded yet or using old script version
Solution:
# Check agent version on agent server
grep "AGENT_VERSION" /usr/local/bin/haproxy-agent | head -1
# Force agent restart
sudo systemctl restart haproxy-agent
# Check logs immediately
sudo tail -20 /var/log/haproxy-agent/agent.logCause: socat not installed
Solution:
# Install socat
sudo yum install -y socat # RHEL/CentOS
sudo apt install -y socat # Debian/Ubuntu
# Restart agent
sudo systemctl restart haproxy-agentCause: HAProxy stats socket not configured
Solution:
# Check HAProxy config for stats socket
grep "stats socket" /etc/haproxy/haproxy.cfg
# Add if missing (in global section):
# stats socket /var/run/haproxy/admin.sock mode 666 level admin
# Reload HAProxy
sudo systemctl reload haproxy- Pipeline completed successfully
- Backend pod restarted with new code
- Agent scripts synced to database (version visible in UI)
- Agents upgraded to new version
- Backend logs show "Has haproxy_stats_csv: True"
- Agent logs show STATS messages
- Dashboard showing real metrics
- Charts populated with data
- No "Waiting for Agent Data" warning
This fix is permanent. Future agent upgrades will NOT break stats collection because:
- ✅ Functions are self-contained with lazy initialization
- ✅ No dependency on global variable initialization order
- ✅ Works in any restart scenario (systemd, daemon mode, upgrade)
- ✅ Backward compatible with existing agents
No manual intervention needed for future upgrades! 🎉