2007-08-29 Esteban Sanchez <estebans@artica.es>

* pandora_windows_service.cc: Fixed a typo. Disconnect ftp client when
        an error happened.

        * ftp/pandora_ftp_client.cc: Set network timeout and ftp timeout.
        Ignore error when executing moving operations, this is compatible
        with FTP jails

        * windows/pandora_wmi.cc: Improved WQL queries and fixed some leaks.


git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@630 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Esteban Sanchez 2007-08-29 14:30:09 +00:00
parent 9f3515799a
commit d38e422804
5 changed files with 140 additions and 180 deletions

View File

@ -1,3 +1,14 @@
2007-08-29 Esteban Sanchez <estebans@artica.es>
* pandora_windows_service.cc: Fixed a typo. Disconnect ftp client when
an error happened.
* ftp/pandora_ftp_client.cc: Set network timeout and ftp timeout.
Ignore error when executing moving operations, this is compatible
with FTP jails
* windows/pandora_wmi.cc: Improved WQL queries and fixed some leaks.
2007-08-28 Esteban Sanchez <estebans@artica.es> 2007-08-28 Esteban Sanchez <estebans@artica.es>
* main.cc: Fixed an error with previous commit. * main.cc: Fixed an error with previous commit.

View File

@ -155,6 +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_FTP_RESPONSE_TIMEOUT, 60);
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,
@ -173,6 +175,8 @@ Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
switch (this->result) { switch (this->result) {
case CURLE_OK: case CURLE_OK:
case CURLE_FTP_QUOTE_ERROR: /* These error happens when FTP is in a jail.
Transfer was OK, moving wasn't. */
break; break;
case CURLE_COULDNT_CONNECT: case CURLE_COULDNT_CONNECT:
throw Unknown_Host (); throw Unknown_Host ();

View File

@ -238,18 +238,20 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
ftp_client.ftpFileFilename (remote_path + filename, ftp_client.ftpFileFilename (remote_path + filename,
filepath); filepath);
} catch (FTP::Unknown_Host e) { } catch (FTP::Unknown_Host e) {
pandoraLog ("Failed when copying to %s (%s)", host.c_str (), pandoraLog ("Pandora Agent: Failed when copying to %s (%s)",
ftp_client.getError ().c_str ()); host.c_str (), ftp_client.getError ().c_str ());
ftp_client.disconnect ();
throw e; throw e;
} catch (FTP::Authentication_Failed e) { } catch (FTP::Authentication_Failed e) {
pandoraLog ("Pandora Agent: Authentication Failed " pandoraLog ("Pandora Agent: Authentication Failed "
"when connecting to %s (%s)", "when connecting to %s (%s)",
host.c_str (), ftp_client.getError ().c_str ()); host.c_str (), ftp_client.getError ().c_str ());
ftp_client.disconnect ();
throw e; throw e;
} catch (FTP::FTP_Exception e) { } catch (FTP::FTP_Exception e) {
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 ());
ftp_client.disconnect ();
throw e; throw e;
} }
@ -275,7 +277,7 @@ Pandora_Windows_Service::copyDataFile (string filename)
copyScpDataFile (host, remote_path, filename); copyScpDataFile (host, remote_path, filename);
} else { } else {
pandoraLog ("Invalid transfer mode: %s." pandoraLog ("Invalid transfer mode: %s."
"Please rechak transfer_mode option " "Please recheck transfer_mode option "
"in configuration file."); "in configuration file.");
} }

View File

@ -24,6 +24,7 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <sstream>
using namespace std; using namespace std;
using namespace Pandora_Wmi; using namespace Pandora_Wmi;
@ -58,30 +59,18 @@ Pandora_Wmi::isProcessRunning (string process_name) {
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
string name; string name;
int result = 0; int result = 0;
string query;
struct QFix { query = "SELECT * FROM Win32_Process WHERE Name=\"" + process_name + "\"";
CDhStringA name, description, state; cout << "Query: " << query << endl;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
L".ExecQuery(%S)", L".ExecQuery(%T)",
L"SELECT * FROM Win32_Process")); query.c_str ()));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 };
dhGetValue (L"%s", &fix.name, quickfix,
L".Name");
name = fix.name;
transform (name.begin (), name.end (), name.begin (),
(int (*) (int)) tolower);
if (process_name == name) {
result++; result++;
}
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
pandoraLog ("isProcessRunning error. %s", errstr.c_str ()); pandoraLog ("isProcessRunning error. %s", errstr.c_str ());
@ -102,39 +91,26 @@ int
Pandora_Wmi::isServiceRunning (string service_name) { Pandora_Wmi::isServiceRunning (string service_name) {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
string name, state; string query;
char *state;
int retval;
struct QFix { query = "SELECT * FROM Win32_Service WHERE Name = \"" + service_name + "\"";
CDhStringA name, state;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
L".ExecQuery(%S)", L".ExecQuery(%T)",
L"SELECT * FROM Win32_Service")); query.c_str ()));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%s", &state, quickfix,
dhGetValue (L"%s", &fix.name, quickfix,
L".Name");
name = fix.name;
transform (name.begin (), name.end (), name.begin (),
(int (*) (int)) tolower);
if (service_name == name) {
dhGetValue (L"%s", &fix.state, quickfix,
L".State"); L".State");
state = fix.state;
if (state == "Running") { retval = (state == "Running") ? 1 : 0;
return 1; dhFreeString (state);
} else {
return 0; return retval;
}
}
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
pandoraLog ("isServiceRunning error. %s", errstr.c_str ()); pandoraLog ("isServiceRunning error. %s", errstr.c_str ());
@ -146,7 +122,7 @@ Pandora_Wmi::isServiceRunning (string service_name) {
/** /**
* Get the free space in a logical disk drive. * Get the free space in a logical disk drive.
* *
* @param disk_id Disk drive letter (C. * @param disk_id Disk drive letter (C: for example).
* *
* @return Free space amount in MB. * @return Free space amount in MB.
* *
@ -172,7 +148,6 @@ Pandora_Wmi::getDiskFreeSpace (string disk_id) {
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
dhGetValue (L"%d", &space, quickfix, dhGetValue (L"%d", &space, quickfix,
L".FreeSpace"); L".FreeSpace");
cout << space << endl;
return space / 1024 / 1024; return space / 1024 / 1024;
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
@ -197,39 +172,28 @@ int
Pandora_Wmi::getCpuUsagePercentage (int cpu_id) { Pandora_Wmi::getCpuUsagePercentage (int cpu_id) {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
string id, cpu_id_str; string query;
cpu_id_str = "CPU";
cpu_id_str += Pandora_Strutils::inttostr (cpu_id);
struct QFix {
CDhStringA id;
long load_percentage; long load_percentage;
}; std::ostringstream stm;
stm << cpu_id;
query = "SELECT * FROM Win32_Processor WHERE DeviceID = \"CPU" + stm.str () + "\"";
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
L".ExecQuery(%S)", L".ExecQuery(%T)",
L"SELECT * FROM Win32_Processor ")); query.c_str ()));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%d", &load_percentage, quickfix,
dhGetValue (L"%s", &fix.id, quickfix,
L".DeviceID");
id = fix.id;
if (cpu_id_str == id) {
dhGetValue (L"%d", &fix.load_percentage, quickfix,
L".LoadPercentage"); L".LoadPercentage");
return fix.load_percentage; return load_percentage;
}
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
cout << query << endl;
cout << errstr << endl;
pandoraLog ("getCpuUsagePercentage error. %s", errstr.c_str ()); pandoraLog ("getCpuUsagePercentage error. %s", errstr.c_str ());
} }
@ -247,10 +211,7 @@ long
Pandora_Wmi::getFreememory () { Pandora_Wmi::getFreememory () {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
struct QFix {
long free_memory; long free_memory;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
@ -259,12 +220,10 @@ Pandora_Wmi::getFreememory () {
L"SELECT * FROM Win32_PerfRawData_PerfOS_Memory ")); L"SELECT * FROM Win32_PerfRawData_PerfOS_Memory "));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%d", &free_memory, quickfix,
dhGetValue (L"%d", &fix.free_memory, quickfix,
L".AvailableMBytes"); L".AvailableMBytes");
return fix.free_memory; return free_memory;
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
pandoraLog ("getFreememory error. %s", errstr.c_str ()); pandoraLog ("getFreememory error. %s", errstr.c_str ());
@ -282,12 +241,9 @@ string
Pandora_Wmi::getOSName () { Pandora_Wmi::getOSName () {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
char *name = NULL;
string ret; string ret;
struct QFix {
CDhStringA name, state, description;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
@ -295,12 +251,11 @@ Pandora_Wmi::getOSName () {
L"SELECT * FROM Win32_OperatingSystem ")); L"SELECT * FROM Win32_OperatingSystem "));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%s", &name, quickfix,
dhGetValue (L"%s", &fix.name, quickfix,
L".Caption"); L".Caption");
ret = fix.name; ret = name;
dhFreeString (name);
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
@ -319,12 +274,9 @@ string
Pandora_Wmi::getOSVersion () { Pandora_Wmi::getOSVersion () {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
char *version = NULL;
string ret; string ret;
struct QFix {
CDhStringA name, state, description;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
@ -332,12 +284,11 @@ Pandora_Wmi::getOSVersion () {
L"SELECT * FROM Win32_OperatingSystem ")); L"SELECT * FROM Win32_OperatingSystem "));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%s", &version, quickfix,
dhGetValue (L"%s", &fix.name, quickfix,
L".CSDVersion"); L".CSDVersion");
ret = fix.name; ret = version;
dhFreeString (version);
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
@ -356,12 +307,9 @@ string
Pandora_Wmi::getOSBuild () { Pandora_Wmi::getOSBuild () {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
char *build = NULL;
string ret; string ret;
struct QFix {
CDhStringA name, state, description;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
@ -369,12 +317,11 @@ Pandora_Wmi::getOSBuild () {
L"SELECT * FROM Win32_OperatingSystem ")); L"SELECT * FROM Win32_OperatingSystem "));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%s", &build, quickfix,
dhGetValue (L"%s", &fix.name, quickfix,
L".Version"); L".Version");
ret = fix.name; ret = build;
dhFreeString (build);
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {
@ -393,12 +340,9 @@ string
Pandora_Wmi::getSystemName () { Pandora_Wmi::getSystemName () {
CDhInitialize init; CDhInitialize init;
CDispPtr wmi_svc, quickfixes; CDispPtr wmi_svc, quickfixes;
char *name = NULL;
string ret; string ret;
struct QFix {
CDhStringA name, state, description;
};
try { try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
@ -406,12 +350,11 @@ Pandora_Wmi::getSystemName () {
L"SELECT * FROM Win32_OperatingSystem ")); L"SELECT * FROM Win32_OperatingSystem "));
FOR_EACH (quickfix, quickfixes, NULL) { FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 }; dhGetValue (L"%s", &name, quickfix,
dhGetValue (L"%s", &fix.name, quickfix,
L".CSName"); L".CSName");
ret = fix.name; ret = name;
dhFreeString (name);
} NEXT_THROW (quickfix); } NEXT_THROW (quickfix);
} catch (string errstr) { } catch (string errstr) {