From 3f79c38f40612fe657fd7c313df74654205aa7f8 Mon Sep 17 00:00:00 2001 From: Igor Zaytsev Date: Thu, 1 Oct 2020 21:04:00 -0400 Subject: [PATCH 1/5] fixed internal monitoring check --- .../monitoring/server/lib/ccs_utils.js | 20 ++++++++++++------- .../check/internal_monitoring.ts | 19 +++++++++++++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/monitoring/server/lib/ccs_utils.js b/x-pack/plugins/monitoring/server/lib/ccs_utils.js index 96910dd86a94d..649611742df2c 100644 --- a/x-pack/plugins/monitoring/server/lib/ccs_utils.js +++ b/x-pack/plugins/monitoring/server/lib/ccs_utils.js @@ -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 @@ -16,8 +19,7 @@ export function appendMetricbeatIndex(config, indexPattern) { mbIndex = get(config, 'ui.metricbeat.index'); } - const newIndexPattern = `${indexPattern},${mbIndex}`; - return newIndexPattern; + return `${indexPattern},${mbIndex}`; } /** @@ -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 @@ -42,7 +44,7 @@ export function prefixIndexPattern(config, indexPattern, ccs) { } if (!ccsEnabled || !ccs) { - return appendMetricbeatIndex(config, indexPattern); + return appendMetricbeatIndex(config, indexPattern, monitoringIndicesOnly); } const patterns = indexPattern.split(','); @@ -50,10 +52,14 @@ export function prefixIndexPattern(config, indexPattern, ccs) { // 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); } /** diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts index 4473d824c9e30..4aecda32b541f 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts @@ -5,8 +5,13 @@ */ 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'; @@ -49,7 +54,10 @@ 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', @@ -62,7 +70,12 @@ export function internalMonitoringCheckRoute(server: unknown, npRoute: RouteDepe mb_indices: 0, }; - const { esIndexPattern, kbnIndexPattern, lsIndexPattern } = getIndexPatterns(server); + const config = server.config(); + const ccs = '*'; + const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs, true); + const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs, true); + const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs, true); + const indexCounts = await Promise.all([ checkLatestMonitoringIsLegacy(context, esIndexPattern), checkLatestMonitoringIsLegacy(context, kbnIndexPattern), From c79fe801dca4655d8b5b2fcc58cd74aa2cab4c91 Mon Sep 17 00:00:00 2001 From: Igor Zaytsev Date: Fri, 2 Oct 2020 13:43:23 -0400 Subject: [PATCH 2/5] Added range filter --- .../check/internal_monitoring.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts index 4aecda32b541f..21bf8c9ec228b 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts @@ -18,6 +18,19 @@ import { RouteDependencies } from '../../../../../types'; const queryBody = { size: 0, + query: { + bool: { + must: [ + { + range: { + timestamp: { + gte: 'now-12h', + }, + }, + }, + ], + }, + }, aggs: { types: { terms: { From f2eaf0787ea4d5ca303b4e178664a3eb3b065ca8 Mon Sep 17 00:00:00 2001 From: Igor Zaytsev Date: Fri, 2 Oct 2020 16:45:31 -0400 Subject: [PATCH 3/5] Added single vs ccs condtion --- .../monitoring/public/services/clusters.js | 4 +-- .../check/internal_monitoring.ts | 25 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/monitoring/public/services/clusters.js b/x-pack/plugins/monitoring/public/services/clusters.js index 7f772ac1e1bcd..0124855d54c77 100644 --- a/x-pack/plugins/monitoring/public/services/clusters.js +++ b/x-pack/plugins/monitoring/public/services/clusters.js @@ -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'; 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, diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts index 21bf8c9ec228b..e89f8716b21a5 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts @@ -4,6 +4,7 @@ * 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, @@ -73,10 +74,14 @@ export function internalMonitoringCheckRoute( ) { 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, @@ -84,10 +89,16 @@ export function internalMonitoringCheckRoute( }; const config = server.config(); - const ccs = '*'; - const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs, true); - const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs, true); - const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs, true); + 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), From c78e30fa54cd25d73bf82f9d6be547e6381f6d0a Mon Sep 17 00:00:00 2001 From: Igor Zaytsev Date: Mon, 5 Oct 2020 13:33:57 -0400 Subject: [PATCH 4/5] Fixed spelling --- x-pack/plugins/monitoring/public/services/clusters.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/monitoring/public/services/clusters.js b/x-pack/plugins/monitoring/public/services/clusters.js index 0124855d54c77..5f046bf296f4e 100644 --- a/x-pack/plugins/monitoring/public/services/clusters.js +++ b/x-pack/plugins/monitoring/public/services/clusters.js @@ -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'; + const ccs = window.location.hash.split('?')[0] === '#/home' ? 'ccs' : 'single'; return $http - .get(`../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring/${css}`) + .get(`../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring/${ccs}`) .then(({ data }) => { showInternalMonitoringToast({ legacyIndices: data.legacy_indices, From 8856761f3f180e3944169a47f29ad488aff30edb Mon Sep 17 00:00:00 2001 From: Igor Zaytsev Date: Thu, 8 Oct 2020 07:33:40 -0400 Subject: [PATCH 5/5] Passing global state ccs --- .../monitoring/public/services/clusters.js | 6 ++++-- .../check/internal_monitoring.ts | 21 +++++++------------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/monitoring/public/services/clusters.js b/x-pack/plugins/monitoring/public/services/clusters.js index 5f046bf296f4e..d0ca1bc6bbde6 100644 --- a/x-pack/plugins/monitoring/public/services/clusters.js +++ b/x-pack/plugins/monitoring/public/services/clusters.js @@ -69,9 +69,11 @@ export function monitoringClustersProvider($injector) { if (Legacy.shims.isCloud) { return Promise.resolve(); } - const ccs = window.location.hash.split('?')[0] === '#/home' ? 'ccs' : 'single'; + const globalState = $injector.get('globalState'); return $http - .get(`../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring/${ccs}`) + .post('../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring', { + ccs: globalState.ccs, + }) .then(({ data }) => { showInternalMonitoringToast({ legacyIndices: data.legacy_indices, diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts index e89f8716b21a5..ef2bd8209a469 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts @@ -72,11 +72,11 @@ export function internalMonitoringCheckRoute( server: { config: () => unknown }, npRoute: RouteDependencies ) { - npRoute.router.get( + npRoute.router.post( { - path: '/api/monitoring/v1/elasticsearch_settings/check/internal_monitoring/{ccs}', + path: '/api/monitoring/v1/elasticsearch_settings/check/internal_monitoring', validate: { - params: schema.object({ + body: schema.object({ ccs: schema.maybe(schema.string()), }), }, @@ -89,17 +89,10 @@ export function internalMonitoringCheckRoute( }; 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 { ccs } = request.body; + const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs, true); + const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs, true); + const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs, true); const indexCounts = await Promise.all([ checkLatestMonitoringIsLegacy(context, esIndexPattern), checkLatestMonitoringIsLegacy(context, kbnIndexPattern),