Improved to be able to define timeout for file transfer programs

in windows agent.
This commit is contained in:
Junichi Satoh 2015-06-03 16:03:27 +09:00
parent 51e76a0289
commit 97c9358fa0
5 changed files with 87 additions and 46 deletions

View File

@ -107,7 +107,7 @@ read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
*/
int
Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
const string filepath)
const string filepath, int timeout)
{
FILE *fd;
string operation1;
@ -155,8 +155,8 @@ Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
curl_easy_setopt (this->curl, CURLOPT_UPLOAD, 1) ;
curl_easy_setopt (this->curl, CURLOPT_URL, url.c_str ());
curl_easy_setopt (this->curl, CURLOPT_POSTQUOTE, headerlist);
curl_easy_setopt (this->curl, CURLOPT_TIMEOUT, 240);
curl_easy_setopt (this->curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 60);
curl_easy_setopt (this->curl, CURLOPT_TIMEOUT, timeout * 4);
curl_easy_setopt (this->curl, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
curl_easy_setopt (this->curl, CURLOPT_READFUNCTION, read_func);
curl_easy_setopt (this->curl, CURLOPT_READDATA, fd);
curl_easy_setopt (curl, CURLOPT_INFILESIZE_LARGE,

View File

@ -72,7 +72,7 @@ namespace FTP {
void disconnect ();
int ftpFileFilename (const string remote_filename,
const string filepath);
const string filepath, int timeout);
string getError ();
};

View File

@ -79,10 +79,16 @@ Pandora_FTP_Test::test () {
char *err;
DIR *dir;
FILE *conf_fh = NULL;
int timeout;
remote_host = this->conf->getValue ("server_ip");
cout << "Connecting with " << remote_host << "..." << endl;
password = this->conf->getValue ("server_pwd");
timeout = atoi (conf->getValue ("transfer_timeout").c_str ());
if (timeout == 0) {
timeout = 30;
}
if (password == "") {
cout << "FTP password not found in configuration file." << endl;
cout << "Check that server_pwd variable is set." << endl;
@ -136,7 +142,7 @@ Pandora_FTP_Test::test () {
try {
ftp_client->ftpFileFilename (remote_filepath + tmp_filename,
tmp_filepath);
tmp_filepath, timeout);
} catch (FTP::Unknown_Host e) {
cout << "Failed when copying to " << remote_host << " (" <<
ftp_client->getError () << ")" << endl;

View File

@ -687,14 +687,14 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
string port,
string ssl,
string pass,
string opts)
string opts,
int timeout)
{
DWORD rc;
string var, filepath;
string tentacle_cmd, working_dir;
PROCESS_INFORMATION pi;
STARTUPINFO si;
int tentacle_timeout = 0;
var = conf->getValue ("temporal");
if (var[var.length () - 1] != '\\') {
@ -740,24 +740,25 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
CloseHandle (pi.hThread);
/* Timeout */
tentacle_timeout = atoi (conf->getValue ("tentacle_timeout").c_str ());
if (tentacle_timeout <= 0) {
tentacle_timeout = INFINITE;
} else {
/* Convert to milliseconds */
tentacle_timeout *= 1000;
}
if (timeout <= 0) {
timeout = 30;
}
/* Convert to milliseconds */
timeout *= 1000;
if (WaitForSingleObject(pi.hProcess, tentacle_timeout) == WAIT_TIMEOUT) {
if (WaitForSingleObject(pi.hProcess, timeout) == WAIT_TIMEOUT) {
TerminateProcess(pi.hProcess, STILL_ACTIVE);
CloseHandle (pi.hProcess);
pandoraLog ("Pandora Agent: Failed when copying to %s (tentacle timeout)", host.c_str ());
return -1;
}
/* Get the return code of the tentacle client*/
GetExitCodeProcess (pi.hProcess, &rc);
GetExitCodeProcess (pi.hProcess, &rc);
if (rc != 0) {
CloseHandle (pi.hProcess);
pandoraLog ("Pandora Agent: Failed when copying to %s",
host.c_str ());
return -1;
}
@ -770,6 +771,8 @@ Pandora_Windows_Service::copyScpDataFile (string host,
string remote_path,
string filename)
{
/* TODO: timeout implementation */
int rc = 0;
SSH::Pandora_Ssh_Client ssh_client;
string tmp_dir, filepath,port_str;
@ -830,7 +833,8 @@ int
Pandora_Windows_Service::copyFtpDataFile (string host,
string remote_path,
string filename,
string password)
string password,
int timeout)
{
int rc = 0;
FTP::Pandora_Ftp_Client ftp_client;
@ -856,7 +860,7 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
password);
rc = ftp_client.ftpFileFilename (remote_path + filename,
filepath);
filepath, timeout);
if (rc == UNKNOWN_HOST) {
pandoraLog ("Pandora Agent: Failed when copying to %s (%s)",
host.c_str (), ftp_client.getError ().c_str ());
@ -882,13 +886,18 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
int
Pandora_Windows_Service::copyDataFile (string filename)
{
int rc = 0;
int rc = 0, timeout;
unsigned char copy_to_secondary = 0;
string mode, host, remote_path;
mode = conf->getValue ("transfer_mode");
host = conf->getValue ("server_ip");
remote_path = conf->getValue ("server_path");
timeout = atoi (conf->getValue ("transfer_timeout").c_str ());
if (timeout == 0) {
timeout = 30;
}
// Fix remote path
if (mode != "local" && remote_path[remote_path.length () - 1] != '/') {
remote_path += "/";
@ -897,11 +906,11 @@ Pandora_Windows_Service::copyDataFile (string filename)
}
if (mode == "ftp") {
rc = copyFtpDataFile (host, remote_path, filename, conf->getValue ("server_pwd"));
rc = copyFtpDataFile (host, remote_path, filename, conf->getValue ("server_pwd"), timeout);
} else if (mode == "tentacle" || mode == "") {
rc = copyTentacleDataFile (host, filename, conf->getValue ("server_port"),
conf->getValue ("server_ssl"), conf->getValue ("server_pwd"),
conf->getValue ("server_opts"));
conf->getValue ("server_opts"), timeout);
} else if (mode == "ssh") {
rc =copyScpDataFile (host, remote_path, filename);
} else if (mode == "local") {
@ -932,6 +941,10 @@ Pandora_Windows_Service::copyDataFile (string filename)
mode = conf->getValue ("secondary_transfer_mode");
host = conf->getValue ("secondary_server_ip");
remote_path = conf->getValue ("secondary_server_path");
timeout = atoi (conf->getValue ("secondary_transfer_timeout").c_str ());
if (timeout == 0) {
timeout = 30;
}
// Fix remote path
if (mode != "local" && remote_path[remote_path.length () - 1] != '/') {
@ -942,11 +955,11 @@ Pandora_Windows_Service::copyDataFile (string filename)
// Send the file to the secondary server
if (mode == "ftp") {
rc = copyFtpDataFile (host, remote_path, filename, conf->getValue ("secondary_server_pwd"));
rc = copyFtpDataFile (host, remote_path, filename, conf->getValue ("secondary_server_pwd"), timeout);
} else if (mode == "tentacle" || mode == "") {
rc = copyTentacleDataFile (host, filename, conf->getValue ("secondary_server_port"),
conf->getValue ("secondary_server_ssl"), conf->getValue ("secondary_server_pwd"),
conf->getValue ("secondary_server_opts"));
conf->getValue ("secondary_server_opts"), timeout);
} else if (mode == "ssh") {
rc = copyScpDataFile (host, remote_path, filename);
} else {
@ -965,11 +978,14 @@ Pandora_Windows_Service::copyDataFile (string filename)
void
Pandora_Windows_Service::recvTentacleDataFile (string host,
string filename)
string filename,
int timeout)
{
int rc;
DWORD rc;
string var;
string tentacle_cmd;
PROCESS_INFORMATION pi;
STARTUPINFO si;
/* Change directory to "temporal" */
var = conf->getValue ("temporal");
@ -1008,42 +1024,58 @@ Pandora_Windows_Service::recvTentacleDataFile (string host,
filename.c_str (), host.c_str ());
pandoraDebug ("Command %s", tentacle_cmd.c_str());
rc = system (tentacle_cmd.c_str());
switch (rc) {
/* system() error */
case -1:
pandoraLog ("Unable to receive file %s", filename.c_str ());
throw Pandora_Exception ();
/* tentacle_client.exe returned OK */
case 0:
break;
/* tentacle_client.exe error */
default:
pandoraDebug ("Tentacle client was unable to receive file %s",
filename.c_str ());
throw Pandora_Exception ();
ZeroMemory (&si, sizeof (si));
ZeroMemory (&pi, sizeof (pi));
if (CreateProcess (NULL , (CHAR *)tentacle_cmd.c_str (), NULL, NULL, FALSE,
CREATE_NO_WINDOW, NULL, NULL, &si, &pi) == 0) {
return;
}
/* close thread handle, because it won't be used */
CloseHandle (pi.hThread);
/* Timeout */
if (timeout <= 0) {
timeout = 30;
}
/* Convert to milliseconds */
timeout *= 1000;
if (WaitForSingleObject(pi.hProcess, timeout) == WAIT_TIMEOUT) {
TerminateProcess(pi.hProcess, STILL_ACTIVE);
CloseHandle (pi.hProcess);
pandoraLog ("Unable to receive file %s (tentacle timeout)", filename.c_str ());
return;
}
/* Get the return code of the tentacle client*/
GetExitCodeProcess (pi.hProcess, &rc);
if (rc != 0) {
CloseHandle (pi.hProcess);
pandoraLog ("Unable to receive file %s", filename.c_str ());
return;
}
CloseHandle (pi.hProcess);
return;
}
void
Pandora_Windows_Service::recvDataFile (string filename) {
string mode, host, remote_path;
int timeout;
mode = conf->getValue ("transfer_mode");
host = conf->getValue ("server_ip");
remote_path = conf->getValue ("server_path");
timeout = atoi (conf->getValue ("transfer_timeout").c_str ());
if (remote_path[remote_path.length () - 1] != '/') {
remote_path += "/";
}
try {
if (mode == "tentacle") {
recvTentacleDataFile (host, filename);
recvTentacleDataFile (host, filename, timeout);
} else {
pandoraLog ("Transfer mode %s does not support file retrieval.", mode.c_str () );
throw Pandora_Exception ();

View File

@ -61,19 +61,22 @@ namespace Pandora {
string port,
string ssl,
string pass,
string opts);
string opts,
int timeout);
int copyScpDataFile (string host,
string remote_path,
string filename);
int copyFtpDataFile (string host,
string remote_path,
string filename,
string password);
string password,
int timeout);
int copyLocalDataFile (string remote_path,
string filename);
void recvDataFile (string filename);
void recvTentacleDataFile (string host,
string filename);
string filename,
int timeout);
int unzipCollection(string zip_path, string dest_dir);
void checkCollections ();