Cron bug and crontab and alert template is sended in win32 TICKETS #3355

This commit is contained in:
fermin831 2016-03-01 19:12:27 +01:00
parent 5e4296b36c
commit 29ee2cb549
4 changed files with 89 additions and 12 deletions

View File

@ -78,6 +78,8 @@ Pandora_Module::Pandora_Module (string name) {
this->warning_inverse = ""; this->warning_inverse = "";
this->quiet = ""; this->quiet = "";
this->module_ff_interval = ""; 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) { } else if (kind == module_plugin_str) {
return MODULE_PLUGIN; return MODULE_PLUGIN;
} else if (kind == module_ping_str) { } else if (kind == module_ping_str) {
return MODULE_PING; return MODULE_PING;
} else if (kind == module_snmpget_str) { } else if (kind == module_snmpget_str) {
return MODULE_SNMPGET; return MODULE_SNMPGET;
} else { } else {
@ -725,6 +727,20 @@ Pandora_Module::getXml () {
module_xml += this->module_ff_interval; module_xml += this->module_ff_interval;
module_xml += "</module_ff_interval>\n"; module_xml += "</module_ff_interval>\n";
} }
/* Module Alert template */
if (this->module_alert_template != "") {
module_xml += "\t<alert_template>";
module_xml += this->module_alert_template;
module_xml += "</alert_template>\n";
}
/* Module Crontab */
if (this->module_crontab != "") {
module_xml += "\t<crontab>";
module_xml += this->module_crontab;
module_xml += "</crontab>\n";
}
/* Write module data */ /* Write module data */
if (this->data_list && this->data_list->size () > 1) { if (this->data_list && this->data_list->size () > 1) {
@ -1002,6 +1018,26 @@ Pandora_Module::setModuleFFInterval (string value) {
this->module_ff_interval = 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. * Set the async flag to the module.
* *
@ -1160,7 +1196,7 @@ Pandora_Module::addGenericCondition (string condition, list<Condition *> **condi
/* Numeric comparison */ /* Numeric comparison */
if (sscanf (condition.c_str (), "%255s %lf %1023[^\n]s", operation, &(cond->value_1), command) == 3) { if (sscanf (condition.c_str (), "%255s %lf %1023[^\n]s", operation, &(cond->value_1), command) == 3) {
cond->operation = operation; cond->operation = operation;
cond->command = command; cond->command = command;
cond->command = command; cond->command = command;
cond->command = "cmd.exe /c \"" + cond->command + "\""; cond->command = "cmd.exe /c \"" + cond->command + "\"";
(*condition_list)->push_back (cond); (*condition_list)->push_back (cond);
@ -1178,7 +1214,7 @@ Pandora_Module::addGenericCondition (string condition, list<Condition *> **condi
(*condition_list)->push_back (cond); (*condition_list)->push_back (cond);
/* Interval */ /* Interval */
} else if (sscanf (condition.c_str (), "(%lf , %lf) %1023[^\n]s", &(cond->value_1), &(cond->value_2), command) == 3) { } 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 = command;
cond->command = "cmd.exe /c \"" + cond->command + "\""; cond->command = "cmd.exe /c \"" + cond->command + "\"";
(*condition_list)->push_back (cond); (*condition_list)->push_back (cond);
@ -1187,7 +1223,7 @@ Pandora_Module::addGenericCondition (string condition, list<Condition *> **condi
delete (cond); delete (cond);
return; return;
} }
return; return;
} }
@ -1526,7 +1562,7 @@ Pandora_Module::evaluateIntensiveConditions () {
* @return 1 if the module should run, 0 if not. * @return 1 if the module should run, 0 if not.
*/ */
int int
Pandora_Module::checkCron () { Pandora_Module::checkCron (int module_interval, int agent_interval) {
int i, time_params[5]; int i, time_params[5];
time_t current_time, offset; time_t current_time, offset;
struct tm *time_struct; struct tm *time_struct;
@ -1568,13 +1604,28 @@ Pandora_Module::checkCron () {
continue; 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 // Check interval
if (cron->params[i][0] <= cron->params[i][1]) { 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; return 0;
} }
} else { } 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; return 0;
} }
} }

View File

@ -177,7 +177,8 @@ namespace Pandora_Modules {
unsigned char intensive_match; unsigned char intensive_match;
int intensive_interval; int intensive_interval;
string unit, custom_id, str_warning, str_critical; 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; string critical_instructions, warning_instructions, unknown_instructions, tags;
protected: protected:
@ -278,6 +279,8 @@ namespace Pandora_Modules {
void setWarningInverse (string value); void setWarningInverse (string value);
void setQuiet (string value); void setQuiet (string value);
void setModuleFFInterval (string value); void setModuleFFInterval (string value);
void setModuleAlertTemplate (string value);
void setModuleCrontab (string value);
void setAsync (bool async); void setAsync (bool async);
void setSave (string save); void setSave (string save);
@ -289,7 +292,7 @@ namespace Pandora_Modules {
void addIntensiveCondition (string intensivecondition); void addIntensiveCondition (string intensivecondition);
int evaluatePreconditions (); int evaluatePreconditions ();
void evaluateConditions (); void evaluateConditions ();
int checkCron (); int checkCron (int module_interval, int agent_interval);
void setCron (string cron_string); void setCron (string cron_string);
void setCronInterval (int interval); void setCronInterval (int interval);
int evaluateCondition (string string_value, double double_value, Condition *condition); int evaluateCondition (string string_value, double double_value, Condition *condition);

View File

@ -119,6 +119,7 @@ using namespace Pandora_Strutils;
#define TOKEN_MODULE_FF_INTERVAL ("module_ff_interval ") #define TOKEN_MODULE_FF_INTERVAL ("module_ff_interval ")
#define TOKEN_MACRO ("module_macro") #define TOKEN_MACRO ("module_macro")
#define TOKEN_NATIVE_ENCODING ("module_native_encoding") #define TOKEN_NATIVE_ENCODING ("module_native_encoding")
#define TOKEN_ALERT_TEMPLATE ("module_alert_template")
string string
parseLine (string line, string token) { 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_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_instructions, module_warning_instructions, module_unknown_instructions, module_tags;
string module_critical_inverse, module_warning_inverse, module_quiet, module_ff_interval; 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; string macro;
Pandora_Module *module; Pandora_Module *module;
bool numeric; bool numeric;
@ -250,6 +251,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module_quiet = ""; module_quiet = "";
module_ff_interval = ""; module_ff_interval = "";
module_native_encoding = ""; module_native_encoding = "";
module_alert_template = "";
macro = ""; macro = "";
stringtok (tokens, definition, "\n"); stringtok (tokens, definition, "\n");
@ -500,6 +502,12 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_ff_interval == "") { if (module_ff_interval == "") {
module_ff_interval = parseLine (line, TOKEN_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 == "") { if (macro == "") {
macro = parseLine (line, TOKEN_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); 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); module->setModuleFFInterval (module_ff_interval);
} }
if (module_alert_template != "") {
module->setModuleAlertTemplate (module_alert_template);
}
if (module_crontab != "") {
module->setModuleCrontab (module_crontab);
}
return module; return module;
} }

View File

@ -1737,7 +1737,7 @@ Pandora_Windows_Service::pandora_run_broker (string config) {
} }
/* Check preconditions */ /* 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 ()); pandoraDebug ("Cron not matched for module %s", module->getName ().c_str ());
module->setNoOutput (); module->setNoOutput ();
this->modules->goNext (); this->modules->goNext ();
@ -1857,7 +1857,7 @@ Pandora_Windows_Service::pandora_run (int forced_run) {
} }
/* Check preconditions */ /* 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 ()); pandoraDebug ("Cron not matched for module %s", module->getName ().c_str ());
module->setNoOutput (); module->setNoOutput ();
this->modules->goNext (); this->modules->goNext ();