Promisified the whole monitoring module

This commit is contained in:
Jesús Ángel González 2018-03-22 17:10:18 +01:00
parent c076a43768
commit 0bd24c19c3

View File

@ -142,7 +142,7 @@ module.exports = (server, options) => {
}; };
// Get API configuration from elastic and callback to loadCredentials // Get API configuration from elastic and callback to loadCredentials
const getConfig = async callback => { const getConfig = async () => {
try { try {
const data = await elasticRequest.callWithInternalUser('search', { const data = await elasticRequest.callWithInternalUser('search', {
index: '.wazuh', index: '.wazuh',
@ -150,21 +150,21 @@ module.exports = (server, options) => {
}) })
if (data.hits.total > 0) { if (data.hits.total > 0) {
return callback(data.hits); return data.hits;
} }
log('monitoring.js getConfig','no credentials'); log('monitoring.js getConfig','no credentials');
return callback({ return {
'error': 'no credentials', error : 'no credentials',
'error_code': 1 error_code: 1
}); };
} catch (error){ } catch (error){
log('monitoring.js getConfig',error.message || error); log('monitoring.js getConfig',error.message || error);
return callback({ return {
'error': 'no elasticsearch', error : 'no elasticsearch',
'error_code': 2 error_code: 2
}); };
} }
}; };
@ -224,7 +224,14 @@ module.exports = (server, options) => {
}; };
// fetchAgents on demand // fetchAgents on demand
const fetchAgents = () => getConfig(loadCredentials); const fetchAgents = async () => {
try {
const data = await getConfig();
return loadCredentials(data);
} catch(error){
return Promise.reject(error);
}
};
// Configure Kibana patterns. // Configure Kibana patterns.
const configureKibana = async () => { const configureKibana = async () => {
@ -429,7 +436,12 @@ module.exports = (server, options) => {
// Cron tab for getting agent status. // Cron tab for getting agent status.
cron.schedule('0 */10 * * * *', () => { cron.schedule('0 */10 * * * *', () => {
agentsArray.length = 0; agentsArray.length = 0;
getConfig(loadCredentials); getConfig()
.then(data => loadCredentials(data))
.catch(error => {
log('monitoring.js',error.message || error);
server.log([blueWazuh, 'monitoring', 'error'], error.message || error)
});
}, true); }, true);
module.exports = fetchAgents; module.exports = fetchAgents;