Log so-elastic-agent-monitor errors to syslog and stop at Fleet's 10000-result limit

This commit is contained in:
reyesj2 committed 2026-12-03 10:10:39 -06:00
1 parent 4f9981ebf9
commit 7ec2dec143
1 file changed
+10 -6
@@ -20,13 +20,17 @@ CRITICAL_AGENTS_FILE="/opt/so/conf/agents/critical-agents.txt"
OFFLINE_THRESHOLD_HOURS={{ OFFLINE_THRESHOLD_HOURS }}
REALERT_THRESHOLD={{ REALERT_THRESHOLD }}
PAGE_SIZE="{{ PAGE_SIZE }}"
# Safety limit on pagination loops. Very large fleets should raise page_size instead.
# Safety limit on pagination loops; Fleet page-based results are also capped at 10000 agents.
MAX_PAGES=100
log_message() {
local level="$1"
local message="$2"
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ") [$level] $message" >&2
# ERROR also goes to syslog: cron output is not monitored, and syslog is shipped to Elasticsearch.
if [ "$level" = "ERROR" ]; then
logger -t so-elastic-agent-monitor -p user.err -- "ERROR: $message" 2>/dev/null || true
fi
}
matches_critical_pattern() {
@@ -150,6 +154,11 @@ main() {
{%- endif %}
while true; do
if (( page * PAGE_SIZE > 10000 )); then
log_message "ERROR" "Fleet page-based results are limited to 10000 agents (page $page x page_size $PAGE_SIZE); $processed_agents of $total_agents agents were checked. Narrow the agents being monitored with custom_kquery under Administration -> Configuration -> manager -> agent_monitoring -> config (enable 'Show advanced settings')."
exit 1
fi
log_message "INFO" "Fetching page $page (${PAGE_SIZE} agents per page)"
if ! response_body=$(curl -K "$CURL_CONFIG" \
@@ -276,11 +285,6 @@ main() {
fi
done
if [ "$total_agents" -gt 0 ] && [ "$processed_agents" -eq 0 ]; then
log_message "ERROR" "Incomplete agent scan: Fleet reported $total_agents agents but none were examined"
exit 1
fi
log_message "INFO" "Fleet agent status check completed. Processed $processed_agents out of $total_agents agents"
}