Skip to content

Commit 3ccd0d5

Browse files
[Monitoring] Fixed internal monitoring check (#79241) (#81955)
* fixed internal monitoring check * Added range filter * Added single vs ccs condtion * Fixed spelling * Passing global state ccs Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> # Conflicts: # x-pack/plugins/monitoring/public/services/clusters.js
1 parent fa0e8b7 commit 3ccd0d5

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

x-pack/plugins/monitoring/server/lib/ccs_utils.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66
import { isFunction, get } from 'lodash';
77

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

19-
const newIndexPattern = `${indexPattern},${mbIndex}`;
20-
return newIndexPattern;
22+
return `${indexPattern},${mbIndex}`;
2123
}
2224

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

4446
if (!ccsEnabled || !ccs) {
45-
return appendMetricbeatIndex(config, indexPattern);
47+
return appendMetricbeatIndex(config, indexPattern, monitoringIndicesOnly);
4648
}
4749

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

5153
// if a wildcard is used, then we also want to search the local indices
5254
if (ccs === '*') {
53-
return appendMetricbeatIndex(config, `${prefixedPattern},${indexPattern}`);
55+
return appendMetricbeatIndex(
56+
config,
57+
`${prefixedPattern},${indexPattern}`,
58+
monitoringIndicesOnly
59+
);
5460
}
5561

56-
return appendMetricbeatIndex(config, prefixedPattern);
62+
return appendMetricbeatIndex(config, prefixedPattern, monitoringIndicesOnly);
5763
}
5864

5965
/**

x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,34 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { schema } from '@kbn/config-schema';
78
import { RequestHandlerContext } from 'kibana/server';
9+
import {
10+
INDEX_PATTERN_ELASTICSEARCH,
11+
INDEX_PATTERN_KIBANA,
12+
INDEX_PATTERN_LOGSTASH,
13+
} from '../../../../../../common/constants';
814
// @ts-ignore
9-
import { getIndexPatterns } from '../../../../../lib/cluster/get_index_patterns';
15+
import { prefixIndexPattern } from '../../../../../lib/ccs_utils';
1016
// @ts-ignore
1117
import { handleError } from '../../../../../lib/errors';
1218
import { RouteDependencies } from '../../../../../types';
1319

1420
const queryBody = {
1521
size: 0,
22+
query: {
23+
bool: {
24+
must: [
25+
{
26+
range: {
27+
timestamp: {
28+
gte: 'now-12h',
29+
},
30+
},
31+
},
32+
],
33+
},
34+
},
1635
aggs: {
1736
types: {
1837
terms: {
@@ -49,20 +68,31 @@ const checkLatestMonitoringIsLegacy = async (context: RequestHandlerContext, ind
4968
return counts;
5069
};
5170

52-
export function internalMonitoringCheckRoute(server: unknown, npRoute: RouteDependencies) {
53-
npRoute.router.get(
71+
export function internalMonitoringCheckRoute(
72+
server: { config: () => unknown },
73+
npRoute: RouteDependencies
74+
) {
75+
npRoute.router.post(
5476
{
5577
path: '/api/monitoring/v1/elasticsearch_settings/check/internal_monitoring',
56-
validate: false,
78+
validate: {
79+
body: schema.object({
80+
ccs: schema.maybe(schema.string()),
81+
}),
82+
},
5783
},
58-
async (context, _request, response) => {
84+
async (context, request, response) => {
5985
try {
6086
const typeCount = {
6187
legacy_indices: 0,
6288
mb_indices: 0,
6389
};
6490

65-
const { esIndexPattern, kbnIndexPattern, lsIndexPattern } = getIndexPatterns(server);
91+
const config = server.config();
92+
const { ccs } = request.body;
93+
const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs, true);
94+
const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs, true);
95+
const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs, true);
6696
const indexCounts = await Promise.all([
6797
checkLatestMonitoringIsLegacy(context, esIndexPattern),
6898
checkLatestMonitoringIsLegacy(context, kbnIndexPattern),

0 commit comments

Comments
 (0)