From a0b71bb432e334513b22887614bae533e257e604 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 5 Sep 2019 17:40:46 +0200 Subject: [PATCH 1/2] module_interval in broker (linux agents) --- pandora_agents/unix/pandora_agent | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index 9b5a24d515..1e5871de4a 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -239,6 +239,15 @@ my $tentacle_pid = undef; # PID of udp_server my $udp_server_pid = undef; +# BrokerFlag +my $BrokerFlag = 0; + +# Global loop counter. +my $LoopCounter = 0; + +# Define a max value for loopCounter to avoid overflow. +use constant MAX_LOOP_COUNTER => 1000000000; + ################################################################################ # Print usage information and exit. ################################################################################ @@ -1385,6 +1394,7 @@ sub sleep_agent { exit (0); } + $LoopCounter = ($LoopCounter + 1) % MAX_LOOP_COUNTER; return $iter_base_time; } @@ -1699,6 +1709,14 @@ sub exec_module { } # Check module interval + if ($BrokerFlag > 0) { + if ($LoopCounter == 0) { + $module->{'counter'} = $module->{'intensive_interval'}; + } else { + $module->{'counter'} = (($LoopCounter -1 ) % $module->{'intensive_interval'}); + } + } + if (++($module->{'counter'}) < $module->{'intensive_interval'}) { $ThreadSem->up () if (defined ($ThreadSem) && $Conf{'agent_threads'} > 1); return; @@ -2985,7 +3003,6 @@ while (1) { @BrokerPid = (); my @broker_agents = read_config ('broker_agent'); foreach my $broker_agent (@broker_agents) { - # Create broker conf file if it does not exist if (! -e "$ConfDir/${broker_agent}.conf") { write_broker_conf($broker_agent); @@ -2995,7 +3012,9 @@ while (1) { # Broker agent if ($main_agent == 0) { - + # Mark broker flag. + $BrokerFlag = 1; + # Set the configuration file $ConfFile = "${broker_agent}.conf"; From 3be05cf38503bc18cabba280ce4b44b491d25f9e Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 24 Sep 2019 19:13:55 +0200 Subject: [PATCH 2/2] added module_interval feature to broker agents --- .../win32/modules/pandora_module.cc | 82 +++++++++++-------- pandora_agents/win32/modules/pandora_module.h | 2 + .../win32/pandora_windows_service.cc | 9 +- .../win32/pandora_windows_service.h | 2 +- 4 files changed, 57 insertions(+), 38 deletions(-) diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index e7c751d367..7cbde21ca7 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -1422,48 +1422,48 @@ Pandora_Module::evaluatePreconditions () { buffer[read] = '\0'; output += (char *) buffer; } - - try { - double_output = Pandora_Strutils::strtodouble (output); - } catch (Pandora_Strutils::Invalid_Conversion e) { - double_output = 0; + + try { + double_output = Pandora_Strutils::strtodouble (output); + } catch (Pandora_Strutils::Invalid_Conversion e) { + double_output = 0; + } + + if (dwRet == WAIT_OBJECT_0) { + break; + } else if(this->getTimeout() < GetTickCount() - tickbase) { + /* STILL_ACTIVE */ + TerminateProcess(pi.hThread, STILL_ACTIVE); + pandoraLog ("evaluatePreconditions: %s timed out (retcode: %d)", this->module_name.c_str (), STILL_ACTIVE); + break; + } } - if (dwRet == WAIT_OBJECT_0) { - break; - } else if(this->getTimeout() < GetTickCount() - tickbase) { - /* STILL_ACTIVE */ - TerminateProcess(pi.hThread, STILL_ACTIVE); - pandoraLog ("evaluatePreconditions: %s timed out (retcode: %d)", this->module_name.c_str (), STILL_ACTIVE); - break; + GetExitCodeProcess (pi.hProcess, &retval); + + if (retval != 0) { + if (! TerminateJobObject (job, 0)) { + pandoraLog ("evaluatePreconditions: TerminateJobObject failed. (error %d)", + GetLastError ()); + } + if (retval != STILL_ACTIVE) { + pandoraLog ("evaluatePreconditions: %s did not executed well (retcode: %d)", + this->module_name.c_str (), retval); + } + /* Close job, process and thread handles. */ + CloseHandle (job); + CloseHandle (pi.hProcess); + CloseHandle (pi.hThread); + CloseHandle (new_stdout); + CloseHandle (out_read); + return 0; } - } - - GetExitCodeProcess (pi.hProcess, &retval); - - if (retval != 0) { - if (! TerminateJobObject (job, 0)) { - pandoraLog ("evaluatePreconditions: TerminateJobObject failed. (error %d)", - GetLastError ()); - } - if (retval != STILL_ACTIVE) { - pandoraLog ("evaluatePreconditions: %s did not executed well (retcode: %d)", - this->module_name.c_str (), retval); - } + /* Close job, process and thread handles. */ CloseHandle (job); CloseHandle (pi.hProcess); CloseHandle (pi.hThread); - CloseHandle (new_stdout); - CloseHandle (out_read); - return 0; } - - /* Close job, process and thread handles. */ - CloseHandle (job); - CloseHandle (pi.hProcess); - CloseHandle (pi.hThread); - } CloseHandle (new_stdout); CloseHandle (out_read); @@ -1693,4 +1693,18 @@ Pandora_Module::getAsync () { return this->async; } +/** + * Get current exections + */ +long +Pandora_Module::getExecutions () { + return this->executions; +} +/** + * Set current execution (global) used for brokers. + */ +void +Pandora_Module::setExecutions (long executions) { + this->executions = executions; +} diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index c766766950..3b9d6c15a4 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -234,6 +234,8 @@ namespace Pandora_Modules { int getTimeout (); string getSave (); bool getAsync (); + void setExecutions(long executions=0); + long getExecutions(); virtual string getXml (); diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index 275ee60724..b62fc1f70d 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -1849,7 +1849,7 @@ Pandora_Windows_Service::sendBufferedXml (string path) { } void -Pandora_Windows_Service::pandora_run_broker (string config) { +Pandora_Windows_Service::pandora_run_broker (string config, long executions) { Pandora_Agent_Conf *conf = NULL; string server_addr; unsigned char data_flag = 0; @@ -1876,7 +1876,10 @@ Pandora_Windows_Service::pandora_run_broker (string config) { Pandora_Module *module; module = this->broker_modules->getCurrentValue (); - + + /* Keep executions matching main agent */ + module->setExecutions(executions); + /* Check preconditions */ if (module->evaluatePreconditions () == 0) { pandoraDebug ("Preconditions not matched for module %s", module->getName ().c_str ()); @@ -2074,7 +2077,7 @@ Pandora_Windows_Service::pandora_run (int forced_run) { check_broker_agents(all_conf); for (i=0;i