Skip to content

Commit

Permalink
[Fleet] Add fleet server telemetry (#101400) (#101684)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
  • Loading branch information
kibanamachine and nchaulet committed Jun 9, 2021
1 parent 3b8eb53 commit 7b6c599
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 2 deletions.
7 changes: 5 additions & 2 deletions x-pack/plugins/fleet/server/collectors/agent_collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* 2.0.
*/

import type { SavedObjectsClient } from 'kibana/server';
import type { ElasticsearchClient } from 'kibana/server';
import type { SavedObjectsClient, ElasticsearchClient } from 'kibana/server';

import type { FleetConfigType } from '../../common/types';
import * as AgentService from '../services/agents';
Expand All @@ -18,6 +17,7 @@ export interface AgentUsage {
unhealthy: number;
offline: number;
total_all_statuses: number;
updating: number;
}

export const getAgentUsage = async (
Expand All @@ -33,6 +33,7 @@ export const getAgentUsage = async (
unhealthy: 0,
offline: 0,
total_all_statuses: 0,
updating: 0,
};
}

Expand All @@ -42,12 +43,14 @@ export const getAgentUsage = async (
online,
error,
offline,
updating,
} = await AgentService.getAgentStatusForAgentPolicy(soClient, esClient);
return {
total_enrolled: total,
healthy: online,
unhealthy: error,
offline,
total_all_statuses: total + inactive,
updating,
};
};
87 changes: 87 additions & 0 deletions x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { SavedObjectsClient, ElasticsearchClient } from 'kibana/server';

import { packagePolicyService, settingsService } from '../services';
import { getAgentStatusForAgentPolicy } from '../services/agents';
import { isFleetServerSetup } from '../services/fleet_server';

const DEFAULT_USAGE = {
total_all_statuses: 0,
total_enrolled: 0,
healthy: 0,
unhealthy: 0,
offline: 0,
updating: 0,
num_host_urls: 0,
};

export interface FleetServerUsage {
total_enrolled: number;
healthy: number;
unhealthy: number;
offline: number;
updating: number;
total_all_statuses: number;
num_host_urls: number;
}

export const getFleetServerUsage = async (
soClient?: SavedObjectsClient,
esClient?: ElasticsearchClient
): Promise<any> => {
if (!soClient || !esClient || !(await isFleetServerSetup())) {
return DEFAULT_USAGE;
}

const numHostsUrls =
(await settingsService.getSettings(soClient)).fleet_server_hosts?.length ?? 0;

// Find all policies with Fleet server than query agent status

let hasMore = true;
const policyIds = new Set<string>();
let page = 1;
while (hasMore) {
const res = await packagePolicyService.list(soClient, {
page: page++,
perPage: 20,
kuery: 'ingest-package-policies.package.name:fleet_server',
});

for (const item of res.items) {
policyIds.add(item.policy_id);
}

if (res.items.length === 0) {
hasMore = false;
}
}

if (policyIds.size === 0) {
return DEFAULT_USAGE;
}

const { total, inactive, online, error, updating, offline } = await getAgentStatusForAgentPolicy(
soClient,
esClient,
undefined,
Array.from(policyIds)
.map((policyId) => `(policy_id:"${policyId}")`)
.join(' or ')
);

return {
total_enrolled: total,
healthy: online,
unhealthy: error,
offline,
updating,
total_all_statuses: total + inactive,
num_host_urls: numHostsUrls,
};
};
55 changes: 55 additions & 0 deletions x-pack/plugins/fleet/server/collectors/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import type { AgentUsage } from './agent_collectors';
import { getInternalClients } from './helpers';
import { getPackageUsage } from './package_collectors';
import type { PackageUsage } from './package_collectors';
import { getFleetServerUsage } from './fleet_server_collector';
import type { FleetServerUsage } from './fleet_server_collector';

interface Usage {
agents_enabled: boolean;
agents: AgentUsage;
packages: PackageUsage[];
fleet_server: FleetServerUsage;
}

export function registerFleetUsageCollector(
Expand All @@ -43,6 +46,7 @@ export function registerFleetUsageCollector(
return {
agents_enabled: getIsAgentsEnabled(config),
agents: await getAgentUsage(config, soClient, esClient),
fleet_server: await getFleetServerUsage(soClient, esClient),
packages: await getPackageUsage(soClient),
};
},
Expand All @@ -67,6 +71,12 @@ export function registerFleetUsageCollector(
description: 'The total number of enrolled agents in an unhealthy state',
},
},
updating: {
type: 'long',
_meta: {
description: 'The total number of enrolled agents in an updating state',
},
},
offline: {
type: 'long',
_meta: {
Expand All @@ -80,6 +90,51 @@ export function registerFleetUsageCollector(
},
},
},
fleet_server: {
total_enrolled: {
type: 'long',
_meta: {
description: 'The total number of enrolled Fleet Server agents, in any state',
},
},
total_all_statuses: {
type: 'long',
_meta: {
description:
'The total number of Fleet Server agents in any state, both enrolled and inactive.',
},
},
healthy: {
type: 'long',
_meta: {
description: 'The total number of enrolled Fleet Server agents in a healthy state.',
},
},
unhealthy: {
type: 'long',
_meta: {
description: 'The total number of enrolled Fleet Server agents in an unhealthy state',
},
},
updating: {
type: 'long',
_meta: {
description: 'The total number of enrolled Fleet Server agents in an updating state',
},
},
offline: {
type: 'long',
_meta: {
description: 'The total number of enrolled Fleet Server agents currently offline',
},
},
num_host_urls: {
type: 'long',
_meta: {
description: 'The number of Fleet Server hosts configured in Fleet settings.',
},
},
},
packages: {
type: 'array',
items: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,12 @@
"description": "The total number of enrolled agents in an unhealthy state"
}
},
"updating": {
"type": "long",
"_meta": {
"description": "The total number of enrolled agents in an updating state"
}
},
"offline": {
"type": "long",
"_meta": {
Expand All @@ -2055,6 +2061,52 @@
}
}
},
"fleet_server": {
"properties": {
"total_enrolled": {
"type": "long",
"_meta": {
"description": "The total number of enrolled Fleet Server agents, in any state"
}
},
"total_all_statuses": {
"type": "long",
"_meta": {
"description": "The total number of Fleet Server agents in any state, both enrolled and inactive."
}
},
"healthy": {
"type": "long",
"_meta": {
"description": "The total number of enrolled Fleet Server agents in a healthy state."
}
},
"unhealthy": {
"type": "long",
"_meta": {
"description": "The total number of enrolled Fleet Server agents in an unhealthy state"
}
},
"updating": {
"type": "long",
"_meta": {
"description": "The total number of enrolled Fleet Server agents in an updating state"
}
},
"offline": {
"type": "long",
"_meta": {
"description": "The total number of enrolled Fleet Server agents currently offline"
}
},
"num_host_urls": {
"type": "long",
"_meta": {
"description": "The number of Fleet Server hosts configured in Fleet settings."
}
}
}
},
"packages": {
"type": "array",
"items": {
Expand Down
Loading

0 comments on commit 7b6c599

Please sign in to comment.