-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Fleet Usage telemetry extension #145353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fleet Usage telemetry extension #145353
Changes from 13 commits
b34f4d4
aa0196d
b92d5f1
25e1e09
8d54d36
cefd848
e110da3
d95598a
4c5f29e
f12a872
b29541b
6c6de57
43788cb
99f5a61
1fa218b
241a5e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,9 @@ | |
| import type { SavedObjectsClient, ElasticsearchClient } from '@kbn/core/server'; | ||
|
|
||
| import type { FleetConfigType } from '../../common/types'; | ||
| import { AGENTS_INDEX } from '../../common'; | ||
| import * as AgentService from '../services/agents'; | ||
| import { appContextService } from '../services'; | ||
|
|
||
| export interface AgentUsage { | ||
| total_enrolled: number; | ||
|
|
@@ -47,3 +49,103 @@ export const getAgentUsage = async ( | |
| updating, | ||
| }; | ||
| }; | ||
|
|
||
| export interface AgentData { | ||
| agent_versions: string[]; | ||
| agent_checkin_status: { | ||
| error: number; | ||
| degraded: number; | ||
| }; | ||
| agent_checkin_status_last_1h: { | ||
| error: number; | ||
| degraded: number; | ||
| }; | ||
| } | ||
|
|
||
| const DEFAULT_AGENT_DATA = { | ||
| agent_versions: [], | ||
| agent_checkin_status: { error: 0, degraded: 0 }, | ||
| agent_checkin_status_last_1h: { error: 0, degraded: 0 }, | ||
| }; | ||
|
|
||
| export const getAgentData = async ( | ||
| esClient: ElasticsearchClient, | ||
| abortController: AbortController | ||
| ): Promise<AgentData> => { | ||
| 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, | ||
| aggs: { | ||
joshdover marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| versions: { | ||
| terms: { field: 'agent.version' }, | ||
| }, | ||
| last_checkin_status: { | ||
| terms: { field: 'last_checkin_status' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { signal: abortController.signal } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. abortController helps to terminate queries if the Currently the task takes about
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for being mindful about performance here. As I understand it, things like background tasks, telemetry collection, etc will eventually run on Kibana's separate "service" process, so heavy operations like this will eventually be optimized such that they aren't taking time or resources away from the main Kibana server/UI. Still, it's good to make sure we're not running a bunch of long running queries here. |
||
| ); | ||
| const versions = ((response?.aggregations?.versions as any).buckets ?? []).map( | ||
| (bucket: any) => bucket.key | ||
| ); | ||
| 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', | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| aggs: { | ||
| last_checkin_status: { | ||
| terms: { field: 'last_checkin_status' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { signal: abortController.signal } | ||
| ); | ||
| const statusesLast1h = transformLastCheckinStatusBuckets(responseLast1h); | ||
|
|
||
| return { | ||
| agent_versions: versions, | ||
| agent_checkin_status: statuses, | ||
| agent_checkin_status_last_1h: statusesLast1h, | ||
| }; | ||
| } catch (error) { | ||
| if (error.statusCode === 404) { | ||
| appContextService.getLogger().debug('Index .fleet-agents does not exist yet.'); | ||
| } else { | ||
| throw error; | ||
| } | ||
| return DEFAULT_AGENT_DATA; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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 { ElasticsearchClient } from '@kbn/core/server'; | ||
|
|
||
| import { AGENT_POLICY_INDEX } from '../../common'; | ||
| import { ES_SEARCH_LIMIT } from '../../common/constants'; | ||
| import { appContextService } from '../services'; | ||
|
|
||
| export 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<AgentPoliciesUsage> => { | ||
| 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<string>(); | ||
| 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 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; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
|
|
||
| import type { SavedObjectsClient, ElasticsearchClient } from '@kbn/core/server'; | ||
|
|
||
| import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, SO_SEARCH_LIMIT } from '../constants'; | ||
|
|
||
| import { packagePolicyService } from '../services'; | ||
| import { getAgentStatusForAgentPolicy } from '../services/agents'; | ||
| import { listFleetServerHosts } from '../services/fleet_server_host'; | ||
|
|
@@ -84,3 +86,25 @@ export const getFleetServerUsage = async ( | |
| num_host_urls: numHostsUrls, | ||
| }; | ||
| }; | ||
|
|
||
| export const getFleetServerConfig = async (soClient: SavedObjectsClient): Promise<any> => { | ||
| const res = await packagePolicyService.list(soClient, { | ||
| page: 1, | ||
| 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: getInputConfig(item), | ||
| })); | ||
|
|
||
| return { policies }; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.