Skip to content

Commit

Permalink
[Fleet] Add fleet server telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jun 4, 2021
1 parent 8f83090 commit 9be5d2c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
3 changes: 1 addition & 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 Down
73 changes: 73 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,73 @@
/*
* 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,
healthy: 0,
num_host_urls: 0,
};

export interface FleetServerUsage {
healthy: 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 } = await getAgentStatusForAgentPolicy(
soClient,
esClient,
undefined,
[...policyIds].map((policyId) => `(policy_id:"${policyId}")`).join(' or ')
);

return {
total_all_statuses: total + inactive,
healthy: online,
num_host_urls: numHostsUrls,
};
};
25 changes: 25 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 Down Expand Up @@ -80,6 +84,27 @@ export function registerFleetUsageCollector(
},
},
},
fleet_server: {
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.',
},
},
num_host_urls: {
type: 'long',
_meta: {
description: 'The number of Fleet Server hosts configured in Fleet settings.',
},
},
},
packages: {
type: 'array',
items: {
Expand Down

0 comments on commit 9be5d2c

Please sign in to comment.