From 5dcb313522f95042bd2b42e06cebabb053921fef Mon Sep 17 00:00:00 2001 From: fermin831 Date: Tue, 1 Mar 2016 19:12:27 +0100 Subject: [PATCH] Cron bug and crontab and alert template is sended in win32 TICKETS #3355 --- .../win32/modules/pandora_module.cc | 65 +++++++++++++++++-- pandora_agents/win32/modules/pandora_module.h | 7 +- .../win32/modules/pandora_module_factory.cc | 25 ++++++- .../win32/pandora_windows_service.cc | 4 +- 4 files changed, 89 insertions(+), 12 deletions(-) diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index 6f46aa9c86..fa77cf548d 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -78,6 +78,8 @@ Pandora_Module::Pandora_Module (string name) { this->warning_inverse = ""; this->quiet = ""; this->module_ff_interval = ""; + this->module_alert_template = ""; + this->module_crontab = ""; } /** @@ -256,7 +258,7 @@ Pandora_Module::parseModuleKindFromString (string kind) { } else if (kind == module_plugin_str) { return MODULE_PLUGIN; } else if (kind == module_ping_str) { - return MODULE_PING; + return MODULE_PING; } else if (kind == module_snmpget_str) { return MODULE_SNMPGET; } else { @@ -725,6 +727,20 @@ Pandora_Module::getXml () { module_xml += this->module_ff_interval; module_xml += "\n"; } + + /* Module Alert template */ + if (this->module_alert_template != "") { + module_xml += "\t"; + module_xml += this->module_alert_template; + module_xml += "\n"; + } + + /* Module Crontab */ + if (this->module_crontab != "") { + module_xml += "\t"; + module_xml += this->module_crontab; + module_xml += "\n"; + } /* Write module data */ if (this->data_list && this->data_list->size () > 1) { @@ -1002,6 +1018,26 @@ Pandora_Module::setModuleFFInterval (string value) { this->module_ff_interval = value; } +/** + * Set the module Alert template for the module. + * + * @param value module Alert template value to set. + */ +void +Pandora_Module::setModuleAlertTemplate (string value) { + this->module_alert_template = value; +} + +/** + * Set the module Crontab for the module. + * + * @param value module Crontab value to set. + */ +void +Pandora_Module::setModuleCrontab (string value) { + this->module_crontab = value; +} + /** * Set the async flag to the module. * @@ -1160,7 +1196,7 @@ Pandora_Module::addGenericCondition (string condition, list **condi /* Numeric comparison */ if (sscanf (condition.c_str (), "%255s %lf %1023[^\n]s", operation, &(cond->value_1), command) == 3) { cond->operation = operation; - cond->command = command; + cond->command = command; cond->command = command; cond->command = "cmd.exe /c \"" + cond->command + "\""; (*condition_list)->push_back (cond); @@ -1178,7 +1214,7 @@ Pandora_Module::addGenericCondition (string condition, list **condi (*condition_list)->push_back (cond); /* Interval */ } else if (sscanf (condition.c_str (), "(%lf , %lf) %1023[^\n]s", &(cond->value_1), &(cond->value_2), command) == 3) { - cond->operation = "()"; + cond->operation = "()"; cond->command = command; cond->command = "cmd.exe /c \"" + cond->command + "\""; (*condition_list)->push_back (cond); @@ -1187,7 +1223,7 @@ Pandora_Module::addGenericCondition (string condition, list **condi delete (cond); return; } - + return; } @@ -1526,7 +1562,7 @@ Pandora_Module::evaluateIntensiveConditions () { * @return 1 if the module should run, 0 if not. */ int -Pandora_Module::checkCron () { +Pandora_Module::checkCron (int module_interval, int agent_interval) { int i, time_params[5]; time_t current_time, offset; struct tm *time_struct; @@ -1568,13 +1604,28 @@ Pandora_Module::checkCron () { 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]) { + 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]) { + if ((time_params[i] < cron->params[i][0] && time_params[i] > cron->params[i][1]) && !overflow_cron) { return 0; } } diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index 429ab67394..3d0905a7b0 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -177,7 +177,8 @@ namespace Pandora_Modules { unsigned char intensive_match; int intensive_interval; string unit, custom_id, str_warning, str_critical; - string module_group, warning_inverse, critical_inverse, quiet, module_ff_interval; + string module_group, warning_inverse, critical_inverse, quiet; + string module_ff_interval, module_alert_template, module_crontab; string critical_instructions, warning_instructions, unknown_instructions, tags; protected: @@ -278,6 +279,8 @@ namespace Pandora_Modules { void setWarningInverse (string value); void setQuiet (string value); void setModuleFFInterval (string value); + void setModuleAlertTemplate (string value); + void setModuleCrontab (string value); void setAsync (bool async); void setSave (string save); @@ -289,7 +292,7 @@ namespace Pandora_Modules { void addIntensiveCondition (string intensivecondition); int evaluatePreconditions (); void evaluateConditions (); - int checkCron (); + int checkCron (int module_interval, int agent_interval); void setCron (string cron_string); void setCronInterval (int interval); int evaluateCondition (string string_value, double double_value, Condition *condition); diff --git a/pandora_agents/win32/modules/pandora_module_factory.cc b/pandora_agents/win32/modules/pandora_module_factory.cc index a3143c6d09..33cf5403e1 100644 --- a/pandora_agents/win32/modules/pandora_module_factory.cc +++ b/pandora_agents/win32/modules/pandora_module_factory.cc @@ -119,6 +119,7 @@ using namespace Pandora_Strutils; #define TOKEN_MODULE_FF_INTERVAL ("module_ff_interval ") #define TOKEN_MACRO ("module_macro") #define TOKEN_NATIVE_ENCODING ("module_native_encoding") +#define TOKEN_ALERT_TEMPLATE ("module_alert_template") string parseLine (string line, string token) { @@ -171,7 +172,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { string module_unit, module_group, module_custom_id, module_str_warning, module_str_critical; string module_critical_instructions, module_warning_instructions, module_unknown_instructions, module_tags; string module_critical_inverse, module_warning_inverse, module_quiet, module_ff_interval; - string module_native_encoding; + string module_native_encoding, module_alert_template; string macro; Pandora_Module *module; bool numeric; @@ -250,6 +251,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { module_quiet = ""; module_ff_interval = ""; module_native_encoding = ""; + module_alert_template = ""; macro = ""; stringtok (tokens, definition, "\n"); @@ -500,6 +502,12 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { if (module_ff_interval == "") { module_ff_interval = parseLine (line, TOKEN_MODULE_FF_INTERVAL); } + + if (module_alert_template == "") { + module_alert_template = parseLine (line, TOKEN_ALERT_TEMPLATE); + module_alert_template.erase (0,1); + } + if (macro == "") { macro = parseLine (line, TOKEN_MACRO); @@ -1069,6 +1077,13 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { module_ff_interval.replace(pos_macro, macro_name.size(), macro_value); } } + + if (module_alert_template != "") { + pos_macro = module_alert_template.find(macro_name); + if (pos_macro != string::npos){ + module_alert_template.replace(pos_macro, macro_name.size(), macro_value); + } + } } } } @@ -1405,5 +1420,13 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { module->setModuleFFInterval (module_ff_interval); } + if (module_alert_template != "") { + module->setModuleAlertTemplate (module_alert_template); + } + + if (module_crontab != "") { + module->setModuleCrontab (module_crontab); + } + return module; } diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index 5853951a19..e2f18755bf 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -1808,7 +1808,7 @@ Pandora_Windows_Service::pandora_run_broker (string config) { } /* Check preconditions */ - if (module->checkCron () == 0) { + if (module->checkCron (module->getInterval (), atoi (conf->getValue ("interval").c_str())) == 0) { pandoraDebug ("Cron not matched for module %s", module->getName ().c_str ()); module->setNoOutput (); this->modules->goNext (); @@ -1928,7 +1928,7 @@ Pandora_Windows_Service::pandora_run (int forced_run) { } /* Check preconditions */ - if (module->checkCron () == 0) { + if (module->checkCron (module->getInterval (), atoi (conf->getValue ("interval").c_str())) == 0) { pandoraDebug ("Cron not matched for module %s", module->getName ().c_str ()); module->setNoOutput (); this->modules->goNext ();