diff --git a/x-pack/plugins/monitoring/server/alerts/base_alert.ts b/x-pack/plugins/monitoring/server/alerts/base_alert.ts index 016acf2737f9b..f583b4882f83c 100644 --- a/x-pack/plugins/monitoring/server/alerts/base_alert.ts +++ b/x-pack/plugins/monitoring/server/alerts/base_alert.ts @@ -9,6 +9,7 @@ import { ILegacyCustomClusterClient, Logger, IUiSettingsClient, + LegacyCallAPIOptions, } from 'kibana/server'; import { i18n } from '@kbn/i18n'; import { @@ -36,6 +37,8 @@ import { MonitoringConfig } from '../config'; import { AlertSeverity } from '../../common/enums'; import { CommonAlertFilter, CommonAlertParams, CommonBaseAlert } from '../../common/types'; import { MonitoringLicenseService } from '../types'; +import { mbSafeQuery } from '../lib/mb_safe_query'; +import { appendMetricbeatIndex } from '../lib/alerts/append_mb_index'; export class BaseAlert { public type!: string; @@ -212,13 +215,20 @@ export class BaseAlert { `Executing alert with params: ${JSON.stringify(params)} and state: ${JSON.stringify(state)}` ); - const callCluster = this.monitoringCluster + const _callCluster = this.monitoringCluster ? this.monitoringCluster.callAsInternalUser : services.callCluster; + const callCluster = async ( + endpoint: string, + clientParams?: Record, + options?: LegacyCallAPIOptions + ) => { + return await mbSafeQuery(async () => _callCluster(endpoint, clientParams, options)); + }; const availableCcs = this.config.ui.ccs.enabled ? await fetchAvailableCcs(callCluster) : []; // Support CCS use cases by querying to find available remote clusters // and then adding those to the index pattern we are searching against - let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH; + let esIndexPattern = appendMetricbeatIndex(this.config, INDEX_PATTERN_ELASTICSEARCH); if (availableCcs) { esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs); } diff --git a/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts index 4b083787f58cb..66085b53516a2 100644 --- a/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts @@ -66,7 +66,11 @@ describe('ClusterHealthAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts index c330e977e53d8..2705a77e0fce4 100644 --- a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts @@ -70,7 +70,11 @@ describe('CpuUsageAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts index afe5abcf1ebd7..5bca84e33da3c 100644 --- a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts @@ -31,6 +31,7 @@ import { CommonAlertParams, CommonAlertParamDetail, } from '../../common/types'; +import { appendMetricbeatIndex } from '../lib/alerts/append_mb_index'; const RESOLVED = i18n.translate('xpack.monitoring.alerts.cpuUsage.resolved', { defaultMessage: 'resolved', @@ -137,7 +138,7 @@ export class CpuUsageAlert extends BaseAlert { uiSettings: IUiSettingsClient, availableCcs: string[] ): Promise { - let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH; + let esIndexPattern = appendMetricbeatIndex(this.config, INDEX_PATTERN_ELASTICSEARCH); if (availableCcs) { esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs); } diff --git a/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts index ed300c211215b..1db85f915d794 100644 --- a/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts @@ -69,7 +69,11 @@ describe('ElasticsearchVersionMismatchAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts index dd3b37b5755e7..362532a995f2d 100644 --- a/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts @@ -72,7 +72,11 @@ describe('KibanaVersionMismatchAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts index e2f21b34efe21..da94e4af83802 100644 --- a/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts @@ -76,7 +76,11 @@ describe('LicenseExpirationAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts index fbb4a01d5b4ed..5ed189014cc6e 100644 --- a/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts @@ -69,7 +69,11 @@ describe('LogstashVersionMismatchAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts index 4b3e3d2d6cb6d..ec2b19eb5dfae 100644 --- a/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts @@ -82,7 +82,11 @@ describe('NodesChangedAlert', () => { }); const monitoringCluster = null; const config = { - ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + ui: { + ccs: { enabled: true }, + container: { elasticsearch: { enabled: false } }, + metricbeat: { index: 'metricbeat-*' }, + }, }; const kibanaUrl = 'http://localhost:5601'; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/append_mb_index.ts b/x-pack/plugins/monitoring/server/lib/alerts/append_mb_index.ts new file mode 100644 index 0000000000000..683a0dfeccb1f --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/append_mb_index.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { MonitoringConfig } from '../../config'; + +export function appendMetricbeatIndex(config: MonitoringConfig, indexPattern: string) { + return `${indexPattern},${config.ui.metricbeat.index}`; +} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/get_ccs_index_pattern.ts b/x-pack/plugins/monitoring/server/lib/alerts/get_ccs_index_pattern.ts index 7fdbc79685f7c..1907d2b4b3401 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/get_ccs_index_pattern.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/get_ccs_index_pattern.ts @@ -4,10 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ export function getCcsIndexPattern(indexPattern: string, remotes: string[]): string { - return `${indexPattern},${indexPattern - .split(',') - .map((pattern) => { - return remotes.map((remoteName) => `${remoteName}:${pattern}`).join(','); - }) - .join(',')}`; + const patternsToAdd = []; + for (const index of indexPattern.split(',')) { + for (const remote of remotes) { + patternsToAdd.push(`${remote}:${index}`); + } + } + return [...indexPattern.split(','), ...patternsToAdd].join(','); } diff --git a/x-pack/plugins/monitoring/server/lib/ccs_utils.js b/x-pack/plugins/monitoring/server/lib/ccs_utils.js index bef07124fb430..96910dd86a94d 100644 --- a/x-pack/plugins/monitoring/server/lib/ccs_utils.js +++ b/x-pack/plugins/monitoring/server/lib/ccs_utils.js @@ -13,7 +13,7 @@ export function appendMetricbeatIndex(config, indexPattern) { if (isFunction(config.get)) { mbIndex = config.get('monitoring.ui.metricbeat.index'); } else { - mbIndex = get(config, 'monitoring.ui.metricbeat.index'); + mbIndex = get(config, 'ui.metricbeat.index'); } const newIndexPattern = `${indexPattern},${mbIndex}`;