diff --git a/x-pack/plugins/fleet/server/collectors/agent_policies.ts b/x-pack/plugins/fleet/server/collectors/agent_policies.ts index 10c8daba37a24..95c840536b6e5 100644 --- a/x-pack/plugins/fleet/server/collectors/agent_policies.ts +++ b/x-pack/plugins/fleet/server/collectors/agent_policies.ts @@ -9,33 +9,53 @@ import type { ElasticsearchClient } from '@kbn/core/server'; import { AGENT_POLICY_INDEX } from '../../common'; import { ES_SEARCH_LIMIT } from '../../common/constants'; +import { appContextService } from '../services'; + +interface AgentPoliciesUsage { + count: number; + output_types: string[]; +} + +const DEFAULT_AGENT_POLICIES_USAGE = { + count: 0, + output_types: [], +}; export const getAgentPoliciesUsage = async ( esClient: ElasticsearchClient, abortController: AbortController -): Promise => { - const res = await esClient.search( - { - index: AGENT_POLICY_INDEX, - size: ES_SEARCH_LIMIT, - track_total_hits: true, - rest_total_hits_as_int: true, - }, - { signal: abortController.signal } - ); - - const agentPolicies = res.hits.hits; - - const outputTypes = new Set(); - agentPolicies.forEach((item) => { - const source = (item._source as any) ?? {}; - Object.keys(source.data.outputs).forEach((output) => { - outputTypes.add(source.data.outputs[output].type); +): Promise => { + try { + const res = await esClient.search( + { + index: AGENT_POLICY_INDEX, + size: ES_SEARCH_LIMIT, + track_total_hits: true, + rest_total_hits_as_int: true, + }, + { signal: abortController.signal } + ); + + const agentPolicies = res.hits.hits; + + const outputTypes = new Set(); + agentPolicies.forEach((item) => { + const source = (item._source as any) ?? {}; + Object.keys(source.data.outputs).forEach((output) => { + outputTypes.add(source.data.outputs[output].type); + }); }); - }); - return { - count: res.hits.total, - output_types: Array.from(outputTypes), - }; + return { + count: res.hits.total as number, + output_types: Array.from(outputTypes), + }; + } catch (error) { + if (error.statusCode === 404) { + appContextService.getLogger().debug('Index .fleet-policies does not exist yet.'); + } else { + throw error; + } + return DEFAULT_AGENT_POLICIES_USAGE; + } }; diff --git a/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts b/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts index 7d49089d02309..f48e166209edf 100644 --- a/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts +++ b/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts @@ -93,8 +93,17 @@ export const getFleetServerConfig = async (soClient: SavedObjectsClient): Promis perPage: SO_SEARCH_LIMIT, kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:fleet_server`, }); + const getInputConfig = (item: any) => { + let config = (item.inputs[0] ?? {}).compiled_input; + if (config.server) { + config = { ...config, server: { ...config.server } }; + delete config.server.host; + delete config.server.port; + } + return config; + }; const policies = res.items.map((item) => ({ - input_config: (item.inputs[0] ?? {}).compiled_input, + input_config: getInputConfig(item), })); return { policies };