-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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] Add fleet server telemetry #101400
Merged
nchaulet
merged 7 commits into
elastic:master
from
nchaulet:feature-fleet-server-telemetry
Jun 8, 2021
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9be5d2c
[Fleet] Add fleet server telemetry
nchaulet aa73aa6
Update mappings
nchaulet 57b6ddc
Merge branch 'master' of github.com:elastic/kibana into feature-fleet…
nchaulet a0dacdc
Add integration tests for Fleet telemetry
nchaulet f0fc3d6
Merge branch 'master' of github.com:elastic/kibana into feature-fleet…
nchaulet 1b0c838
Breakdown all agent statuses in agent and fleet collector
nchaulet 6cbd41e
use Array.from instead of spread
nchaulet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
x-pack/test/fleet_api_integration/apis/fleet_telemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../api_integration/ftr_provider_context'; | ||
import { skipIfNoDockerRegistry } from '../helpers'; | ||
import { setupFleetAndAgents } from './agents/services'; | ||
|
||
export default function (providerContext: FtrProviderContext) { | ||
const { getService } = providerContext; | ||
const supertest = getService('supertest'); | ||
const es = getService('es'); | ||
const esArchiver = getService('esArchiver'); | ||
|
||
let agentCount = 0; | ||
async function generateAgent(status: string, policyId: string) { | ||
let data: any = {}; | ||
|
||
switch (status) { | ||
case 'unhealthy': | ||
data = { last_checkin_status: 'error' }; | ||
break; | ||
case 'offline': | ||
data = { last_checkin: '2017-06-07T18:59:04.498Z' }; | ||
break; | ||
default: | ||
data = { last_checkin: new Date().toISOString() }; | ||
} | ||
|
||
await es.index({ | ||
index: '.fleet-agents', | ||
body: { | ||
id: `agent-${++agentCount}`, | ||
active: true, | ||
last_checkin: new Date().toISOString(), | ||
policy_id: policyId, | ||
policy_revision: 1, | ||
...data, | ||
}, | ||
refresh: 'wait_for', | ||
}); | ||
} | ||
|
||
describe('fleet_telemetry', () => { | ||
skipIfNoDockerRegistry(providerContext); | ||
before(async () => { | ||
await esArchiver.load('empty_kibana'); | ||
await esArchiver.load('fleet/empty_fleet_server'); | ||
}); | ||
|
||
setupFleetAndAgents(providerContext); | ||
|
||
after(async () => { | ||
await esArchiver.unload('empty_kibana'); | ||
await esArchiver.unload('fleet/empty_fleet_server'); | ||
}); | ||
|
||
before(async () => { | ||
// Get FleetServer policy id | ||
const { body: apiResponse } = await supertest.get(`/api/fleet/agent_policies`).expect(200); | ||
const defaultFleetServerPolicy = apiResponse.items.find( | ||
(item: any) => item.is_default_fleet_server | ||
); | ||
|
||
const defaultServerPolicy = apiResponse.items.find((item: any) => item.is_default); | ||
|
||
if (!defaultFleetServerPolicy) { | ||
throw new Error('No default Fleet server policy'); | ||
} | ||
|
||
if (!defaultServerPolicy) { | ||
throw new Error('No default policy'); | ||
} | ||
|
||
await supertest | ||
.put(`/api/fleet/settings`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ fleet_server_hosts: ['https://test1.fr', 'https://test2.fr'] }) | ||
.expect(200); | ||
|
||
// Default Fleet Server | ||
await generateAgent('healthy', defaultFleetServerPolicy.id); | ||
await generateAgent('healthy', defaultFleetServerPolicy.id); | ||
await generateAgent('unhealthy', defaultFleetServerPolicy.id); | ||
|
||
// Default policy | ||
await generateAgent('healthy', defaultServerPolicy.id); | ||
await generateAgent('offline', defaultServerPolicy.id); | ||
await generateAgent('unhealthy', defaultServerPolicy.id); | ||
}); | ||
|
||
it('should return the correct telemetry values for fleet', async () => { | ||
const { | ||
body: [apiResponse], | ||
} = await supertest | ||
.post(`/api/telemetry/v2/clusters/_stats`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
unencrypted: true, | ||
}) | ||
.expect(200); | ||
|
||
expect(apiResponse.stack_stats.kibana.plugins.fleet.agents).eql({ | ||
total_enrolled: 6, | ||
healthy: 3, | ||
unhealthy: 2, | ||
offline: 1, | ||
total_all_statuses: 6, | ||
}); | ||
|
||
expect(apiResponse.stack_stats.kibana.plugins.fleet.fleet_server).eql({ | ||
total_all_statuses: 3, | ||
healthy: 2, | ||
num_host_urls: 2, | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need the
[...policyIds]
?.map
won't mutate the array, so this could maybe bepolicyIds.map
directly w/o the copy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
policyIds
is not an array it's a set (I want uniq values) so I think we need the spread operator here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I understand.
Array.from
might convey the intent more explicitly in that case: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from