Skip to content

Commit

Permalink
added last checkin status for last 1h
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 18, 2022
1 parent e110da3 commit d95598a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
58 changes: 49 additions & 9 deletions x-pack/plugins/fleet/server/collectors/agent_collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,35 @@ export const getAgentUsage = async (

export interface AgentData {
agent_versions: string[];
agent_last_checkin_status: {
agent_checkin_status: {
error: number;
degraded: number;
};
agent_checkin_status_last_1h: {
error: number;
degraded: number;
};
}

const DEFAULT_AGENT_DATA = {
agent_versions: [],
agent_last_checkin_status: { error: 0, degraded: 0 },
agent_checkin_status: { error: 0, degraded: 0 },
agent_checkin_status_last_1h: { error: 0, degraded: 0 },
};

export const getAgentData = async (esClient?: ElasticsearchClient): Promise<AgentData> => {
if (!esClient) {
return DEFAULT_AGENT_DATA;
}
try {
const transformLastCheckinStatusBuckets = (resp: any) =>
((resp?.aggregations?.last_checkin_status as any).buckets ?? []).reduce(
(acc: any, bucket: any) => {
if (acc[bucket.key] !== undefined) acc[bucket.key] = bucket.doc_count;
return acc;
},
{ error: 0, degraded: 0 }
);
const response = await esClient.search({
index: AGENTS_INDEX,
size: 0,
Expand All @@ -83,16 +96,43 @@ export const getAgentData = async (esClient?: ElasticsearchClient): Promise<Agen
const versions = ((response?.aggregations?.versions as any).buckets ?? []).map(
(bucket: any) => bucket.key
);
const statuses = ((response?.aggregations?.last_checkin_status as any).buckets ?? []).reduce(
(acc: any, bucket: any) => {
if (acc[bucket.key] !== undefined) acc[bucket.key] = bucket.doc_count;
return acc;
const statuses = transformLastCheckinStatusBuckets(response);

const responseLast1h = await esClient.search({
index: AGENTS_INDEX,
size: 0,
query: {
bool: {
filter: [
{
bool: {
must: [
{
range: {
last_checkin: {
gte: 'now-1h/h',
lt: 'now/h',
},
},
},
],
},
},
],
},
},
{ error: 0, degraded: 0 }
);
aggs: {
last_checkin_status: {
terms: { field: 'last_checkin_status' },
},
},
});
const statusesLast1h = transformLastCheckinStatusBuckets(responseLast1h);

return {
agent_versions: versions,
agent_last_checkin_status: statuses,
agent_checkin_status: statuses,
agent_checkin_status_last_1h: statusesLast1h,
};
} catch (error) {
if (error.statusCode === 404) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,23 @@ export const fleetUsagesSchema: RootSchema<any> = {
},
},
},
agent_last_checkin_status: {
agent_checkin_status: {
properties: {
error: {
type: 'long',
_meta: {
description: 'Count of agent last checkin status error',
},
},
degraded: {
type: 'long',
_meta: {
description: 'Count of agent last checkin status degraded',
},
},
},
},
agent_checkin_status_last_1h: {
properties: {
error: {
type: 'long',
Expand Down

0 comments on commit d95598a

Please sign in to comment.