Skip to content

Commit

Permalink
[Fleet] Change agent status order offline before updating
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Sep 13, 2022
1 parent 2be75d5 commit b41f8f2
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions x-pack/plugins/fleet/common/services/agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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';
}
Expand All @@ -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)})`;
}

0 comments on commit b41f8f2

Please sign in to comment.