-
Notifications
You must be signed in to change notification settings - Fork 8k
/
get_logstash_stats.ts
114 lines (105 loc) · 3.9 KB
/
get_logstash_stats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* 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 { ElasticsearchClient } from '@kbn/core/server';
import {
INDEX_PATTERN_LOGSTASH,
METRICBEAT_INDEX_NAME_UNIQUE_TOKEN,
TELEMETRY_QUERY_SOURCE,
} from '../../common/constants';
import { LogstashSelfMonitoring } from './logstash_self_monitoring';
import { LogstashMetricbeatMonitoring } from './logstash_metricbeat_monitoring';
import { LogstashAgentMonitoring } from './logstash_agent_monitoring';
import {
LogstashMonitoring,
LogstashProcessOptions,
LogstashStatsByClusterUuid,
} from './logstash_monitoring';
const SELF_MONITORING: string = 'self';
const METRICBEAT_MONITORING: string = 'metricbeat';
const AGENT_MONITORING: string = 'agent';
export const logstashMonitoringInstances: { [key: string]: LogstashMonitoring } = {
self: new LogstashSelfMonitoring(),
metricbeat: new LogstashMetricbeatMonitoring(),
agent: new LogstashAgentMonitoring(),
};
/*
* Call the function for fetching and summarizing Logstash stats
* @return {Object} - Logstash stats in an object keyed by the cluster UUIDs
*/
export async function getLogstashStats(
callCluster: ElasticsearchClient,
clusterUuids: string[],
start: string,
end: string
): Promise<LogstashStatsByClusterUuid> {
const options: LogstashProcessOptions = {
clusters: {}, // the result object to be built up
allEphemeralIds: {},
allHostIds: {},
versions: {},
plugins: {},
};
const monitoringClusterInfo = await callCluster.info();
const monitoringClusterUuid: string = monitoringClusterInfo.cluster_uuid;
// figure out the monitoring methods cluster is using based on the Logstash metrics indices
// mostly single method will be resolved
// multiple monitoring methods case might be due to migration (ex: from self to metricbeat)
const monitoringMethods: string[] = await getLogstashMonitoringMethods(callCluster);
// collect all _method_ (:self, :metricbeat, :agent) metrics in a given period
for (const monitoringMethod of monitoringMethods) {
const monitoringInstance = logstashMonitoringInstances[monitoringMethod];
if (monitoringInstance) {
await monitoringInstance.collectMetrics(
callCluster,
clusterUuids,
monitoringClusterUuid,
start,
end,
options
);
}
}
return options.clusters;
}
export async function getLogstashMonitoringMethods(
callCluster: ElasticsearchClient
): Promise<string[]> {
const response = await callCluster.cat.indices(
{ index: INDEX_PATTERN_LOGSTASH, format: 'json' },
{
headers: {
'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE,
},
}
);
const monitoringMethods: string[] = [];
for (const record of response) {
if (record.index!.indexOf('monitoring-logstash-') !== -1) {
if (record.index!.indexOf(METRICBEAT_INDEX_NAME_UNIQUE_TOKEN) !== -1) {
// legacy driven metricbeat monitoring
if (!monitoringMethods.includes(METRICBEAT_MONITORING)) {
monitoringMethods.push(METRICBEAT_MONITORING);
logstashMonitoringInstances.metricbeat.setIndexPattern('legacy');
}
} else {
if (!monitoringMethods.includes(SELF_MONITORING)) {
monitoringMethods.push(SELF_MONITORING);
}
}
} else if (record.index!.indexOf('metrics-logstash.node') !== -1) {
if (!monitoringMethods.includes(AGENT_MONITORING)) {
monitoringMethods.push(AGENT_MONITORING);
}
} else if (record.index!.indexOf('metrics-logstash.stack_monitoring') !== -1) {
if (!monitoringMethods.includes(METRICBEAT_MONITORING)) {
monitoringMethods.push(METRICBEAT_MONITORING);
logstashMonitoringInstances.metricbeat.setIndexPattern('stack');
}
}
}
return monitoringMethods;
}