mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-09-26 03:19:05 +02:00
Improved to be able to define timeout for file transfer programs
in windows agent.
This commit is contained in:
parent
51e76a0289
commit
97c9358fa0
@ -107,7 +107,7 @@ read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
|||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
|
Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
|
||||||
const string filepath)
|
const string filepath, int timeout)
|
||||||
{
|
{
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
string operation1;
|
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_UPLOAD, 1) ;
|
||||||
curl_easy_setopt (this->curl, CURLOPT_URL, url.c_str ());
|
curl_easy_setopt (this->curl, CURLOPT_URL, url.c_str ());
|
||||||
curl_easy_setopt (this->curl, CURLOPT_POSTQUOTE, headerlist);
|
curl_easy_setopt (this->curl, CURLOPT_POSTQUOTE, headerlist);
|
||||||
curl_easy_setopt (this->curl, CURLOPT_TIMEOUT, 240);
|
curl_easy_setopt (this->curl, CURLOPT_TIMEOUT, timeout * 4);
|
||||||
curl_easy_setopt (this->curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 60);
|
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_READFUNCTION, read_func);
|
||||||
curl_easy_setopt (this->curl, CURLOPT_READDATA, fd);
|
curl_easy_setopt (this->curl, CURLOPT_READDATA, fd);
|
||||||
curl_easy_setopt (curl, CURLOPT_INFILESIZE_LARGE,
|
curl_easy_setopt (curl, CURLOPT_INFILESIZE_LARGE,
|
||||||
|
@ -72,7 +72,7 @@ namespace FTP {
|
|||||||
void disconnect ();
|
void disconnect ();
|
||||||
|
|
||||||
int ftpFileFilename (const string remote_filename,
|
int ftpFileFilename (const string remote_filename,
|
||||||
const string filepath);
|
const string filepath, int timeout);
|
||||||
|
|
||||||
string getError ();
|
string getError ();
|
||||||
};
|
};
|
||||||
|
@ -79,10 +79,16 @@ Pandora_FTP_Test::test () {
|
|||||||
char *err;
|
char *err;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
FILE *conf_fh = NULL;
|
FILE *conf_fh = NULL;
|
||||||
|
int timeout;
|
||||||
|
|
||||||
remote_host = this->conf->getValue ("server_ip");
|
remote_host = this->conf->getValue ("server_ip");
|
||||||
cout << "Connecting with " << remote_host << "..." << endl;
|
cout << "Connecting with " << remote_host << "..." << endl;
|
||||||
password = this->conf->getValue ("server_pwd");
|
password = this->conf->getValue ("server_pwd");
|
||||||
|
timeout = atoi (conf->getValue ("transfer_timeout").c_str ());
|
||||||
|
if (timeout == 0) {
|
||||||
|
timeout = 30;
|
||||||
|
}
|
||||||
|
|
||||||
if (password == "") {
|
if (password == "") {
|
||||||
cout << "FTP password not found in configuration file." << endl;
|
cout << "FTP password not found in configuration file." << endl;
|
||||||
cout << "Check that server_pwd variable is set." << endl;
|
cout << "Check that server_pwd variable is set." << endl;
|
||||||
@ -136,7 +142,7 @@ Pandora_FTP_Test::test () {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ftp_client->ftpFileFilename (remote_filepath + tmp_filename,
|
ftp_client->ftpFileFilename (remote_filepath + tmp_filename,
|
||||||
tmp_filepath);
|
tmp_filepath, timeout);
|
||||||
} catch (FTP::Unknown_Host e) {
|
} catch (FTP::Unknown_Host e) {
|
||||||
cout << "Failed when copying to " << remote_host << " (" <<
|
cout << "Failed when copying to " << remote_host << " (" <<
|
||||||
ftp_client->getError () << ")" << endl;
|
ftp_client->getError () << ")" << endl;
|
||||||
|
@ -687,14 +687,14 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
|
|||||||
string port,
|
string port,
|
||||||
string ssl,
|
string ssl,
|
||||||
string pass,
|
string pass,
|
||||||
string opts)
|
string opts,
|
||||||
|
int timeout)
|
||||||
{
|
{
|
||||||
DWORD rc;
|
DWORD rc;
|
||||||
string var, filepath;
|
string var, filepath;
|
||||||
string tentacle_cmd, working_dir;
|
string tentacle_cmd, working_dir;
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
STARTUPINFO si;
|
STARTUPINFO si;
|
||||||
int tentacle_timeout = 0;
|
|
||||||
|
|
||||||
var = conf->getValue ("temporal");
|
var = conf->getValue ("temporal");
|
||||||
if (var[var.length () - 1] != '\\') {
|
if (var[var.length () - 1] != '\\') {
|
||||||
@ -740,24 +740,25 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
|
|||||||
CloseHandle (pi.hThread);
|
CloseHandle (pi.hThread);
|
||||||
|
|
||||||
/* Timeout */
|
/* Timeout */
|
||||||
tentacle_timeout = atoi (conf->getValue ("tentacle_timeout").c_str ());
|
if (timeout <= 0) {
|
||||||
if (tentacle_timeout <= 0) {
|
timeout = 30;
|
||||||
tentacle_timeout = INFINITE;
|
}
|
||||||
} else {
|
/* Convert to milliseconds */
|
||||||
/* Convert to milliseconds */
|
timeout *= 1000;
|
||||||
tentacle_timeout *= 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (WaitForSingleObject(pi.hProcess, tentacle_timeout) == WAIT_TIMEOUT) {
|
if (WaitForSingleObject(pi.hProcess, timeout) == WAIT_TIMEOUT) {
|
||||||
TerminateProcess(pi.hProcess, STILL_ACTIVE);
|
TerminateProcess(pi.hProcess, STILL_ACTIVE);
|
||||||
CloseHandle (pi.hProcess);
|
CloseHandle (pi.hProcess);
|
||||||
|
pandoraLog ("Pandora Agent: Failed when copying to %s (tentacle timeout)", host.c_str ());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get the return code of the tentacle client*/
|
/* Get the return code of the tentacle client*/
|
||||||
GetExitCodeProcess (pi.hProcess, &rc);
|
GetExitCodeProcess (pi.hProcess, &rc);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
CloseHandle (pi.hProcess);
|
CloseHandle (pi.hProcess);
|
||||||
|
pandoraLog ("Pandora Agent: Failed when copying to %s",
|
||||||
|
host.c_str ());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -770,6 +771,8 @@ Pandora_Windows_Service::copyScpDataFile (string host,
|
|||||||
string remote_path,
|
string remote_path,
|
||||||
string filename)
|
string filename)
|
||||||
{
|
{
|
||||||
|
/* TODO: timeout implementation */
|
||||||
|
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
SSH::Pandora_Ssh_Client ssh_client;
|
SSH::Pandora_Ssh_Client ssh_client;
|
||||||
string tmp_dir, filepath,port_str;
|
string tmp_dir, filepath,port_str;
|
||||||
@ -830,7 +833,8 @@ int
|
|||||||
Pandora_Windows_Service::copyFtpDataFile (string host,
|
Pandora_Windows_Service::copyFtpDataFile (string host,
|
||||||
string remote_path,
|
string remote_path,
|
||||||
string filename,
|
string filename,
|
||||||
string password)
|
string password,
|
||||||
|
int timeout)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
FTP::Pandora_Ftp_Client ftp_client;
|
FTP::Pandora_Ftp_Client ftp_client;
|
||||||
@ -856,7 +860,7 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
|
|||||||
password);
|
password);
|
||||||
|
|
||||||
rc = ftp_client.ftpFileFilename (remote_path + filename,
|
rc = ftp_client.ftpFileFilename (remote_path + filename,
|
||||||
filepath);
|
filepath, timeout);
|
||||||
if (rc == UNKNOWN_HOST) {
|
if (rc == UNKNOWN_HOST) {
|
||||||
pandoraLog ("Pandora Agent: Failed when copying to %s (%s)",
|
pandoraLog ("Pandora Agent: Failed when copying to %s (%s)",
|
||||||
host.c_str (), ftp_client.getError ().c_str ());
|
host.c_str (), ftp_client.getError ().c_str ());
|
||||||
@ -882,13 +886,18 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
|
|||||||
int
|
int
|
||||||
Pandora_Windows_Service::copyDataFile (string filename)
|
Pandora_Windows_Service::copyDataFile (string filename)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0, timeout;
|
||||||
unsigned char copy_to_secondary = 0;
|
unsigned char copy_to_secondary = 0;
|
||||||
string mode, host, remote_path;
|
string mode, host, remote_path;
|
||||||
|
|
||||||
mode = conf->getValue ("transfer_mode");
|
mode = conf->getValue ("transfer_mode");
|
||||||
host = conf->getValue ("server_ip");
|
host = conf->getValue ("server_ip");
|
||||||
remote_path = conf->getValue ("server_path");
|
remote_path = conf->getValue ("server_path");
|
||||||
|
timeout = atoi (conf->getValue ("transfer_timeout").c_str ());
|
||||||
|
if (timeout == 0) {
|
||||||
|
timeout = 30;
|
||||||
|
}
|
||||||
|
|
||||||
// Fix remote path
|
// Fix remote path
|
||||||
if (mode != "local" && remote_path[remote_path.length () - 1] != '/') {
|
if (mode != "local" && remote_path[remote_path.length () - 1] != '/') {
|
||||||
remote_path += "/";
|
remote_path += "/";
|
||||||
@ -897,11 +906,11 @@ Pandora_Windows_Service::copyDataFile (string filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "ftp") {
|
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 == "") {
|
} else if (mode == "tentacle" || mode == "") {
|
||||||
rc = copyTentacleDataFile (host, filename, conf->getValue ("server_port"),
|
rc = copyTentacleDataFile (host, filename, conf->getValue ("server_port"),
|
||||||
conf->getValue ("server_ssl"), conf->getValue ("server_pwd"),
|
conf->getValue ("server_ssl"), conf->getValue ("server_pwd"),
|
||||||
conf->getValue ("server_opts"));
|
conf->getValue ("server_opts"), timeout);
|
||||||
} else if (mode == "ssh") {
|
} else if (mode == "ssh") {
|
||||||
rc =copyScpDataFile (host, remote_path, filename);
|
rc =copyScpDataFile (host, remote_path, filename);
|
||||||
} else if (mode == "local") {
|
} else if (mode == "local") {
|
||||||
@ -932,6 +941,10 @@ Pandora_Windows_Service::copyDataFile (string filename)
|
|||||||
mode = conf->getValue ("secondary_transfer_mode");
|
mode = conf->getValue ("secondary_transfer_mode");
|
||||||
host = conf->getValue ("secondary_server_ip");
|
host = conf->getValue ("secondary_server_ip");
|
||||||
remote_path = conf->getValue ("secondary_server_path");
|
remote_path = conf->getValue ("secondary_server_path");
|
||||||
|
timeout = atoi (conf->getValue ("secondary_transfer_timeout").c_str ());
|
||||||
|
if (timeout == 0) {
|
||||||
|
timeout = 30;
|
||||||
|
}
|
||||||
|
|
||||||
// Fix remote path
|
// Fix remote path
|
||||||
if (mode != "local" && remote_path[remote_path.length () - 1] != '/') {
|
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
|
// Send the file to the secondary server
|
||||||
if (mode == "ftp") {
|
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 == "") {
|
} else if (mode == "tentacle" || mode == "") {
|
||||||
rc = copyTentacleDataFile (host, filename, conf->getValue ("secondary_server_port"),
|
rc = copyTentacleDataFile (host, filename, conf->getValue ("secondary_server_port"),
|
||||||
conf->getValue ("secondary_server_ssl"), conf->getValue ("secondary_server_pwd"),
|
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") {
|
} else if (mode == "ssh") {
|
||||||
rc = copyScpDataFile (host, remote_path, filename);
|
rc = copyScpDataFile (host, remote_path, filename);
|
||||||
} else {
|
} else {
|
||||||
@ -965,11 +978,14 @@ Pandora_Windows_Service::copyDataFile (string filename)
|
|||||||
|
|
||||||
void
|
void
|
||||||
Pandora_Windows_Service::recvTentacleDataFile (string host,
|
Pandora_Windows_Service::recvTentacleDataFile (string host,
|
||||||
string filename)
|
string filename,
|
||||||
|
int timeout)
|
||||||
{
|
{
|
||||||
int rc;
|
DWORD rc;
|
||||||
string var;
|
string var;
|
||||||
string tentacle_cmd;
|
string tentacle_cmd;
|
||||||
|
PROCESS_INFORMATION pi;
|
||||||
|
STARTUPINFO si;
|
||||||
|
|
||||||
/* Change directory to "temporal" */
|
/* Change directory to "temporal" */
|
||||||
var = conf->getValue ("temporal");
|
var = conf->getValue ("temporal");
|
||||||
@ -1008,42 +1024,58 @@ Pandora_Windows_Service::recvTentacleDataFile (string host,
|
|||||||
filename.c_str (), host.c_str ());
|
filename.c_str (), host.c_str ());
|
||||||
pandoraDebug ("Command %s", tentacle_cmd.c_str());
|
pandoraDebug ("Command %s", tentacle_cmd.c_str());
|
||||||
|
|
||||||
rc = system (tentacle_cmd.c_str());
|
ZeroMemory (&si, sizeof (si));
|
||||||
switch (rc) {
|
ZeroMemory (&pi, sizeof (pi));
|
||||||
|
if (CreateProcess (NULL , (CHAR *)tentacle_cmd.c_str (), NULL, NULL, FALSE,
|
||||||
/* system() error */
|
CREATE_NO_WINDOW, NULL, NULL, &si, &pi) == 0) {
|
||||||
case -1:
|
return;
|
||||||
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 ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Pandora_Windows_Service::recvDataFile (string filename) {
|
Pandora_Windows_Service::recvDataFile (string filename) {
|
||||||
string mode, host, remote_path;
|
string mode, host, remote_path;
|
||||||
|
int timeout;
|
||||||
|
|
||||||
mode = conf->getValue ("transfer_mode");
|
mode = conf->getValue ("transfer_mode");
|
||||||
host = conf->getValue ("server_ip");
|
host = conf->getValue ("server_ip");
|
||||||
remote_path = conf->getValue ("server_path");
|
remote_path = conf->getValue ("server_path");
|
||||||
|
timeout = atoi (conf->getValue ("transfer_timeout").c_str ());
|
||||||
if (remote_path[remote_path.length () - 1] != '/') {
|
if (remote_path[remote_path.length () - 1] != '/') {
|
||||||
remote_path += "/";
|
remote_path += "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (mode == "tentacle") {
|
if (mode == "tentacle") {
|
||||||
recvTentacleDataFile (host, filename);
|
recvTentacleDataFile (host, filename, timeout);
|
||||||
} else {
|
} else {
|
||||||
pandoraLog ("Transfer mode %s does not support file retrieval.", mode.c_str () );
|
pandoraLog ("Transfer mode %s does not support file retrieval.", mode.c_str () );
|
||||||
throw Pandora_Exception ();
|
throw Pandora_Exception ();
|
||||||
|
@ -61,19 +61,22 @@ namespace Pandora {
|
|||||||
string port,
|
string port,
|
||||||
string ssl,
|
string ssl,
|
||||||
string pass,
|
string pass,
|
||||||
string opts);
|
string opts,
|
||||||
|
int timeout);
|
||||||
int copyScpDataFile (string host,
|
int copyScpDataFile (string host,
|
||||||
string remote_path,
|
string remote_path,
|
||||||
string filename);
|
string filename);
|
||||||
int copyFtpDataFile (string host,
|
int copyFtpDataFile (string host,
|
||||||
string remote_path,
|
string remote_path,
|
||||||
string filename,
|
string filename,
|
||||||
string password);
|
string password,
|
||||||
|
int timeout);
|
||||||
int copyLocalDataFile (string remote_path,
|
int copyLocalDataFile (string remote_path,
|
||||||
string filename);
|
string filename);
|
||||||
void recvDataFile (string filename);
|
void recvDataFile (string filename);
|
||||||
void recvTentacleDataFile (string host,
|
void recvTentacleDataFile (string host,
|
||||||
string filename);
|
string filename,
|
||||||
|
int timeout);
|
||||||
|
|
||||||
int unzipCollection(string zip_path, string dest_dir);
|
int unzipCollection(string zip_path, string dest_dir);
|
||||||
void checkCollections ();
|
void checkCollections ();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user