Hot replicas modifying

This commit is contained in:
Jesús Ángel 2018-08-23 15:37:37 +02:00
parent 27b6b78f8d
commit 8516798b52
4 changed files with 89 additions and 37 deletions

View File

@ -17,6 +17,7 @@ import packageJSON from '../package.json'
import kibana_template from './integration-files/kibana-template'
import getConfiguration from './lib/get-configuration'
import defaultExt from './lib/default-ext'
import { BuildBody } from './lib/replicas-shards-helper'
export default (server, options) => {
const blueWazuh = colors.blue('wazuh');
@ -150,25 +151,9 @@ export default (server, options) => {
// Save Wazuh App setup
const saveConfiguration = async () => {
try{
const shards = configurationFile && typeof configurationFile["wazuh-version.shards"] !== 'undefined' ?
configurationFile["wazuh-version.shards"] :
1;
const shardConfiguration = BuildBody(configurationFile, 'wazuh-version', 1, 1)
const replicas = configurationFile && typeof configurationFile["wazuh-version.replicas"] !== 'undefined' ?
configurationFile["wazuh-version.replicas"] :
1;
const shard_configuration = {
settings: {
index: {
number_of_shards : shards,
number_of_replicas: replicas
}
}
};
await wzWrapper.createWazuhVersionIndex(shard_configuration);
await wzWrapper.createWazuhVersionIndex(shardConfiguration);
const commonDate = new Date().toISOString();
@ -264,26 +249,14 @@ export default (server, options) => {
const result = await wzWrapper.checkIfIndexExists('.wazuh');
const shardConfiguration = BuildBody(configurationFile, 'wazuh', 1, 1);
if (!result) {
const shards = configurationFile && typeof configurationFile["wazuh.shards"] !== 'undefined' ?
configurationFile["wazuh.shards"] :
1;
const replicas = configurationFile && typeof configurationFile["wazuh.replicas"] !== 'undefined' ?
configurationFile["wazuh.replicas"] :
1;
let configuration = {
"settings": {
"index": {
"number_of_shards": shards,
"number_of_replicas": replicas
}
}
};
try{
await wzWrapper.createWazuhIndex(configuration);
await wzWrapper.createWazuhIndex(shardConfiguration);
log('[initialize][checkWazuhIndex]', 'Index .wazuh created.','info')
server.log([blueWazuh, 'initialize', 'info'], 'Index .wazuh created.');
@ -293,7 +266,8 @@ export default (server, options) => {
}
} else {
await wzWrapper.updateIndexSettings('.wazuh',shardConfiguration)
await checkAPIEntriesExtensions();
// The .wazuh index exists, we now proceed to check whether it's from an older version
@ -324,6 +298,8 @@ export default (server, options) => {
try{
await wzWrapper.getWazuhVersionIndex();
const shardConfiguration = BuildBody(configurationFile, 'wazuh-version', 1, 1)
await wzWrapper.updateIndexSettings('.wazuh-version', shardConfiguration)
} catch (error) {
log('[initialize][checkWazuhVersionIndex]','.wazuh-version document does not exist. Initializating configuration...','info');
server.log([blueWazuh, 'initialize', 'info'], '.wazuh-version document does not exist. Initializating configuration...');

View File

@ -824,5 +824,30 @@ export default class ElasticWrapper {
}
}
/**
* Updates replicas and few other settings (if they are given) for specific index
* @param {string} index Target index name
* @param {object} configuration Settings to be updated
*/
async updateIndexSettings(index, configuration) {
try {
if(!index) throw new Error('No valid index given');
if(!configuration) throw new Error('No valid configuration given');
// Number of shards is not dynamic so delete that setting if it's given
if(configuration.settings && configuration.settings.index && configuration.settings.index.number_of_shards) {
delete configuration.settings.index.number_of_shards;
}
const data = await this.elasticRequest.callWithInternalUser('indices.putSettings', {
index: index,
body: configuration
})
return data;
} catch (error) {
return Promise.reject(error);
}
}
}

View File

@ -0,0 +1,43 @@
/*
* Wazuh app - Elastic wrapper helper
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
/**
* Returns well formatted object to set shards and replicas when creating/updating indices.
* @param {object} file Parsed content from config.yml file
* @param {string} indexName Target index name
* @param {number} defaultShards Default shards value if missing in configuration
* @param {number} defaulReplicas Default replicas value if missing in configuration
*/
export function BuildBody(file, indexName, defaultShards = 5, defaulReplicas = 1) {
if(indexName) {
const shards = file && typeof file[`${indexName}.shards`] !== 'undefined' ?
file[`${indexName}.shards`] :
defaultShards;
const replicas = file && typeof file[`${indexName}.replicas`] !== 'undefined' ?
file[`${indexName}.replicas`] :
defaulReplicas;
const configuration = {
settings: {
index: {
number_of_shards: shards,
number_of_replicas: replicas
}
}
};
return configuration;
}
return null;
}

View File

@ -19,6 +19,7 @@ import monitoringTemplate from './integration-files/monitoring-template'
import packageJSON from '../package.json'
import getConfiguration from './lib/get-configuration'
import parseCron from './lib/parse-cron'
import { BuildBody } from './lib/replicas-shards-helper'
export default (server, options) => {
const blueWazuh = colors.blue('wazuh');
@ -299,7 +300,14 @@ export default (server, options) => {
const result = await wzWrapper.checkIfIndexExists(todayIndex);
result ? await insertDocument(todayIndex,clusterName) : await createIndex(todayIndex,clusterName);
if(result) {
const configurationFile = getConfiguration();
const shardConfiguration = BuildBody(configurationFile, 'wazuh.monitoring', 5, 1);
await wzWrapper.updateIndexSettings(todayIndex, shardConfiguration)
await insertDocument(todayIndex,clusterName);
} else {
await createIndex(todayIndex,clusterName);
}
return;