Skip to content

Commit

Permalink
[Fleet] Fix agent status computation to support agent v2 uppercase (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Dec 1, 2022
1 parent 66718fc commit 1145112
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
10 changes: 5 additions & 5 deletions x-pack/plugins/fleet/common/services/agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export function getAgentStatus(agent: Agent | FleetServerAgent): AgentStatus {
return 'unenrolling';
}

if (agent.last_checkin_status === 'error') {
if (agent.last_checkin_status?.toLowerCase() === 'error') {
return 'error';
}
if (agent.last_checkin_status === 'degraded') {
if (agent.last_checkin_status?.toLowerCase() === 'degraded') {
return 'degraded';
}

Expand All @@ -61,10 +61,10 @@ export function getPreviousAgentStatusForOfflineAgents(
return 'unenrolling';
}

if (agent.last_checkin_status === 'error') {
if (agent.last_checkin_status?.toLowerCase() === 'error') {
return 'error';
}
if (agent.last_checkin_status === 'degraded') {
if (agent.last_checkin_status?.toLowerCase() === 'degraded') {
return 'degraded';
}

Expand Down Expand Up @@ -96,7 +96,7 @@ export function buildKueryForOnlineAgents(path: string = ''): string {
}

export function buildKueryForErrorAgents(path: string = ''): string {
return `(${path}last_checkin_status:error or ${path}last_checkin_status:degraded) ${addExclusiveKueryFilter(
return `(${path}last_checkin_status:error or ${path}last_checkin_status:degraded or ${path}last_checkin_status:DEGRADED or ${path}last_checkin_status:ERROR) ${addExclusiveKueryFilter(
[buildKueryForOfflineAgents, buildKueryForUnenrollingAgents],
path
)}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,30 @@ describe('test endpoint routes', () => {
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
match: {
'united.agent.last_checkin_status': 'DEGRADED',
},
},
],
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
match: {
'united.agent.last_checkin_status': 'ERROR',
},
},
],
minimum_should_match: 1,
},
},
],
minimum_should_match: 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,32 @@ export const expectedCompleteUnitedIndexQuery = {
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
match: {
'united.agent.last_checkin_status':
'DEGRADED',
},
},
],
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
match: {
'united.agent.last_checkin_status':
'ERROR',
},
},
],
minimum_should_match: 1,
},
},
],
minimum_should_match: 1,
},
Expand Down Expand Up @@ -248,6 +274,24 @@ export const expectedCompleteUnitedIndexQuery = {
minimum_should_match: 1,
},
},
{
bool: {
should: [
{
match: { 'united.agent.last_checkin_status': 'DEGRADED' },
},
],
minimum_should_match: 1,
},
},
{
bool: {
should: [
{ match: { 'united.agent.last_checkin_status': 'ERROR' } },
],
minimum_should_match: 1,
},
},
],
minimum_should_match: 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('test filtering endpoint hosts by agent status', () => {
const status = ['healthy'];
const kuery = buildStatusesKuery(status);
expect(kuery).toMatchInlineSnapshot(
`"(united.agent.last_checkin:* AND not ((united.agent.last_checkin < now-300s) or ((((united.agent.upgrade_started_at:*) and not (united.agent.upgraded_at:*)) or (not (united.agent.last_checkin:*)) or (united.agent.unenrollment_started_at:*) or (not united.agent.policy_revision_idx:*)) AND not ((united.agent.last_checkin < now-300s) or ((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*))))) or ((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))))"`
`"(united.agent.last_checkin:* AND not ((united.agent.last_checkin < now-300s) or ((((united.agent.upgrade_started_at:*) and not (united.agent.upgraded_at:*)) or (not (united.agent.last_checkin:*)) or (united.agent.unenrollment_started_at:*) or (not united.agent.policy_revision_idx:*)) AND not ((united.agent.last_checkin < now-300s) or ((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded or united.agent.last_checkin_status:DEGRADED or united.agent.last_checkin_status:ERROR) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*))))) or ((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded or united.agent.last_checkin_status:DEGRADED or united.agent.last_checkin_status:ERROR) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))))"`
);
});

Expand All @@ -107,15 +107,15 @@ describe('test filtering endpoint hosts by agent status', () => {
const status = ['unhealthy'];
const kuery = buildStatusesKuery(status);
expect(kuery).toMatchInlineSnapshot(
`"((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))"`
`"((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded or united.agent.last_checkin_status:DEGRADED or united.agent.last_checkin_status:ERROR) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))"`
);
});

it('correctly builds kuery for updating status', () => {
const status = ['updating'];
const kuery = buildStatusesKuery(status);
expect(kuery).toMatchInlineSnapshot(
`"((((united.agent.upgrade_started_at:*) and not (united.agent.upgraded_at:*)) or (not (united.agent.last_checkin:*)) or (united.agent.unenrollment_started_at:*) or (not united.agent.policy_revision_idx:*)) AND not ((united.agent.last_checkin < now-300s) or ((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))))"`
`"((((united.agent.upgrade_started_at:*) and not (united.agent.upgraded_at:*)) or (not (united.agent.last_checkin:*)) or (united.agent.unenrollment_started_at:*) or (not united.agent.policy_revision_idx:*)) AND not ((united.agent.last_checkin < now-300s) or ((united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded or united.agent.last_checkin_status:DEGRADED or united.agent.last_checkin_status:ERROR) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))))"`
);
});

Expand All @@ -130,7 +130,7 @@ describe('test filtering endpoint hosts by agent status', () => {
const statuses = ['offline', 'unhealthy'];
const kuery = buildStatusesKuery(statuses);
expect(kuery).toMatchInlineSnapshot(
`"(united.agent.last_checkin < now-300s OR (united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))"`
`"(united.agent.last_checkin < now-300s OR (united.agent.last_checkin_status:error or united.agent.last_checkin_status:degraded or united.agent.last_checkin_status:DEGRADED or united.agent.last_checkin_status:ERROR) AND not ((united.agent.last_checkin < now-300s) or (united.agent.unenrollment_started_at:*)))"`
);
});
});
Expand Down

0 comments on commit 1145112

Please sign in to comment.