Skip to content
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

[Monitoring] Fixed internal monitoring check #79241

Merged
merged 16 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x-pack/plugins/monitoring/public/services/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export function monitoringClustersProvider($injector) {
if (Legacy.shims.isCloud) {
return Promise.resolve();
}

const css = window.location.hash.split('?')[0] === '#/home' ? 'css' : 'single';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of hard-coding a URL when I think we can manage without it.

monitoringClusters is only called from a few places, and we can just provide an additional parameter to the function call for ccs and we can specify which one from the invokers.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisronline Actually this won't work. I tried passing it here:

return monitoringClusters(undefined, undefined, CODE_PATHS);
but looks like monitoringClusters executes somewhere else before hand (probably in a routeInit), which sets the once variable before we can check the param. This means I would have to do some extra logic to determine the "real" origin (not as clean as the one liner above)

return $http
.get('../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring')
.get(`../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring/${css}`)
.then(({ data }) => {
showInternalMonitoringToast({
legacyIndices: data.legacy_indices,
Expand Down
20 changes: 13 additions & 7 deletions x-pack/plugins/monitoring/server/lib/ccs_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
import { isFunction, get } from 'lodash';

export function appendMetricbeatIndex(config, indexPattern) {
export function appendMetricbeatIndex(config, indexPattern, bypass = false) {
if (bypass) {
return indexPattern;
}
// Leverage this function to also append the dynamic metricbeat index too
let mbIndex = null;
// TODO: NP
Expand All @@ -16,8 +19,7 @@ export function appendMetricbeatIndex(config, indexPattern) {
mbIndex = get(config, 'ui.metricbeat.index');
}

const newIndexPattern = `${indexPattern},${mbIndex}`;
return newIndexPattern;
return `${indexPattern},${mbIndex}`;
}

/**
Expand All @@ -31,7 +33,7 @@ export function appendMetricbeatIndex(config, indexPattern) {
* @param {String} ccs The optional cluster-prefix to prepend.
* @return {String} The index pattern with the {@code cluster} prefix appropriately prepended.
*/
export function prefixIndexPattern(config, indexPattern, ccs) {
export function prefixIndexPattern(config, indexPattern, ccs, monitoringIndicesOnly = false) {
let ccsEnabled = false;
// TODO: NP
// This function is called with both NP config and LP config
Expand All @@ -42,18 +44,22 @@ export function prefixIndexPattern(config, indexPattern, ccs) {
}

if (!ccsEnabled || !ccs) {
return appendMetricbeatIndex(config, indexPattern);
return appendMetricbeatIndex(config, indexPattern, monitoringIndicesOnly);
}

const patterns = indexPattern.split(',');
const prefixedPattern = patterns.map((pattern) => `${ccs}:${pattern}`).join(',');

// if a wildcard is used, then we also want to search the local indices
if (ccs === '*') {
return appendMetricbeatIndex(config, `${prefixedPattern},${indexPattern}`);
return appendMetricbeatIndex(
config,
`${prefixedPattern},${indexPattern}`,
monitoringIndicesOnly
);
}

return appendMetricbeatIndex(config, prefixedPattern);
return appendMetricbeatIndex(config, prefixedPattern, monitoringIndicesOnly);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,34 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { schema } from '@kbn/config-schema';
import { RequestHandlerContext } from 'kibana/server';
import {
INDEX_PATTERN_ELASTICSEARCH,
INDEX_PATTERN_KIBANA,
INDEX_PATTERN_LOGSTASH,
} from '../../../../../../common/constants';
// @ts-ignore
import { getIndexPatterns } from '../../../../../lib/cluster/get_index_patterns';
import { prefixIndexPattern } from '../../../../../lib/ccs_utils';
// @ts-ignore
import { handleError } from '../../../../../lib/errors';
import { RouteDependencies } from '../../../../../types';

const queryBody = {
size: 0,
query: {
bool: {
must: [
{
range: {
timestamp: {
gte: 'now-12h',
},
},
},
],
},
},
aggs: {
types: {
terms: {
Expand Down Expand Up @@ -49,20 +68,38 @@ const checkLatestMonitoringIsLegacy = async (context: RequestHandlerContext, ind
return counts;
};

export function internalMonitoringCheckRoute(server: unknown, npRoute: RouteDependencies) {
export function internalMonitoringCheckRoute(
server: { config: () => unknown },
npRoute: RouteDependencies
) {
npRoute.router.get(
{
path: '/api/monitoring/v1/elasticsearch_settings/check/internal_monitoring',
validate: false,
path: '/api/monitoring/v1/elasticsearch_settings/check/internal_monitoring/{ccs}',
validate: {
params: schema.object({
ccs: schema.maybe(schema.string()),
}),
},
},
async (context, _request, response) => {
async (context, request, response) => {
try {
const typeCount = {
legacy_indices: 0,
mb_indices: 0,
};

const { esIndexPattern, kbnIndexPattern, lsIndexPattern } = getIndexPatterns(server);
const config = server.config();
const { ccs } = request.params;
const ccsPrefix = ccs === 'ccs' ? '*' : null;
const esIndexPattern = prefixIndexPattern(
config,
INDEX_PATTERN_ELASTICSEARCH,
ccsPrefix,
true
);
const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccsPrefix, true);
const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccsPrefix, true);

const indexCounts = await Promise.all([
checkLatestMonitoringIsLegacy(context, esIndexPattern),
checkLatestMonitoringIsLegacy(context, kbnIndexPattern),
Expand Down