2019-03-01 15:33:07 +00:00
|
|
|
/*
|
2019-03-04 14:21:23 +00:00
|
|
|
* Wazuh app - Module for refreshing all known fields every 2 minutes
|
2019-03-01 15:33:07 +00:00
|
|
|
* Copyright (C) 2015-2019 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.
|
|
|
|
*/
|
|
|
|
import { checkKnownFields } from './lib/refresh-known-fields';
|
|
|
|
import cron from 'node-cron';
|
|
|
|
import { ElasticWrapper } from './lib/elastic-wrapper';
|
|
|
|
import { log } from './logger';
|
|
|
|
|
|
|
|
export class IndexPatternCronJob {
|
2019-03-01 15:36:31 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} server Hapi.js server object provided by Kibana
|
|
|
|
*/
|
|
|
|
constructor(server) {
|
|
|
|
this.server = server;
|
|
|
|
this.wzWrapper = new ElasticWrapper(server);
|
2019-03-04 14:21:23 +00:00
|
|
|
this.CRON_FREQ = '0 */2 * * * *'; // Every 2 minutes
|
2019-03-01 15:36:31 +00:00
|
|
|
}
|
2019-03-01 15:33:07 +00:00
|
|
|
|
2019-03-01 15:36:31 +00:00
|
|
|
/**
|
|
|
|
* Check all known fields for all Wazuh valid index patterns, every "this.CRON_FREQ".
|
|
|
|
* This function is wrapped into a double try/catch block because we
|
|
|
|
* don't want to kill the Node.js process or to stop the app execution.
|
|
|
|
* This is not a reason to stop, a log is just enough to advice the user.
|
|
|
|
*/
|
|
|
|
async run() {
|
|
|
|
try {
|
|
|
|
// Launch the Cron job
|
|
|
|
cron.schedule(
|
|
|
|
this.CRON_FREQ,
|
|
|
|
async () => {
|
|
|
|
try {
|
|
|
|
// Call the proper method to refresh the known fields
|
|
|
|
await checkKnownFields(
|
|
|
|
this.wzWrapper,
|
|
|
|
false,
|
|
|
|
this.server,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
// Await execution failed
|
2019-04-15 10:47:45 +00:00
|
|
|
log('IndexPatternCronJob:checkKnownFields', error.message || error);
|
2019-03-01 15:36:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2019-03-01 15:47:57 +00:00
|
|
|
log(
|
2019-04-15 10:47:45 +00:00
|
|
|
'IndexPatternCronJob:create-job',
|
2019-03-26 11:05:24 +00:00
|
|
|
'Index pattern cron job started',
|
2019-04-15 10:47:45 +00:00
|
|
|
'debug'
|
2019-03-01 15:47:57 +00:00
|
|
|
);
|
2019-03-01 15:36:31 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Cron job creation failed
|
2019-04-15 10:47:45 +00:00
|
|
|
log('IndexPatternCronJob:create-job', error.message || error);
|
2019-03-01 15:33:07 +00:00
|
|
|
}
|
2019-03-01 15:36:31 +00:00
|
|
|
}
|
|
|
|
}
|