From b41f8f20a6a47dc7666688eaf8f2b58416249d2c Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 13 Sep 2022 09:27:16 -0400 Subject: [PATCH] [Fleet] Change agent status order offline before updating --- .../fleet/common/services/agent_status.ts | 71 +++++++++++++------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/x-pack/plugins/fleet/common/services/agent_status.ts b/x-pack/plugins/fleet/common/services/agent_status.ts index 7d7b11f5d8a5d..a1a4fe3821e4c 100644 --- a/x-pack/plugins/fleet/common/services/agent_status.ts +++ b/x-pack/plugins/fleet/common/services/agent_status.ts @@ -16,9 +16,7 @@ export function getAgentStatus(agent: Agent | FleetServerAgent): AgentStatus { if (!agent.active) { return 'inactive'; } - if (agent.unenrollment_started_at && !agent.unenrolled_at) { - return 'unenrolling'; - } + if (!agent.last_checkin) { return 'enrolling'; } @@ -27,6 +25,14 @@ export function getAgentStatus(agent: Agent | FleetServerAgent): AgentStatus { const msSinceLastCheckIn = new Date().getTime() - msLastCheckIn; const intervalsSinceLastCheckIn = Math.floor(msSinceLastCheckIn / AGENT_POLLING_THRESHOLD_MS); + if (intervalsSinceLastCheckIn >= offlineTimeoutIntervalCount) { + return 'offline'; + } + + if (agent.unenrollment_started_at && !agent.unenrolled_at) { + return 'unenrolling'; + } + if (agent.last_checkin_status === 'error') { return 'error'; } @@ -44,49 +50,74 @@ export function getAgentStatus(agent: Agent | FleetServerAgent): AgentStatus { if (!policyRevision || (agent.upgrade_started_at && agent.upgrade_status !== 'completed')) { return 'updating'; } - if (intervalsSinceLastCheckIn >= offlineTimeoutIntervalCount) { - return 'offline'; - } return 'online'; } -export function buildKueryForEnrollingAgents(path: string = '') { +export function buildKueryForEnrollingAgents(path: string = ''): string { return `not (${path}last_checkin:*)`; } -export function buildKueryForUnenrollingAgents(path: string = '') { +export function buildKueryForUnenrollingAgents(path: string = ''): string { return `${path}unenrollment_started_at:*`; } -export function buildKueryForOnlineAgents(path: string = '') { - return `not (${buildKueryForOfflineAgents(path)}) AND not (${buildKueryForErrorAgents( +export function buildKueryForOnlineAgents(path: string = ''): string { + return `not (${buildKueryForOfflineAgents(path)}) ${addExclusiveKueryFilter( + buildKueryForOnlineAgents, path - )}) AND not (${buildKueryForUpdatingAgents(path)})`; + )}`; } -export function buildKueryForErrorAgents(path: string = '') { - return `(${path}last_checkin_status:error or ${path}last_checkin_status:degraded) AND not (${buildKueryForUpdatingAgents( +export function buildKueryForErrorAgents(path: string = ''): string { + return `(${path}last_checkin_status:error or ${path}last_checkin_status:degraded) ${addExclusiveKueryFilter( + buildKueryForErrorAgents, path - )})`; + )}`; } -export function buildKueryForOfflineAgents(path: string = '') { +export function buildKueryForOfflineAgents(path: string = ''): string { return `${path}last_checkin < now-${ (offlineTimeoutIntervalCount * AGENT_POLLING_THRESHOLD_MS) / 1000 - }s AND not (${buildKueryForErrorAgents(path)}) AND not ( ${buildKueryForUpdatingAgents(path)} )`; + }s ${addExclusiveKueryFilter(buildKueryForOfflineAgents, path)}`; } -export function buildKueryForUpgradingAgents(path: string = '') { +export function buildKueryForUpgradingAgents(path: string = ''): string { return `(${path}upgrade_started_at:*) and not (${path}upgrade_status:completed)`; } -export function buildKueryForUpdatingAgents(path: string = '') { - return `(${buildKueryForUpgradingAgents(path)}) or (${buildKueryForEnrollingAgents( +export function buildKueryForUpdatingAgents(path: string = ''): string { + return `((${buildKueryForUpgradingAgents(path)}) or (${buildKueryForEnrollingAgents( + path + )}) or (${buildKueryForUnenrollingAgents( + path + )}) or (not ${path}policy_revision_idx:*)) ${addExclusiveKueryFilter( + buildKueryForUpdatingAgents, path - )}) or (${buildKueryForUnenrollingAgents(path)}) or (not ${path}policy_revision_idx:*)`; + )}`; } export function buildKueryForInactiveAgents(path: string = '') { return `${path}active:false`; } + +const ORDERED_KUERY_BUILDER = [ + buildKueryForInactiveAgents, + buildKueryForOfflineAgents, + buildKueryForErrorAgents, + buildKueryForUpdatingAgents, + buildKueryForOnlineAgents, +]; + +function addExclusiveKueryFilter(currentKueryBuilder: (path?: string) => string, path?: string) { + if (ORDERED_KUERY_BUILDER.indexOf(currentKueryBuilder) === 0) { + return ''; + } + + const previousKueryBuilderIndex = + ORDERED_KUERY_BUILDER.indexOf(currentKueryBuilder) > 0 + ? ORDERED_KUERY_BUILDER.indexOf(currentKueryBuilder) - 1 + : ORDERED_KUERY_BUILDER.length - 1; + + return ` AND not (${ORDERED_KUERY_BUILDER[previousKueryBuilderIndex](path)})`; +}