From fd32b25976b559131eaf604b6a204c7590e721a5 Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Fri, 11 Oct 2019 09:20:44 -0400 Subject: [PATCH 1/2] [Monitoring] New platform migration - Server shim (#46507) * WIP * More NP refactoring * Hook up infra postInit logic * Fix broken tests * Cleanup --- x-pack/legacy/plugins/monitoring/index.js | 65 ++++++++- x-pack/legacy/plugins/monitoring/init.js | 123 ---------------- .../es_client/__tests__/instantiate_client.js | 32 ++--- .../server/es_client/instantiate_client.js | 20 +-- .../server/init_monitoring_xpack_info.js | 11 +- .../__tests__/bulk_uploader.js | 37 ++--- .../server/kibana_monitoring/bulk_uploader.js | 20 +-- .../collectors/get_kibana_usage_collector.js | 5 +- .../collectors/get_ops_stats_collector.js | 31 ++-- .../collectors/get_settings_collector.js | 5 +- .../collectors/ops_buffer/ops_buffer.js | 8 +- .../server/kibana_monitoring/init.js | 10 +- .../lib/get_kibana_info_for_stats.js | 9 +- .../server/lib/logs/init_infra_source.js | 5 +- .../plugins/monitoring/server/plugin.js | 135 ++++++++++++++++++ .../server/routes/api/v1/settings.js | 7 +- 16 files changed, 293 insertions(+), 230 deletions(-) delete mode 100644 x-pack/legacy/plugins/monitoring/init.js create mode 100644 x-pack/legacy/plugins/monitoring/server/plugin.js diff --git a/x-pack/legacy/plugins/monitoring/index.js b/x-pack/legacy/plugins/monitoring/index.js index 95dd0c1f33077..97046bfb7d5b4 100644 --- a/x-pack/legacy/plugins/monitoring/index.js +++ b/x-pack/legacy/plugins/monitoring/index.js @@ -5,10 +5,11 @@ */ import { resolve } from 'path'; -import { init, postInit } from './init'; import { config } from './config'; import { deprecations } from './deprecations'; import { getUiExports } from './ui_exports'; +import { Plugin } from './server/plugin'; +import { initInfraSource } from './server/lib/logs/init_infra_source'; /** * Invokes plugin modules to instantiate the Monitoring plugin for Kibana @@ -20,11 +21,69 @@ export const monitoring = (kibana) => new kibana.Plugin({ id: 'monitoring', configPrefix: 'xpack.monitoring', publicDir: resolve(__dirname, 'public'), - init(server, _options) { init(this, server); }, + init(server, _options) { + const configs = [ + 'xpack.monitoring.ui.enabled', + 'xpack.monitoring.kibana.collection.enabled', + 'xpack.monitoring.max_bucket_size', + 'xpack.monitoring.min_interval_seconds', + 'kibana.index', + 'xpack.monitoring.show_license_expiration', + 'xpack.monitoring.ui.container.elasticsearch.enabled', + 'xpack.monitoring.ui.container.logstash.enabled', + 'xpack.monitoring.tests.cloud_detector.enabled', + 'xpack.monitoring.kibana.collection.interval', + 'xpack.monitoring.elasticsearch.hosts', + 'xpack.monitoring.elasticsearch', + 'xpack.monitoring.xpack_api_polling_frequency_millis', + 'server.uuid', + 'server.name', + 'server.host', + 'server.port', + 'xpack.monitoring.cluster_alerts.email_notifications.enabled', + 'xpack.monitoring.cluster_alerts.email_notifications.email_address', + 'xpack.monitoring.ccs.enabled', + 'xpack.monitoring.elasticsearch.logFetchCount' + ]; + + const serverConfig = server.config(); + const serverFacade = { + config: () => ({ + get: key => { + if (configs.includes(key)) { + return serverConfig.get(key); + } + throw `Unknown key '${key}'`; + } + }), + usage: { + collectorSet: server.usage.collectorSet + }, + injectUiAppVars: server.injectUiAppVars, + log: (...args) => server.log(...args), + getOSInfo: server.getOSInfo, + events: { + on: (...args) => server.events.on(...args) + }, + expose: (...args) => server.expose(...args), + route: (...args) => server.route(...args), + _hapi: server, + _kbnServer: this.kbnServer + }; + + const plugins = { + xpack_main: server.plugins.xpack_main, + elasticsearch: server.plugins.elasticsearch, + infra: server.plugins.infra, + }; + + new Plugin().setup(serverFacade, plugins); + }, config, deprecations, uiExports: getUiExports(), postInit(server) { - postInit(server); + const serverConfig = server.config(); + initInfraSource(serverConfig, server.plugins.infra); }, }); diff --git a/x-pack/legacy/plugins/monitoring/init.js b/x-pack/legacy/plugins/monitoring/init.js deleted file mode 100644 index 5350f1f9522b8..0000000000000 --- a/x-pack/legacy/plugins/monitoring/init.js +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG } from './common/constants'; -import { requireUIRoutes } from './server/routes'; -import { instantiateClient } from './server/es_client/instantiate_client'; -import { initMonitoringXpackInfo } from './server/init_monitoring_xpack_info'; -import { initBulkUploader } from './server/kibana_monitoring'; -import { - getKibanaUsageCollector, - getOpsStatsCollector, - getSettingsCollector, -} from './server/kibana_monitoring/collectors'; -import { initInfraSource } from './server/lib/logs/init_infra_source'; - -/** - * Initialize the Kibana Monitoring plugin by starting up asynchronous server tasks - * - [1] instantiation of an elasticsearch-js client exposed as a server plugin object - * - [2] start monitoring cluster x-pack license and features check - * - [3] webserver route handling - * - [4] start the internal monitoring collector/bulk uploader - * - [5] expose the monitoring collector object for other plugins to register with - * @param monitoringPlugin {Object} Monitoring UI plugin - * @param server {Object} HapiJS server instance - */ -export const init = (monitoringPlugin, server) => { - const kbnServer = monitoringPlugin.kbnServer; - const config = server.config(); - const { collectorSet } = server.usage; - /* - * Register collector objects for stats to show up in the APIs - */ - collectorSet.register(getOpsStatsCollector(server, kbnServer)); - collectorSet.register(getKibanaUsageCollector(server)); - collectorSet.register(getSettingsCollector(server, kbnServer)); - - /* - * Instantiate and start the internal background task that calls collector - * fetch methods and uploads to the ES monitoring bulk endpoint - */ - const xpackMainPlugin = server.plugins.xpack_main; - xpackMainPlugin.status.once('green', async () => { // first time xpack_main turns green - /* - * End-user-facing services - */ - const uiEnabled = config.get('xpack.monitoring.ui.enabled'); - - if (uiEnabled) { - await instantiateClient(server); // Instantiate the dedicated ES client - await initMonitoringXpackInfo(server); // Route handlers depend on this for xpackInfo - await requireUIRoutes(server); - } - }); - - xpackMainPlugin.registerFeature({ - id: 'monitoring', - name: i18n.translate('xpack.monitoring.featureRegistry.monitoringFeatureName', { - defaultMessage: 'Stack Monitoring', - }), - icon: 'monitoringApp', - navLinkId: 'monitoring', - app: ['monitoring', 'kibana'], - catalogue: ['monitoring'], - privileges: {}, - reserved: { - privilege: { - savedObject: { - all: [], - read: [] - }, - ui: [], - }, - description: i18n.translate('xpack.monitoring.feature.reserved.description', { - defaultMessage: 'To grant users access, you should also assign the monitoring_user role.' - }) - } - }); - - const bulkUploader = initBulkUploader(kbnServer, server); - const kibanaCollectionEnabled = config.get('xpack.monitoring.kibana.collection.enabled'); - const { info: xpackMainInfo } = xpackMainPlugin; - - if (kibanaCollectionEnabled) { - /* - * Bulk uploading of Kibana stats - */ - xpackMainInfo.onLicenseInfoChange(() => { - // use updated xpack license info to start/stop bulk upload - const mainMonitoring = xpackMainInfo.feature('monitoring'); - const monitoringBulkEnabled = mainMonitoring && mainMonitoring.isAvailable() && mainMonitoring.isEnabled(); - if (monitoringBulkEnabled) { - bulkUploader.start(collectorSet); - } else { - bulkUploader.handleNotEnabled(); - } - }); - } else if (!kibanaCollectionEnabled) { - server.log( - ['info', LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG], - 'Internal collection for Kibana monitoring is disabled per configuration.' - ); - } - - server.injectUiAppVars('monitoring', (server) => { - const config = server.config(); - return { - maxBucketSize: config.get('xpack.monitoring.max_bucket_size'), - minIntervalSeconds: config.get('xpack.monitoring.min_interval_seconds'), - kbnIndex: config.get('kibana.index'), - showLicenseExpiration: config.get('xpack.monitoring.show_license_expiration'), - showCgroupMetricsElasticsearch: config.get('xpack.monitoring.ui.container.elasticsearch.enabled'), - showCgroupMetricsLogstash: config.get('xpack.monitoring.ui.container.logstash.enabled') // Note, not currently used, but see https://github.com/elastic/x-pack-kibana/issues/1559 part 2 - }; - }); -}; - -export const postInit = server => { - initInfraSource(server); -}; diff --git a/x-pack/legacy/plugins/monitoring/server/es_client/__tests__/instantiate_client.js b/x-pack/legacy/plugins/monitoring/server/es_client/__tests__/instantiate_client.js index bc2afc7c25e19..853f1f5bcdc65 100644 --- a/x-pack/legacy/plugins/monitoring/server/es_client/__tests__/instantiate_client.js +++ b/x-pack/legacy/plugins/monitoring/server/es_client/__tests__/instantiate_client.js @@ -26,22 +26,18 @@ function getMockServerFromConnectionUrl(monitoringClusterUrl) { }, }; - const config = () => { - return { - get: (path) => { return get(server, path); }, - set: noop - }; + const config = { + get: (path) => { return get(server, path); }, + set: noop }; return { config, - plugins: { - elasticsearch: { - getCluster: sinon.stub().withArgs('admin').returns({ - config: sinon.stub().returns(server.elasticsearch) - }), - createCluster: sinon.stub(), - } + elasticsearchPlugin: { + getCluster: sinon.stub().withArgs('admin').returns({ + config: sinon.stub().returns(server.elasticsearch) + }), + createCluster: sinon.stub(), }, events: { on: noop, @@ -81,7 +77,7 @@ describe('Instantiate Client', () => { exposeClient(server); - const createCluster = server.plugins.elasticsearch.createCluster; + const createCluster = server.elasticsearchPlugin.createCluster; const createClusterCall = createCluster.getCall(0); sinon.assert.calledOnce(createCluster); @@ -94,7 +90,7 @@ describe('Instantiate Client', () => { exposeClient(server); - const createCluster = server.plugins.elasticsearch.createCluster; + const createCluster = server.elasticsearchPlugin.createCluster; const createClusterCall = createCluster.getCall(0); sinon.assert.calledOnce(createCluster); @@ -110,7 +106,7 @@ describe('Instantiate Client', () => { const server = getMockServerFromConnectionUrl(null); // pass null for URL to create the client using prod config exposeClient(server); - const createCluster = server.plugins.elasticsearch.createCluster; + const createCluster = server.elasticsearchPlugin.createCluster; const createClusterCall = createCluster.getCall(0); const createClientOptions = createClusterCall.args[1]; @@ -125,7 +121,7 @@ describe('Instantiate Client', () => { const server = getMockServerFromConnectionUrl('http://monitoring-cluster.test:9200'); exposeClient(server); - const createCluster = server.plugins.elasticsearch.createCluster; + const createCluster = server.elasticsearchPlugin.createCluster; const createClusterCall = createCluster.getCall(0); const createClientOptions = createClusterCall.args[1]; @@ -140,12 +136,12 @@ describe('Instantiate Client', () => { describe('hasMonitoringCluster', () => { it('returns true if monitoring is configured', () => { const server = getMockServerFromConnectionUrl('http://monitoring-cluster.test:9200'); // pass null for URL to create the client using prod config - expect(hasMonitoringCluster(server)).to.be(true); + expect(hasMonitoringCluster(server.config)).to.be(true); }); it('returns false if monitoring is not configured', () => { const server = getMockServerFromConnectionUrl(null); - expect(hasMonitoringCluster(server)).to.be(false); + expect(hasMonitoringCluster(server.config)).to.be(false); }); }); }); diff --git a/x-pack/legacy/plugins/monitoring/server/es_client/instantiate_client.js b/x-pack/legacy/plugins/monitoring/server/es_client/instantiate_client.js index a4930d9d1dc21..b6cf9d07ba1e6 100644 --- a/x-pack/legacy/plugins/monitoring/server/es_client/instantiate_client.js +++ b/x-pack/legacy/plugins/monitoring/server/es_client/instantiate_client.js @@ -14,21 +14,21 @@ import { LOGGING_TAG } from '../../common/constants'; * Kibana itself is connected to a production cluster. */ -export function exposeClient(server) { - const config = hasMonitoringCluster(server) ? server.config().get('xpack.monitoring.elasticsearch') : {}; - const cluster = server.plugins.elasticsearch.createCluster('monitoring', { - ...config, +export function exposeClient({ config, events, log, elasticsearchPlugin }) { + const elasticsearchConfig = hasMonitoringCluster(config) ? config.get('xpack.monitoring.elasticsearch') : {}; + const cluster = elasticsearchPlugin.createCluster('monitoring', { + ...elasticsearchConfig, plugins: [monitoringBulk], - logQueries: Boolean(config.logQueries), + logQueries: Boolean(elasticsearchConfig.logQueries), }); - server.events.on('stop', bindKey(cluster, 'close')); - const configSource = hasMonitoringCluster(server) ? 'monitoring' : 'production'; - server.log([LOGGING_TAG, 'es-client'], `config sourced from: ${configSource} cluster`); + events.on('stop', bindKey(cluster, 'close')); + const configSource = hasMonitoringCluster(config) ? 'monitoring' : 'production'; + log([LOGGING_TAG, 'es-client'], `config sourced from: ${configSource} cluster`); } -export function hasMonitoringCluster(server) { - const hosts = server.config().get('xpack.monitoring.elasticsearch.hosts'); +export function hasMonitoringCluster(config) { + const hosts = config.get('xpack.monitoring.elasticsearch.hosts'); return Boolean(hosts && hosts.length); } diff --git a/x-pack/legacy/plugins/monitoring/server/init_monitoring_xpack_info.js b/x-pack/legacy/plugins/monitoring/server/init_monitoring_xpack_info.js index 39d078c800925..b7ab2c017aeef 100644 --- a/x-pack/legacy/plugins/monitoring/server/init_monitoring_xpack_info.js +++ b/x-pack/legacy/plugins/monitoring/server/init_monitoring_xpack_info.js @@ -11,19 +11,18 @@ import { LOGGING_TAG } from '../common/constants'; /* * Expose xpackInfo for the Monitoring cluster as server.plugins.monitoring.info */ -export const initMonitoringXpackInfo = async server => { - const config = server.config(); - const xpackInfo = hasMonitoringCluster(server) ? server.plugins.xpack_main.createXPackInfo({ +export const initMonitoringXpackInfo = async ({ config, xpackMainPlugin, expose, log }) => { + const xpackInfo = hasMonitoringCluster(config) ? xpackMainPlugin.createXPackInfo({ clusterSource: 'monitoring', pollFrequencyInMillis: config.get('xpack.monitoring.xpack_api_polling_frequency_millis') - }) : server.plugins.xpack_main.info; + }) : xpackMainPlugin.info; xpackInfo.feature('monitoring').registerLicenseCheckResultsGenerator(checkLicenseGenerator); - server.expose('info', xpackInfo); + expose('info', xpackInfo); // check if X-Pack is installed on Monitoring Cluster const xpackInfoTest = await xpackInfo.refreshNow(); if (!xpackInfoTest.isAvailable()) { - server.log([LOGGING_TAG, 'warning'], `X-Pack Monitoring Cluster Alerts will not be available: ${xpackInfoTest.unavailableReason()}`); + log([LOGGING_TAG, 'warning'], `X-Pack Monitoring Cluster Alerts will not be available: ${xpackInfoTest.unavailableReason()}`); } }; diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js index 8778a460a2499..d494963c4492d 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js @@ -49,14 +49,12 @@ describe('BulkUploader', () => { server = { log: sinon.spy(), - plugins: { - xpack_main: { - telemetryCollectionInterval: 3000, - }, - elasticsearch: { - createCluster: () => cluster, - getCluster: () => cluster, - }, + xpackMainPlugin: { + telemetryCollectionInterval: 3000, + }, + elasticsearchPlugin: { + createCluster: () => cluster, + getCluster: () => cluster, }, usage: {}, }; @@ -72,7 +70,8 @@ describe('BulkUploader', () => { } ]); - const uploader = new BulkUploader(server, { + const uploader = new BulkUploader({ + ...server, interval: FETCH_INTERVAL }); @@ -117,9 +116,7 @@ describe('BulkUploader', () => { } ]); - const uploader = new BulkUploader(server, { - interval: FETCH_INTERVAL - }); + const uploader = new BulkUploader({ ...server, interval: FETCH_INTERVAL }); uploader.start(collectors); @@ -154,9 +151,7 @@ describe('BulkUploader', () => { formatForBulkUpload: result => result } ]); - const uploader = new BulkUploader(server, { - interval: FETCH_INTERVAL - }); + const uploader = new BulkUploader({ ...server, interval: FETCH_INTERVAL }); uploader.start(collectors); @@ -200,9 +195,7 @@ describe('BulkUploader', () => { } ]); - const uploader = new BulkUploader(server, { - interval: FETCH_INTERVAL - }); + const uploader = new BulkUploader({ ...server, interval: FETCH_INTERVAL }); uploader._lastFetchUsageTime = Date.now(); uploader.start(collectors); @@ -227,9 +220,7 @@ describe('BulkUploader', () => { } ]); - const uploader = new BulkUploader(server, { - interval: FETCH_INTERVAL - }); + const uploader = new BulkUploader({ ...server, interval: FETCH_INTERVAL }); uploader._onPayload = async () => ({ took: 0, ignored: true, errors: false }); @@ -260,9 +251,7 @@ describe('BulkUploader', () => { } ]); - const uploader = new BulkUploader(server, { - interval: FETCH_INTERVAL - }); + const uploader = new BulkUploader({ ...server, interval: FETCH_INTERVAL }); uploader._lastFetchUsageTime = Date.now() - uploader._usageInterval; uploader.start(collectors); diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/bulk_uploader.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/bulk_uploader.js index a8ff17c52e905..015af8f3d633a 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/bulk_uploader.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/bulk_uploader.js @@ -36,7 +36,7 @@ const LOGGING_TAGS = [LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG]; * @param {Object} xpackInfo server.plugins.xpack_main.info object */ export class BulkUploader { - constructor(server, { kbnServer, interval }) { + constructor({ config, log, interval, xpackMainPlugin, elasticsearchPlugin, kbnServerStatus, kbnServerVersion }) { if (typeof interval !== 'number') { throw new Error('interval number of milliseconds is required'); } @@ -44,20 +44,24 @@ export class BulkUploader { this._timer = null; this._interval = interval; this._lastFetchUsageTime = null; - this._usageInterval = server.plugins.xpack_main.telemetryCollectionInterval; + this._usageInterval = xpackMainPlugin.telemetryCollectionInterval; this._log = { - debug: message => server.log(['debug', ...LOGGING_TAGS], message), - info: message => server.log(['info', ...LOGGING_TAGS], message), - warn: message => server.log(['warning', ...LOGGING_TAGS], message) + debug: message => log(['debug', ...LOGGING_TAGS], message), + info: message => log(['info', ...LOGGING_TAGS], message), + warn: message => log(['warning', ...LOGGING_TAGS], message) }; - this._cluster = server.plugins.elasticsearch.createCluster('admin', { + this._cluster = elasticsearchPlugin.createCluster('admin', { plugins: [monitoringBulk], }); - this._callClusterWithInternalUser = callClusterFactory(server).getCallClusterInternal(); - this._getKibanaInfoForStats = () => getKibanaInfoForStats(server, kbnServer); + this._callClusterWithInternalUser = callClusterFactory({ plugins: { elasticsearch: elasticsearchPlugin } }).getCallClusterInternal(); + this._getKibanaInfoForStats = () => getKibanaInfoForStats({ + kbnServerStatus, + kbnServerVersion, + config + }); } /* diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_kibana_usage_collector.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_kibana_usage_collector.js index 96d0c98cf3f05..25efc63fafb5d 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_kibana_usage_collector.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_kibana_usage_collector.js @@ -19,13 +19,12 @@ const TYPES = [ /** * Fetches saved object counts by querying the .kibana index */ -export function getKibanaUsageCollector(server) { - const { collectorSet } = server.usage; +export function getKibanaUsageCollector({ collectorSet, config }) { return collectorSet.makeUsageCollector({ type: KIBANA_USAGE_TYPE, isReady: () => true, async fetch(callCluster) { - const index = server.config().get('kibana.index'); + const index = config.get('kibana.index'); const savedObjectCountSearchParams = { index, ignoreUnavailable: true, diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_ops_stats_collector.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_ops_stats_collector.js index b5116de7f3d49..f1f47761d9f0c 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_ops_stats_collector.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_ops_stats_collector.js @@ -16,11 +16,11 @@ import { cloneDeep } from 'lodash'; let bufferHadEvents = false; class OpsMonitor { - constructor(server, buffer, interval) { + constructor(hapiServer, buffer, interval) { this._buffer = buffer; this._interval = interval; - this._oppsy = new Oppsy(server); - this._server = server; + this._oppsy = new Oppsy(hapiServer); + this._server = hapiServer; } start = () => { @@ -49,35 +49,42 @@ class OpsMonitor { /* * Initialize a collector for Kibana Ops Stats */ -export function getOpsStatsCollector(server, kbnServer) { - const buffer = opsBuffer(server); - const interval = kbnServer.config.get('ops.interval'); - const opsMonitor = new OpsMonitor(server, buffer, interval); +export function getOpsStatsCollector({ + elasticsearchPlugin, + kbnServerConfig, + log, + config, + getOSInfo, + hapiServer, + collectorSet +}) { + const buffer = opsBuffer({ log, config, getOSInfo }); + const interval = kbnServerConfig.get('ops.interval'); + const opsMonitor = new OpsMonitor(hapiServer, buffer, interval); /* Handle stopping / restarting the event listener if Elasticsearch stops and restarts * NOTE it is possible for the plugin status to go from red to red and * trigger handlers twice */ - server.plugins.elasticsearch.status.on('red', opsMonitor.stop); - server.plugins.elasticsearch.status.on('green', opsMonitor.start); + elasticsearchPlugin.status.on('red', opsMonitor.stop); + elasticsearchPlugin.status.on('green', opsMonitor.start); // `process` is a NodeJS global, and is always available without using require/import process.on('SIGHUP', () => { - server.log( + log( ['info', LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG], 'Re-initializing Kibana Monitoring due to SIGHUP' ); setTimeout(() => { opsMonitor.stop(); opsMonitor.start(); - server.log( + log( ['info', LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG], 'Re-initializing Kibana Monitoring due to SIGHUP' ); }, 5 * 1000); // wait 5 seconds to avoid race condition with reloading logging configuration }); - const { collectorSet } = server.usage; return collectorSet.makeStatsCollector({ type: KIBANA_STATS_TYPE_MONITORING, init: opsMonitor.start, diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.js index 11129fe94b579..315e76433ef4a 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.js @@ -80,10 +80,7 @@ export async function checkForEmailValue( } } -export function getSettingsCollector(server) { - const config = server.config(); - const { collectorSet } = server.usage; - +export function getSettingsCollector({ config, collectorSet }) { return collectorSet.makeStatsCollector({ type: KIBANA_SETTINGS_TYPE, isReady: () => true, diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js index 457213c784ad6..d80ecd575266a 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js @@ -13,11 +13,11 @@ import { CloudDetector } from '../../../cloud'; * @param {Object} server HapiJS server instance * @return {Object} the revealed `push` and `flush` modules */ -export function opsBuffer(server) { +export function opsBuffer({ config, log, getOSInfo }) { // determine the cloud service in the background const cloudDetector = new CloudDetector(); - if(server.config().get('xpack.monitoring.tests.cloud_detector.enabled')) { + if(config.get('xpack.monitoring.tests.cloud_detector.enabled')) { cloudDetector.detectCloudService(); } @@ -26,7 +26,7 @@ export function opsBuffer(server) { return { push(event) { eventRoller.addEvent(event); - server.log(['debug', LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG], 'Received Kibana Ops event data'); + log(['debug', LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG], 'Received Kibana Ops event data'); }, hasEvents() { @@ -44,7 +44,7 @@ export function opsBuffer(server) { if (eventRollup && eventRollup.os) { eventRollup.os = { ...eventRollup.os, - ...(await server.getOSInfo()) + ...(await getOSInfo()) }; } diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/init.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/init.js index 64242d1fe4f34..bf79ddc210902 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/init.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/init.js @@ -15,11 +15,11 @@ import { BulkUploader } from './bulk_uploader'; * @param {Object} kbnServer manager of Kibana services - see `src/legacy/server/kbn_server` in Kibana core * @param {Object} server HapiJS server instance */ -export function initBulkUploader(kbnServer, server) { - const config = server.config(); +export function initBulkUploader({ config, ...params }) { const interval = config.get('xpack.monitoring.kibana.collection.interval'); - return new BulkUploader(server, { - kbnServer, - interval + return new BulkUploader({ + interval, + config, + ...params, }); } diff --git a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/lib/get_kibana_info_for_stats.js b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/lib/get_kibana_info_for_stats.js index 3928caeaf3ab9..2bc5839ba6553 100644 --- a/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/lib/get_kibana_info_for_stats.js +++ b/x-pack/legacy/plugins/monitoring/server/kibana_monitoring/lib/get_kibana_info_for_stats.js @@ -17,9 +17,8 @@ const snapshotRegex = /-snapshot/i; * @param {String} host Kibana host * @return {Object} The object containing a "kibana" field and source instance details. */ -export function getKibanaInfoForStats(server, kbnServer) { - const config = server.config(); - const status = kbnServer.status.toJSON(); +export function getKibanaInfoForStats({ kbnServerStatus, kbnServerVersion, config }) { + const status = kbnServerStatus.toJSON(); return { uuid: config.get('server.uuid'), @@ -27,8 +26,8 @@ export function getKibanaInfoForStats(server, kbnServer) { index: config.get('kibana.index'), host: config.get('server.host'), transport_address: `${config.get('server.host')}:${config.get('server.port')}`, - version: kbnServer.version.replace(snapshotRegex, ''), - snapshot: snapshotRegex.test(kbnServer.version), + version: kbnServerVersion.replace(snapshotRegex, ''), + snapshot: snapshotRegex.test(kbnServerVersion), status: get(status, 'overall.state') }; } diff --git a/x-pack/legacy/plugins/monitoring/server/lib/logs/init_infra_source.js b/x-pack/legacy/plugins/monitoring/server/lib/logs/init_infra_source.js index 4059679e452d7..7f506df7b2ee1 100644 --- a/x-pack/legacy/plugins/monitoring/server/lib/logs/init_infra_source.js +++ b/x-pack/legacy/plugins/monitoring/server/lib/logs/init_infra_source.js @@ -7,11 +7,8 @@ import { prefixIndexPattern } from '../ccs_utils'; import { INDEX_PATTERN_FILEBEAT, INFRA_SOURCE_ID } from '../../../common/constants'; -export const initInfraSource = server => { - const infraPlugin = server.plugins.infra; - +export const initInfraSource = (config, infraPlugin) => { if (infraPlugin) { - const config = server.config(); const filebeatIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_FILEBEAT, '*'); infraPlugin.defineInternalSourceConfiguration(INFRA_SOURCE_ID, { name: 'Elastic Stack Logs', diff --git a/x-pack/legacy/plugins/monitoring/server/plugin.js b/x-pack/legacy/plugins/monitoring/server/plugin.js new file mode 100644 index 0000000000000..349e705434bfa --- /dev/null +++ b/x-pack/legacy/plugins/monitoring/server/plugin.js @@ -0,0 +1,135 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG } from '../common/constants'; +import { requireUIRoutes } from './routes'; +import { instantiateClient } from './es_client/instantiate_client'; +import { initMonitoringXpackInfo } from './init_monitoring_xpack_info'; +import { initBulkUploader } from './kibana_monitoring'; +import { + getKibanaUsageCollector, + getOpsStatsCollector, + getSettingsCollector, +} from './kibana_monitoring/collectors'; + +export class Plugin { + setup(core, plugins) { + const kbnServer = core._kbnServer; + const config = core.config(); + const { collectorSet } = core.usage; + /* + * Register collector objects for stats to show up in the APIs + */ + collectorSet.register(getOpsStatsCollector({ + elasticsearchPlugin: plugins.elasticsearch, + kbnServerConfig: kbnServer.config, + log: core.log, + config, + getOSInfo: core.getOSInfo, + hapiServer: core._hapi, + collectorSet: core.usage.collectorSet, + })); + collectorSet.register(getKibanaUsageCollector({ collectorSet, config })); + collectorSet.register(getSettingsCollector({ collectorSet, config })); + + /* + * Instantiate and start the internal background task that calls collector + * fetch methods and uploads to the ES monitoring bulk endpoint + */ + const xpackMainPlugin = plugins.xpack_main; + xpackMainPlugin.status.once('green', async () => { // first time xpack_main turns green + /* + * End-user-facing services + */ + const uiEnabled = config.get('xpack.monitoring.ui.enabled'); + + if (uiEnabled) { + await instantiateClient({ + log: core.log, + events: core.events, + config, + elasticsearchPlugin: plugins.elasticsearch + }); // Instantiate the dedicated ES client + await initMonitoringXpackInfo({ + config, + log: core.log, + xpackMainPlugin: plugins.xpack_main, + expose: core.expose + }); // Route handlers depend on this for xpackInfo + await requireUIRoutes(core); + } + }); + + xpackMainPlugin.registerFeature({ + id: 'monitoring', + name: i18n.translate('xpack.monitoring.featureRegistry.monitoringFeatureName', { + defaultMessage: 'Stack Monitoring', + }), + icon: 'monitoringApp', + navLinkId: 'monitoring', + app: ['monitoring', 'kibana'], + catalogue: ['monitoring'], + privileges: {}, + reserved: { + privilege: { + savedObject: { + all: [], + read: [] + }, + ui: [], + }, + description: i18n.translate('xpack.monitoring.feature.reserved.description', { + defaultMessage: 'To grant users access, you should also assign the monitoring_user role.' + }) + } + }); + + const bulkUploader = initBulkUploader({ + elasticsearchPlugin: plugins.elasticsearch, + xpackMainPlugin: plugins.xpack_main, + config, + log: core.log, + kbnServerStatus: kbnServer.status, + kbnServerVersion: kbnServer.version + }); + const kibanaCollectionEnabled = config.get('xpack.monitoring.kibana.collection.enabled'); + const { info: xpackMainInfo } = xpackMainPlugin; + + if (kibanaCollectionEnabled) { + /* + * Bulk uploading of Kibana stats + */ + xpackMainInfo.onLicenseInfoChange(() => { + // use updated xpack license info to start/stop bulk upload + const mainMonitoring = xpackMainInfo.feature('monitoring'); + const monitoringBulkEnabled = mainMonitoring && mainMonitoring.isAvailable() && mainMonitoring.isEnabled(); + if (monitoringBulkEnabled) { + bulkUploader.start(collectorSet); + } else { + bulkUploader.handleNotEnabled(); + } + }); + } else if (!kibanaCollectionEnabled) { + core.log( + ['info', LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG], + 'Internal collection for Kibana monitoring is disabled per configuration.' + ); + } + + core.injectUiAppVars('monitoring', (core) => { + const config = core.config(); + return { + maxBucketSize: config.get('xpack.monitoring.max_bucket_size'), + minIntervalSeconds: config.get('xpack.monitoring.min_interval_seconds'), + kbnIndex: config.get('kibana.index'), + showLicenseExpiration: config.get('xpack.monitoring.show_license_expiration'), + showCgroupMetricsElasticsearch: config.get('xpack.monitoring.ui.container.elasticsearch.enabled'), + showCgroupMetricsLogstash: config.get('xpack.monitoring.ui.container.logstash.enabled') // Note, not currently used, but see https://github.com/elastic/x-pack-kibana/issues/1559 part 2 + }; + }); + } +} diff --git a/x-pack/legacy/plugins/xpack_main/server/routes/api/v1/settings.js b/x-pack/legacy/plugins/xpack_main/server/routes/api/v1/settings.js index 599be67e93d9a..d5753ef6f3c85 100644 --- a/x-pack/legacy/plugins/xpack_main/server/routes/api/v1/settings.js +++ b/x-pack/legacy/plugins/xpack_main/server/routes/api/v1/settings.js @@ -32,7 +32,12 @@ export function settingsRoute(server, kbnServer) { } const uuid = await getClusterUuid(callCluster); - const kibana = getKibanaInfoForStats(server, kbnServer); + const kibana = getKibanaInfoForStats({ + kbnServerStatus: kbnServer.status, + kbnServerVersion: kbnServer.version, + config: server.config() + }); + return { cluster_uuid: uuid, settings: { From 79a2b9a4301bdee1ddde28d0e714766124fe2053 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Mon, 14 Oct 2019 14:20:22 -0400 Subject: [PATCH 2/2] Add missing config for 7.x --- x-pack/legacy/plugins/monitoring/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/monitoring/index.js b/x-pack/legacy/plugins/monitoring/index.js index 97046bfb7d5b4..5526e4564d9d8 100644 --- a/x-pack/legacy/plugins/monitoring/index.js +++ b/x-pack/legacy/plugins/monitoring/index.js @@ -28,6 +28,7 @@ export const monitoring = (kibana) => new kibana.Plugin({ 'xpack.monitoring.max_bucket_size', 'xpack.monitoring.min_interval_seconds', 'kibana.index', + 'pkg.version', 'xpack.monitoring.show_license_expiration', 'xpack.monitoring.ui.container.elasticsearch.enabled', 'xpack.monitoring.ui.container.logstash.enabled', @@ -43,7 +44,7 @@ export const monitoring = (kibana) => new kibana.Plugin({ 'xpack.monitoring.cluster_alerts.email_notifications.enabled', 'xpack.monitoring.cluster_alerts.email_notifications.email_address', 'xpack.monitoring.ccs.enabled', - 'xpack.monitoring.elasticsearch.logFetchCount' + 'xpack.monitoring.elasticsearch.logFetchCount', ]; const serverConfig = server.config();