diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index 718b824baa..5383ca394d 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,30 @@ +2006-07-05 Esteban Sanchez + + * windows/pandora_wmi.cc: Removed unnecessary debug. Check the service state on isRunningService () + + * pandora_agent_conf.cc, modules/pandora_module_factory.cc: Removed unnecessary debug. + + * bin/PandoraAgent.tar.gz: Added to repository. + + * ssh/pandora_ssh_client.cc: Debug converted into log. + + * pandora_windows_service.cc: Get the interval value (in minutes) and set it to the service. + Check if the XML of the module is NULL before adding it to the file. Added log output on error. + Delete temporal file before finishing the run. + + * pandora_windows_service.h: Macro modified. + + * modules/pandora_module.h, modules/pandora_module.cc: Added new exception, raised when the interval + is not fulfilled. Added code to the run funtion of the Pandora_Module class, to check the execution + interval. + + * modules/pandora_module_proc.cc, modules/pandora_module_exec.cc, modules/pandora_module_service.cc: + Call to the run() base class function before running the specific code. + + * modules/pandora_module_list.cc: Removed use of namespace and use a explicit specification. + + * Changelog: Renamed to ChangeLog. + 2006-07-04 Esteban Sanchez * windows/pandora_wmi.*, windows/pandora_windows_info.*, windows/wmi/disphelper.*, diff --git a/pandora_agents/win32/bin/PandoraAgent.tar.gz b/pandora_agents/win32/bin/PandoraAgent.tar.gz new file mode 100644 index 0000000000..d8ad383ab6 Binary files /dev/null and b/pandora_agents/win32/bin/PandoraAgent.tar.gz differ diff --git a/pandora_agents/win32/main.cc b/pandora_agents/win32/main.cc index 8aef01fa20..6c9b8f6e04 100644 --- a/pandora_agents/win32/main.cc +++ b/pandora_agents/win32/main.cc @@ -60,7 +60,7 @@ main (int argc, char *argv[]) { } } service->run (); - + delete service; return 0; } diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index b9841f5811..6895d05c9e 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -103,7 +103,22 @@ Pandora_Module::getOutput () const { void Pandora_Module::run () { - + + this->output = ""; + + /* Check the interval */ + if (this->executions % this->module_interval != 0) { + pandoraDebug ("%s: Interval is not fulfilled", + this->module_name.c_str ()); + this->executions++; + has_output = false; + throw Interval_Not_Fulfilled (); + } + + /* Increment the executions after check. This is done to execute the + first time */ + this->executions++; + has_output = true; } TiXmlElement * @@ -116,6 +131,10 @@ Pandora_Module::getXml () { pandoraDebug ("%s getXML begin", module_name.c_str ()); + if (!this->has_output) { + return NULL; + } + try { data = this->getOutput (); } catch (Output_Error e) { @@ -187,6 +206,5 @@ Pandora_Module::setInterval (int interval) { void Pandora_Module::setDescription (string description) { - pandoraDebug ("Set description to: %s", description.c_str ()); this->module_description = description; } diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index 02758b1828..043ac74095 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -52,8 +52,9 @@ namespace Pandora_Modules { const string module_proc_str = "module_proc"; const string module_service_str = "module_service"; - class Output_Error : public Pandora::Pandora_Exception { }; - class Interval_Error : public Pandora::Pandora_Exception { }; + class Output_Error : public Pandora::Pandora_Exception { }; + class Interval_Error : public Pandora::Pandora_Exception { }; + class Interval_Not_Fulfilled : public Pandora::Pandora_Exception { }; class Pandora_Module { protected: @@ -68,6 +69,7 @@ namespace Pandora_Modules { string output; int max, min; bool has_limits; + bool has_output; public: Pandora_Module (string name); virtual ~Pandora_Module (); diff --git a/pandora_agents/win32/modules/pandora_module_exec.cc b/pandora_agents/win32/modules/pandora_module_exec.cc index e41d3d58c4..c56277592f 100644 --- a/pandora_agents/win32/modules/pandora_module_exec.cc +++ b/pandora_agents/win32/modules/pandora_module_exec.cc @@ -45,19 +45,12 @@ Pandora_Module_Exec::run () { HANDLE out, new_stdout, out_read, job; string working_dir; - this->output = ""; - - /* Check the interval */ - if (this->executions % this->module_interval != 0) { - pandoraDebug ("Interval is not fulfilled"); - this->executions++; + try { + Pandora_Module::run (); + } catch (Interval_Not_Fulfilled e) { return; } - /* Increment the executions after check. This is done to execute the - first time */ - this->executions++; - /* Set the bInheritHandle flag so pipe handles are inherited. */ attributes.nLength = sizeof (SECURITY_ATTRIBUTES); attributes.bInheritHandle = TRUE; @@ -133,7 +126,6 @@ Pandora_Module_Exec::run () { PeekNamedPipe (out_read, buffer, BUFSIZE, &read, &avail, NULL); /* Read from the stdout */ - if (read != 0) { do { ReadFile (out_read, buffer, BUFSIZE, &read, diff --git a/pandora_agents/win32/modules/pandora_module_factory.cc b/pandora_agents/win32/modules/pandora_module_factory.cc index 95d6bb785c..0c16ef83d0 100644 --- a/pandora_agents/win32/modules/pandora_module_factory.cc +++ b/pandora_agents/win32/modules/pandora_module_factory.cc @@ -125,7 +125,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { } else { return NULL; } - pandoraDebug ("Description %s", module_description.c_str ()); + if (module_description != "") { module->setDescription (module_description); } diff --git a/pandora_agents/win32/modules/pandora_module_list.cc b/pandora_agents/win32/modules/pandora_module_list.cc index 0aa4381e12..2e67921b73 100644 --- a/pandora_agents/win32/modules/pandora_module_list.cc +++ b/pandora_agents/win32/modules/pandora_module_list.cc @@ -27,7 +27,6 @@ using namespace std; using namespace Pandora; -using namespace Pandora_Module_Factory; Pandora_Module_List::Pandora_Module_List (string filename) { ifstream file (filename.c_str ()); @@ -90,7 +89,7 @@ Pandora_Module_List::parseModuleDefinition (string definition) { Pandora_Module_Proc *module_proc; Pandora_Module_Service *module_service; - module = getModuleFromDefinition (definition); + module = Pandora_Module_Factory::getModuleFromDefinition (definition); if (module != NULL) { switch (module->getModuleKind ()) { diff --git a/pandora_agents/win32/modules/pandora_module_proc.cc b/pandora_agents/win32/modules/pandora_module_proc.cc index 9359d65faa..3934cab2f9 100644 --- a/pandora_agents/win32/modules/pandora_module_proc.cc +++ b/pandora_agents/win32/modules/pandora_module_proc.cc @@ -30,7 +30,8 @@ using namespace Pandora_Strutils; Pandora_Module_Proc::Pandora_Module_Proc (string name, string process_name) : Pandora_Module (name) { - + + this->process_name = process_name; transform (process_name.begin (), process_name.end (), this->process_name.begin (), (int (*) (int)) tolower); @@ -40,10 +41,15 @@ Pandora_Module_Proc::Pandora_Module_Proc (string name, string process_name) void Pandora_Module_Proc::run () { - pandoraDebug ("MODULE_PROC RUN"); - + int res; - int res = Pandora_Wmi::isProcessRunning (this->process_name); - pandoraDebug ("res: %d", res); - output = "1"; + try { + Pandora_Module::run (); + } catch (Interval_Not_Fulfilled e) { + return; + } + + res = Pandora_Wmi::isProcessRunning (this->process_name); + + output = inttostr (res); } diff --git a/pandora_agents/win32/modules/pandora_module_service.cc b/pandora_agents/win32/modules/pandora_module_service.cc index 509f142ad9..9d6c87fbf8 100644 --- a/pandora_agents/win32/modules/pandora_module_service.cc +++ b/pandora_agents/win32/modules/pandora_module_service.cc @@ -31,7 +31,9 @@ using namespace Pandora_Strutils; Pandora_Module_Service::Pandora_Module_Service (string name, string service_name) : Pandora_Module (name) { - + + this->service_name = service_name; + transform (service_name.begin (), service_name.end (), this->service_name.begin (), (int (*) (int)) tolower); @@ -41,7 +43,14 @@ Pandora_Module_Service::Pandora_Module_Service (string name, string service_name void Pandora_Module_Service::run () { - pandoraDebug ("MODULE_SERVICE RUN"); + int res; + + try { + Pandora_Module::run (); + } catch (Interval_Not_Fulfilled e) { + return; + } - output = inttostr (Pandora_Wmi::isServiceRunning (this->service_name)); + res = Pandora_Wmi::isServiceRunning (this->service_name); + output = inttostr (res); } diff --git a/pandora_agents/win32/pandora_agent_conf.cc b/pandora_agents/win32/pandora_agent_conf.cc index b21af44960..742010b64d 100644 --- a/pandora_agents/win32/pandora_agent_conf.cc +++ b/pandora_agents/win32/pandora_agent_conf.cc @@ -34,7 +34,6 @@ Pandora_Agent_Conf::Pandora_Agent_Conf (string filename) { this->key_values = new list (); if (!file.is_open ()) { - pandoraDebug ("No hay conf"); return; } diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index a65996250d..32c99dd3a8 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -59,8 +59,8 @@ Pandora_Windows_Service::~Pandora_Windows_Service () { void Pandora_Windows_Service::pandora_init () { - int interval_ms = 10000; - string conf_file; + int interval_ms = 60000; + string conf_file, interval; setPandoraDebug (true); pandoraDebug ("Init begin"); @@ -69,7 +69,18 @@ Pandora_Windows_Service::pandora_init () { conf_file += "pandora_agent.conf"; this->conf = new Pandora_Agent_Conf (conf_file); this->modules = new Pandora_Module_List (conf_file); + + /* Get the interval value (in minutes) and set it to the service */ + interval = conf->getValue ("interval"); + if (interval != "") { + try { + interval_ms = strtoint (interval) + * 1000 /* miliseconds */; + } catch (Invalid_Conversion e) { + } + } + srand ((unsigned) time (0)); this->setSleepTime (interval_ms); @@ -138,9 +149,11 @@ Pandora_Windows_Service::pandora_run () { module->run (); local_xml = module->getXml (); - agent->InsertEndChild (*local_xml); - - delete local_xml; + if (local_xml != NULL) { + agent->InsertEndChild (*local_xml); + + delete local_xml; + } this->modules->goNext (); } } @@ -178,8 +191,7 @@ Pandora_Windows_Service::pandora_run () { pubkey_file += "key\\id_dsa.pub"; privkey_file = Pandora::getPandoraInstallDir (); privkey_file += "key\\id_dsa"; - pandoraDebug ("Pub: %s Priv: %s", pubkey_file.c_str (), - privkey_file.c_str ()); + ssh_client->connectWithPublicKey (remote_host.c_str (), 22, "babel", pubkey_file, privkey_file, ""); } catch (SSH::Authentication_Failed e) { @@ -213,6 +225,8 @@ Pandora_Windows_Service::pandora_run () { ssh_client->scpFileFilename (remote_filepath + tmp_filename, tmp_filepath); } catch (Pandora_Exception e) { + pandoraLog ("Unable to copy at %s%s", remote_filepath.c_str (), + tmp_filename.c_str ()); ssh_client->disconnect(); delete ssh_client; try { @@ -223,7 +237,13 @@ Pandora_Windows_Service::pandora_run () { } ssh_client->disconnect(); + delete ssh_client; + try { + Pandora_File::removeFile (tmp_filepath); + } catch (Pandora_File::Delete_Error e) { + } + pandoraDebug ("Execution number %d", execution_number); return; diff --git a/pandora_agents/win32/pandora_windows_service.h b/pandora_agents/win32/pandora_windows_service.h index ac2776c1a5..27f949042f 100644 --- a/pandora_agents/win32/pandora_windows_service.h +++ b/pandora_agents/win32/pandora_windows_service.h @@ -19,7 +19,7 @@ */ #ifndef __PANDORA_WINDOWS_SERVICE_H__ -#define __PANDORA_WINDOWS_SERVICE_CLIENT_H__ +#define __PANDORA_WINDOWS_SERVICE_H__ #include #include "windows_service.h" @@ -38,7 +38,7 @@ private: long execution_number; TiXmlElement * getXmlHeader (); - + void pandora_run (); void pandora_init (); public: diff --git a/pandora_agents/win32/ssh/pandora_ssh_client.cc b/pandora_agents/win32/ssh/pandora_ssh_client.cc index ab767ab617..20299682f9 100644 --- a/pandora_agents/win32/ssh/pandora_ssh_client.cc +++ b/pandora_agents/win32/ssh/pandora_ssh_client.cc @@ -227,7 +227,7 @@ Pandora_Ssh_Client::scpFileFilename (const string remote_filename, const string Scp_Failed *e; errmsg = (char *) malloc (sizeof (char) * 1000); libssh2_session_last_error (session, &errmsg, &errmsg_len, 1); - pandoraDebug ("Error %d on SCP %s", sent, errmsg); + pandoraLog ("Error %d on SCP %s", sent, errmsg); e = new Scp_Failed (errmsg); libssh2_channel_close (scp_channel); diff --git a/pandora_agents/win32/windows/pandora_wmi.cc b/pandora_agents/win32/windows/pandora_wmi.cc index bf0a3d6c36..abd2294b8f 100644 --- a/pandora_agents/win32/windows/pandora_wmi.cc +++ b/pandora_agents/win32/windows/pandora_wmi.cc @@ -73,8 +73,7 @@ Pandora_Wmi::isProcessRunning (string process_name) { name = fix.name; transform (name.begin (), name.end (), name.begin (), (int (*) (int)) tolower); - pandoraDebug ("name %s", name.c_str ()); - + if (process_name == name) { result++; } @@ -91,7 +90,6 @@ Pandora_Wmi::isServiceRunning (string service_name) { CDhInitialize init; CDispPtr wmi_svc, quickfixes; string name, state; - int result = 0; dhToggleExceptions (TRUE); @@ -114,20 +112,24 @@ Pandora_Wmi::isServiceRunning (string service_name) { name = fix.name; transform (name.begin (), name.end (), name.begin (), (int (*) (int)) tolower); - pandoraDebug ("name %s", name.c_str ()); if (service_name == name) { dhGetValue (L"%s", &fix.state, quickfix, L".State"); state = fix.state; - pandoraDebug ("state %s", state.c_str ()); + + if (state == "Running") { + return 1; + } else { + return 0; + } } } NEXT_THROW (quickfix); } catch (string errstr) { cerr << "Fatal error details:" << endl << errstr << endl; } - return result; + return 0; } string