From 4b05351df05b867bc77acac6c7474cfa225b4c3f Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 18 May 2017 11:55:44 +0200 Subject: [PATCH] Fixed async issues in windows brokers --- .../win32/modules/pandora_module.cc | 4 ++ pandora_agents/win32/modules/pandora_module.h | 3 +- .../win32/modules/pandora_module_proc.cc | 11 ++++ .../win32/modules/pandora_module_proc.h | 1 + .../win32/modules/pandora_module_service.cc | 10 ++++ .../win32/modules/pandora_module_service.h | 1 + .../win32/pandora_windows_service.cc | 54 +++++++++++++------ .../win32/pandora_windows_service.h | 2 + 8 files changed, 68 insertions(+), 18 deletions(-) diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index 1754ab7cb7..296f42f2ea 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -1789,5 +1789,9 @@ Pandora_Module::getIntensiveMatch () { return this->intensive_match; } +bool +Pandora_Module::getAsync () { + return this->async; +} diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index 4303b88eec..b2606d2442 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -91,7 +91,7 @@ namespace Pandora_Modules { MODULE_TCPCHECK, /**< The module checks whether a tcp port is open */ MODULE_REGEXP, /**< The module searches a file for matches of a regular expression */ MODULE_PLUGIN, /**< Plugin */ - MODULE_PING, /**< Ping module */ + MODULE_PING, /**< Ping module */ MODULE_SNMPGET /**< SNMP get module */ } Module_Kind; @@ -238,6 +238,7 @@ namespace Pandora_Modules { void setTimeout (int timeout); int getTimeout (); string getSave (); + bool getAsync (); virtual string getXml (); diff --git a/pandora_agents/win32/modules/pandora_module_proc.cc b/pandora_agents/win32/modules/pandora_module_proc.cc index 91deab0efb..e8eed1f97d 100644 --- a/pandora_agents/win32/modules/pandora_module_proc.cc +++ b/pandora_agents/win32/modules/pandora_module_proc.cc @@ -52,6 +52,17 @@ Pandora_Module_Proc::Pandora_Module_Proc (string name, string process_name) this->retries = 3; this->start_delay = 5000; this->retry_delay = 2000; + this->thread = 0; +} +/** + * Destroys a Pandora_Module_Service object. + */ +Pandora_Module_Proc::~Pandora_Module_Proc () { + + // Close the thread if module is async + if (this->thread) { + TerminateThread(this->thread, 0); + } } string diff --git a/pandora_agents/win32/modules/pandora_module_proc.h b/pandora_agents/win32/modules/pandora_module_proc.h index 60615f189a..edab85cb88 100644 --- a/pandora_agents/win32/modules/pandora_module_proc.h +++ b/pandora_agents/win32/modules/pandora_module_proc.h @@ -38,6 +38,7 @@ namespace Pandora_Modules { int retry_delay; public: Pandora_Module_Proc (string name, string process_name); + ~Pandora_Module_Proc (); string getProcessName () const; string getStartCommand () const; diff --git a/pandora_agents/win32/modules/pandora_module_service.cc b/pandora_agents/win32/modules/pandora_module_service.cc index bbbe799f9e..bf82120aa8 100644 --- a/pandora_agents/win32/modules/pandora_module_service.cc +++ b/pandora_agents/win32/modules/pandora_module_service.cc @@ -49,6 +49,16 @@ Pandora_Module_Service::Pandora_Module_Service (string name, string service_name this->thread = 0; this->watchdog = false; } +/** + * Destroys a Pandora_Module_Service object. + */ +Pandora_Module_Service::~Pandora_Module_Service () { + + // Close the thread if module is async + if (this->thread) { + TerminateThread(this->thread, 0); + } +} string Pandora_Module_Service::getServiceName () const { diff --git a/pandora_agents/win32/modules/pandora_module_service.h b/pandora_agents/win32/modules/pandora_module_service.h index 6e82697985..bdec6152ed 100644 --- a/pandora_agents/win32/modules/pandora_module_service.h +++ b/pandora_agents/win32/modules/pandora_module_service.h @@ -36,6 +36,7 @@ namespace Pandora_Modules { bool watchdog; public: Pandora_Module_Service (string name, string service_name); + ~Pandora_Module_Service (); void run (); string getServiceName () const; diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index a7c1105975..cad177411a 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -76,6 +76,7 @@ Pandora_Windows_Service::setValues (const char * svc_name, this->service_description = (char *) svc_description; execution_number = 0; this->modules = NULL; + this->broker_modules = NULL; this->conf = NULL; this->interval = 60000; this->timestamp = 0; @@ -105,6 +106,10 @@ Pandora_Windows_Service::~Pandora_Windows_Service () { if (this->modules != NULL) { delete this->modules; } + + if (this->broker_modules != NULL) { + delete this->broker_modules; + } pandoraLog ("Pandora agent stopped"); } @@ -136,10 +141,10 @@ Pandora_Windows_Service::pandora_init_broker (string file_conf) { this->conf = Pandora::Pandora_Agent_Conf::getInstance (); this->conf->setFile (file_conf); - if (this->modules != NULL) { - delete this->modules; + if (this->broker_modules != NULL) { + delete this->broker_modules; } - this->modules = new Pandora_Module_List (file_conf); + this->broker_modules = new Pandora_Module_List (file_conf); pandoraDebug ("Pandora broker agent started"); } @@ -208,9 +213,14 @@ Pandora_Windows_Service::check_broker_agents(string *all_conf){ void Pandora_Windows_Service::pandora_init () { + pandora_init(true); +} + +void +Pandora_Windows_Service::pandora_init (bool reload_modules) { string conf_file, interval, debug, disable_logfile, intensive_interval, util_dir, path, env; string udp_server_enabled, udp_server_port, udp_server_addr, udp_server_auth_addr; - string agent_name, agent_name_cmd, agent_alias, pandora_agent; + string agent_name, agent_name_cmd, agent_alias, agent_alias_cmd, pandora_agent; string proxy_mode, server_ip; string *all_conf; int pos, num; @@ -224,7 +234,7 @@ Pandora_Windows_Service::pandora_init () { this->conf = Pandora::Pandora_Agent_Conf::getInstance (); this->conf->setFile (all_conf); - if (this->modules != NULL) { + if (this->modules != NULL && reload_modules) { delete this->modules; } @@ -255,7 +265,9 @@ Pandora_Windows_Service::pandora_init () { this->setSleepTime (this->intensive_interval); // Read modules - this->modules = new Pandora_Module_List (conf_file); + if (reload_modules) { + this->modules = new Pandora_Module_List (conf_file); + } delete []all_conf; // Get the agent alias. @@ -1856,19 +1868,19 @@ Pandora_Windows_Service::pandora_run_broker (string config) { server_addr = conf->getValue ("server_ip"); - if (this->modules != NULL) { - this->modules->goFirst (); + if (this->broker_modules != NULL) { + this->broker_modules->goFirst (); - while (! this->modules->isLast ()) { + while (! this->broker_modules->isLast ()) { Pandora_Module *module; - module = this->modules->getCurrentValue (); + module = this->broker_modules->getCurrentValue (); /* Check preconditions */ if (module->evaluatePreconditions () == 0) { pandoraDebug ("Preconditions not matched for module %s", module->getName ().c_str ()); module->setNoOutput (); - this->modules->goNext (); + this->broker_modules->goNext (); continue; } @@ -1876,15 +1888,23 @@ Pandora_Windows_Service::pandora_run_broker (string config) { 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 (); + this->broker_modules->goNext (); continue; } + /* Check async */ + if (module->getAsync()) { + pandoraDebug ("Forbidden async module %s in broker agents", module->getName ().c_str ()); + module->setNoOutput (); + this->broker_modules->goNext (); + continue; + } + pandoraDebug ("Run %s", module->getName ().c_str ()); module->run (); if (! module->hasOutput ()) { module->setNoOutput (); - this->modules->goNext (); + this->broker_modules->goNext (); continue; } @@ -1897,7 +1917,7 @@ Pandora_Windows_Service::pandora_run_broker (string config) { intensive_match = module->evaluateIntensiveConditions (); if (intensive_match == module->getIntensiveMatch () && module->getTimestamp () + module->getInterval () * this->interval_sec > this->run_time) { module->setNoOutput (); - this->modules->goNext (); + this->broker_modules->goNext (); continue; } module->setIntensiveMatch (intensive_match); @@ -1912,7 +1932,7 @@ Pandora_Windows_Service::pandora_run_broker (string config) { /* At least one module has data */ data_flag = 1; - this->modules->goNext (); + this->broker_modules->goNext (); } } @@ -1920,7 +1940,7 @@ Pandora_Windows_Service::pandora_run_broker (string config) { // Send the XML if (!server_addr.empty ()) { - this->sendXml (this->modules); + this->sendXml (this->broker_modules); } } @@ -2059,7 +2079,7 @@ Pandora_Windows_Service::pandora_run (int forced_run) { /* Reload the original configuration */ if (num != 0) { - pandora_init (); + pandora_init (false); } /* Reset time reference if necessary */ diff --git a/pandora_agents/win32/pandora_windows_service.h b/pandora_agents/win32/pandora_windows_service.h index be9a7a4eb3..17f0130884 100644 --- a/pandora_agents/win32/pandora_windows_service.h +++ b/pandora_agents/win32/pandora_windows_service.h @@ -42,6 +42,7 @@ namespace Pandora { private: Pandora_Agent_Conf *conf; Pandora_Module_List *modules; + Pandora_Module_List *broker_modules; long execution_number; string agent_name; string alias; @@ -98,6 +99,7 @@ namespace Pandora { void pandora_run (int forced_run); void pandora_run (); void pandora_init (); + void pandora_init (bool reload_modules); long interval; long interval_sec;