From f8792f1fb7a8e1dff3399f427fdb5d87b722e29c Mon Sep 17 00:00:00 2001
From: Ramon Novoa <rnovoa@artica.es>
Date: Thu, 11 Dec 2008 11:57:03 +0000
Subject: [PATCH] 2008-12-11  Ramon Novoa  <rnovoa@artica.es>

	* win32/pandora_windows_service.h,
          win32/pandora_windows_service.cc: Added support for a secondary server
          .




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1288 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_agents/ChangeLog                      |  7 ++
 .../win32/pandora_windows_service.cc          | 68 ++++++++++++++-----
 .../win32/pandora_windows_service.h           |  9 ++-
 3 files changed, 64 insertions(+), 20 deletions(-)

diff --git a/pandora_agents/ChangeLog b/pandora_agents/ChangeLog
index 2c4e005252..a462a2a495 100644
--- a/pandora_agents/ChangeLog
+++ b/pandora_agents/ChangeLog
@@ -1,3 +1,10 @@
+2008-12-11  Ramon Novoa  <rnovoa@artica.es>
+
+	* win32/pandora_windows_service.h,
+	  win32/pandora_windows_service.cc: Added support for a secondary server
+	  .
+
+
 2008-12-11  Ramon Novoa  <rnovoa@artica.es>
 
 	* win32/windows/pandora_wmi.cc,
diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc
index 5048d98b6e..d401f496a9 100644
--- a/pandora_agents/win32/pandora_windows_service.cc
+++ b/pandora_agents/win32/pandora_windows_service.cc
@@ -201,7 +201,11 @@ Pandora_Windows_Service::getXmlHeader () {
 
 void
 Pandora_Windows_Service::copyTentacleDataFile (string host,
-					       string filename)
+					       string filename,
+					       string port,
+					       string ssl,
+					       string pass,
+					       string opts)
 {
 	int     rc;
 	string  var, filepath;
@@ -217,24 +221,20 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
 	/* Build the command to launch the Tentacle client */
 	tentacle_cmd = "tentacle_client.exe -a " + host;
 
-	var = conf->getValue ("server_port");	
-	if (var != "") {
-		tentacle_cmd += " -p " + var;
+	if (port != "") {
+		tentacle_cmd += " -p " + port;
 	}
 
-	var = conf->getValue ("server_ssl");	
-	if (var == "1") {
+	if (ssl == "1") {
 		tentacle_cmd += " -c";
 	}
 
-	var = conf->getValue ("server_pwd");
-	if (var != "") {
-		tentacle_cmd += " -x " + var;
+	if (pass != "") {
+		tentacle_cmd += " -x " + pass;
 	}
 
-	var = conf->getValue ("server_opts");
-	if (var != "") {
-		tentacle_cmd += " " + var;
+	if (opts != "") {
+		tentacle_cmd += " " + opts;
 	}
 
 	tentacle_cmd += " " +  filepath;
@@ -322,11 +322,11 @@ Pandora_Windows_Service::copyScpDataFile (string host,
 void
 Pandora_Windows_Service::copyFtpDataFile (string host,
 					  string remote_path,
-					  string filename)
+					  string filename,
+					  string password)
 {
 	FTP::Pandora_Ftp_Client ftp_client;
 	string                  filepath;
-	string                  password;
 
 	filepath = conf->getValue ("temporal");
 	if (filepath[filepath.length () - 1] != '\\') {
@@ -334,8 +334,6 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
 	}
 	filepath += filename;
 
-	password = conf->getValue ("server_pwd");
-
 	ftp_client.connect (host,
 			    22,
 			    "pandora",
@@ -368,6 +366,7 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
 void
 Pandora_Windows_Service::copyDataFile (string filename)
 {
+	unsigned char copy_to_secondary = 0;
 	string mode, host, remote_path;
 
 	mode = conf->getValue ("transfer_mode");
@@ -379,9 +378,11 @@ Pandora_Windows_Service::copyDataFile (string filename)
 
 	try {
 		if (mode == "ftp") {
-			copyFtpDataFile (host, remote_path, filename);
+			copyFtpDataFile (host, remote_path, filename, conf->getValue ("server_pwd"));
 		} else if (mode == "tentacle") {
-			copyTentacleDataFile (host, filename);
+			copyTentacleDataFile (host, filename, conf->getValue ("server_port"),
+			                      conf->getValue ("server_ssl"), conf->getValue ("server_pwd"),
+			                      conf->getValue ("server_opts"));
 		} else if (mode == "ssh" || mode == "") {
 			copyScpDataFile (host, remote_path, filename);
 		} else {
@@ -391,6 +392,37 @@ Pandora_Windows_Service::copyDataFile (string filename)
 		}
 	
 		pandoraDebug ("Successfuly copied XML file to server.");
+	} catch (Pandora_Exception e) {
+		if (conf->getValue ("secondary_mode") == "on_error") {
+			copy_to_secondary = 1;
+		}
+	}
+	
+	if (conf->getValue ("secondary_mode") == "always") {
+		copy_to_secondary = 1;	
+	}
+
+	// Copy the file to the secondary server if needed
+	if (copy_to_secondary == 0) {
+		return;
+	}
+	
+	try {
+		if (mode == "ftp") {
+			copyFtpDataFile (host, remote_path, filename, conf->getValue ("secondary_server_pwd"));
+		} else if (mode == "tentacle") {
+			copyTentacleDataFile (host, filename, conf->getValue ("secondary_server_port"),
+			                      conf->getValue ("secondary_server_ssl"), conf->getValue ("secondary_server_pwd"),
+			                      conf->getValue ("secondary_server_opts"));
+		} else if (mode == "ssh" || mode == "") {
+			copyScpDataFile (host, remote_path, filename);
+		} else {
+			pandoraLog ("Invalid transfer mode: %s."
+				    "Please recheck transfer_mode option "
+				    "in configuration file.");
+		}
+	
+		pandoraDebug ("Successfuly copied XML file to secondary server.");
 	} catch (Pandora_Exception e) {
 	}
 }
diff --git a/pandora_agents/win32/pandora_windows_service.h b/pandora_agents/win32/pandora_windows_service.h
index 10c7791350..062492c560 100644
--- a/pandora_agents/win32/pandora_windows_service.h
+++ b/pandora_agents/win32/pandora_windows_service.h
@@ -49,13 +49,18 @@ namespace Pandora {
 		TiXmlElement  *getXmlHeader    ();
 		void           copyDataFile    (string filename);
 		void           copyTentacleDataFile (string host,
-						     string filename);
+						     string filename,
+						     string port,
+						     string ssl,
+						     string pass,
+						     string opts);
 		void           copyScpDataFile (string host,
 						string remote_path,
 						string filename);
 		void           copyFtpDataFile (string host,
 						string remote_path,
-						string filename);
+						string filename,
+						string password);
 		void           recvDataFile (string filename);
 		void           recvTentacleDataFile (string host,
 						     string filename);