diff --git a/pandora_agents/win32/misc/cron.cc b/pandora_agents/win32/misc/cron.cc index 84a3c5b926..676ce58320 100644 --- a/pandora_agents/win32/misc/cron.cc +++ b/pandora_agents/win32/misc/cron.cc @@ -79,6 +79,13 @@ Cron::Cron (string cron_string) { } } } + +/** + * @brief Getter of isSet property + * + */ +bool Cron::getIsSet () { return this->isSet; } + /** * @brief Given a date, return if is inside a cron string or not * @@ -162,4 +169,31 @@ bool Cron::isNormalInterval (int position) { return true; } return (this->params[position][CRUP] >= this->params[position][CRDOWN]); +} + +/** + * @brief Check if cron module should be executed at a given time + * + * @param date + * @return true if should execute + * @return false if should not execute + */ +bool Cron::shouldExecuteAt (time_t date) { + return this->utimestamp < date; +} + +/** + * @brief Update the cron utimestamp + * + * @param date Timestamp "from" to update cron utimestamp + * @param interval Module interval + */ +void Cron::update (time_t date, int interval) { + // TODO + this->utimestamp = date + interval; + Pandora::pandoraDebug( + "Module with cron %s will be executed at timestamp: %d.", + this->cronString.c_str(), + this->utimestamp + ); } \ No newline at end of file diff --git a/pandora_agents/win32/misc/cron.h b/pandora_agents/win32/misc/cron.h index 86b5906189..574ae9c791 100644 --- a/pandora_agents/win32/misc/cron.h +++ b/pandora_agents/win32/misc/cron.h @@ -61,11 +61,13 @@ class Cron { // Constructor Cron(string cron_string); // Getter & setters + bool getIsSet(); string getCronString(); time_t getNextExecution(); // Other methods - bool executeNow(); + void update(time_t date, int interval); + bool shouldExecuteAt(time_t date); }; #endif diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index 99b0d47f88..3b7e57df03 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -1563,158 +1563,28 @@ Pandora_Module::evaluateIntensiveConditions () { /** * Checks the module cron. Returns 1 if the module should run, 0 if not. * - * @return 1 if the module should run, 0 if not. + * @return true if the module should run. */ -int -Pandora_Module::checkCron (int module_interval, int agent_interval) { - int i, time_params[5]; - time_t current_time, offset; - struct tm *time_struct; - Cron *cron = this->cron; +bool +Pandora_Module::checkCron (int interval) { - // No cron - if (cron == NULL) { - return 1; - } + // Execute always if cron is not configured + if (!this->cron->getIsSet()) return true; - // Get current time - current_time = time (NULL); + time_t now = time(NULL); + if (!this->cron->shouldExecuteAt(now)) return false; - // Check if the module was already executed - if (current_time <= cron->utimestamp) { - return 0; - } + this->cron->update(now, interval); - // Break current time - time_struct = localtime(¤t_time); - if (time_struct == NULL) { - return 1; - } - - time_params[0] = time_struct->tm_min; - time_params[1] = time_struct->tm_hour; - time_params[2] = time_struct->tm_mday; - time_params[3] = time_struct->tm_mon; - time_params[4] = time_struct->tm_wday; - - // Fix month (localtime retuns 0..11 and we need 1..12) - time_params[3] += 1; - - // Check cron parameters - for (i = 0; i < 5; i++) { - - // Wildcard - if (cron->params[i][0] < 0) { - continue; - } - - // Check if next execution will overflow the cron (only minutes overflow) - // If overflow, execute the module - bool overflow_cron = false; - if (i == 0) { - int start_cron_seconds = cron->params[i][0]*60; - int current_exec_seconds = time_params[i]*60; - int next_exec_seconds = time_params[i]*60 + module_interval*agent_interval; - if (current_exec_seconds > start_cron_seconds && current_exec_seconds > next_exec_seconds) { - start_cron_seconds += 3600; - } - if ((current_exec_seconds <= start_cron_seconds) && (start_cron_seconds <= next_exec_seconds)) { - overflow_cron = true; - } - } - - // Check interval - if (cron->params[i][0] <= cron->params[i][1]) { - if ((time_params[i] < cron->params[i][0] || time_params[i] > cron->params[i][1]) && !overflow_cron) { - return 0; - } - } else { - if ((time_params[i] < cron->params[i][0] && time_params[i] > cron->params[i][1]) && !overflow_cron) { - return 0; - } - } - } - - // Do not check in the next minute, hour, day or month. - offset = 0; - if (cron->interval >= 0) { - offset = cron->interval; - } else if(cron->params[0][0] >= 0) { - // 1 minute - offset = 60; - } else if(cron->params[1][0] >= 0) { - // 1 hour - offset = 3600; - } else if(cron->params[2][0] >=0 || cron->params[4][0] >= 0) { - // 1 day - offset = 86400; - } else if(cron->params[3][0] >= 0) { - // 31 days - offset = 2678400; - } - - cron->utimestamp = current_time + offset; - return 1; + return true; } /** * Sets the module cron from a string. - * - * @return 1 if the module should run, 0 if not. */ void Pandora_Module::setCron (string cron_string) { - int i, value; - char cron_params[5][256], bottom[256], top[256]; - - /* Create the new cron if necessary */ - if (this->cron == NULL) { - this->cron = new Cron (); - } - - /* Parse the cron string */ - if (sscanf (cron_string.c_str (), "%255s %255s %255s %255s %255s", cron_params[0], cron_params[1], cron_params[2], cron_params[3], cron_params[4]) != 5) { - pandoraDebug ("Invalid cron string: %s", cron_string.c_str ()); - return; - } - - /* Fill the cron structure */ - this->cron->utimestamp = 0; - this->cron->interval = -1; - for (i = 0; i < 5; i++) { - - /* Wildcard */ - if (cron_params[i][0] == '*') { - this->cron->params[i][0] = -1; - - /* Interval */ - } else if (sscanf (cron_params[i], "%255[^-]-%255s", bottom, top) == 2) { - value = atoi (bottom); - this->cron->params[i][0] = value; - value = atoi (top); - this->cron->params[i][1] = value; - - /* Single value */ - } else { - value = atoi (cron_params[i]); - this->cron->params[i][0] = value; - this->cron->params[i][1] = value; - } - } -} - -/** - * Sets the interval of the module cron. - * - * @param interval Module cron interval in seconds. - */ -void -Pandora_Module::setCronInterval (int interval) { - if (this->cron == NULL) { - this->cron = new Cron (); - } - - this->cron->interval = interval; + this->cron = new Cron(cron_string); } /** diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index 2fbeb52e41..7d378bb6dd 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -22,6 +22,7 @@ #define __PANDORA_MODULE_H__ #include "../pandora.h" +#include "../misc/cron.h" #include "pandora_data.h" #include "boost/regex.h" #include @@ -108,15 +109,6 @@ namespace Pandora_Modules { regex_t regexp; } Condition; - /** - * Defines the structure that holds the module cron. - */ - typedef struct { - time_t utimestamp; - int interval; - int params[5][2]; - } Cron; - const string module_exec_str = "module_exec"; const string module_proc_str = "module_proc"; const string module_service_str = "module_service"; @@ -298,7 +290,7 @@ namespace Pandora_Modules { void addIntensiveCondition (string intensivecondition); int evaluatePreconditions (); void evaluateConditions (); - int checkCron (int module_interval, int agent_interval); + bool checkCron (int interval); void setCron (string cron_string); void setCronInterval (int interval); int evaluateCondition (string string_value, double double_value, Condition *condition);