2008-11-28 Esteban Sanchez <estebans@artica.es>

* modules/pandora_module.[cc,h]: Added an async property. Added
	getLatestOutput() to get the latest output of a module.

	* modules/pandora_module_factory.cc: Parse async token in modules.

	* modules/pandora_module_list.[cc,h]: It can create empty lists now
	and add modules using new addModule() function.

	* modules/pandora_module_service.[cc,h]: Added an async thread to
	watch services events and track services status changes quickly and
	asynchronously.

	* windows/pandora_wmi.cc: Style correction.

	* pandora_windows_service.[cc,h]: Pandora Windows service object is
	now single instance, so the instance to the running service can be
	accessed anywhere (useful in async threads). The XML generation and
	sending process has been moved to a new function called sendXml()
	which is thread-safe.

	* windows_service.h: Some properties are now protected instead of
	public to make it easier the singleton pattern in child classes.

	* main.cc: Adopted to singleton pattern in Pandora windows service.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1268 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
esanchezm 2008-11-28 10:51:03 +00:00
parent 11e9ad236b
commit 7427997eee
14 changed files with 804 additions and 568 deletions

View File

@ -1,3 +1,30 @@
2008-11-28 Esteban Sanchez <estebans@artica.es>
* modules/pandora_module.[cc,h]: Added an async property. Added
getLatestOutput() to get the latest output of a module.
* modules/pandora_module_factory.cc: Parse async token in modules.
* modules/pandora_module_list.[cc,h]: It can create empty lists now
and add modules using new addModule() function.
* modules/pandora_module_service.[cc,h]: Added an async thread to
watch services events and track services status changes quickly and
asynchronously.
* windows/pandora_wmi.cc: Style correction.
* pandora_windows_service.[cc,h]: Pandora Windows service object is
now single instance, so the instance to the running service can be
accessed anywhere (useful in async threads). The XML generation and
sending process has been moved to a new function called sendXml()
which is thread-safe.
* windows_service.h: Some properties are now protected instead of
public to make it easier the singleton pattern in child classes.
* main.cc: Adopted to singleton pattern in Pandora windows service.
2008-11-26 Esteban Sanchez <estebans@artica.es>
* pandora_strutils.cc, pandora_strutils.h, ftp/pandora_ftp_client.cc,

View File

@ -37,8 +37,10 @@ main (int argc, char *argv[]) {
string aux;
unsigned int pos;
service = new Pandora_Windows_Service (Pandora::name, Pandora::display_name,
Pandora::description);
service = Pandora_Windows_Service::getInstance ();
service->setValues (Pandora::name, Pandora::display_name,
Pandora::description);
service->start ();
GetModuleFileName (NULL, buffer, MAX_PATH);
aux = buffer;

View File

@ -40,6 +40,7 @@ Pandora_Module::Pandora_Module (string name) {
this->max = 0;
this->min = 0;
this->has_limits = false;
this->async = false;
this->data_list = NULL;
}
@ -176,6 +177,21 @@ Pandora_Module::getModuleKind () const {
return this->module_kind;
}
/**
* Get the output of the module.
*
* @return The module output in a string value.
*/
string
Pandora_Module::getLatestOutput () const {
list<Pandora_Data *>::iterator iter;
if (this->data_list == NULL)
return "";
iter = this->data_list->begin ();
return (*iter)->getValue ();
}
/**
* Get the type of the module in a integer_value.
*
@ -222,7 +238,7 @@ Pandora_Module::getDataOutput (Pandora_Data *data) {
}
}
return trim(data->getValue ());
return trim (data->getValue ());
}
/**
@ -351,15 +367,16 @@ Pandora_Module::getXml () {
data_element = new TiXmlElement ("data");
element = new TiXmlElement ("value");
try {
data_clean = strreplace (this->getDataOutput (data), "%", "%%" );
} catch (Output_Error e) {
delete element;
continue;
}
data_clean = strreplace (this->getDataOutput (data),
"%", "%%" );
} catch (Output_Error e) {
delete element;
continue;
}
text = new TiXmlText (data_clean);
element->InsertEndChild (*text);
data_element->InsertEndChild (*element);
text = new TiXmlText (data_clean);
element->InsertEndChild (*text);
data_element->InsertEndChild (*element);
delete text;
delete element;
@ -379,11 +396,11 @@ Pandora_Module::getXml () {
data = data_list->front ();
element = new TiXmlElement ("data");
try {
data_clean = strreplace (this->getDataOutput (data), "%", "%%" );
text = new TiXmlText (data_clean);
element->InsertEndChild (*text);
root->InsertEndChild (*element);
delete text;
data_clean = strreplace (this->getDataOutput (data), "%", "%%" );
text = new TiXmlText (data_clean);
element->InsertEndChild (*text);
root->InsertEndChild (*element);
delete text;
} catch (Output_Error e) {
}
delete element;
@ -427,6 +444,19 @@ Pandora_Module::setMin (int value) {
this->min = value;
}
/**
* Set the async flag to the module.
*
* If a module is set to be async, it would try to works only when the
* events happen. Note that not all the modules can work in async mode.
*
* @param async Flag to set.
*/
void
Pandora_Module::setAsync (bool async) {
this->async = async;
}
/**
* Set the module type from a string type.
*

View File

@ -80,7 +80,7 @@ namespace Pandora_Modules {
const string module_freememory_str = "module_freememory";
const string module_cpuusage_str = "module_cpuusage";
const string module_odbc_str = "module_odbc";
const string module_logevent_str = "module_logevent";
const string module_logevent_str = "module_logevent";
/**
* Pandora module super-class exception.
@ -135,6 +135,11 @@ namespace Pandora_Modules {
* The description of the module.
*/
string module_description;
/**
* Flag to set a module as asynchronous
*/
bool async;
public:
Pandora_Module (string name);
virtual ~Pandora_Module ();
@ -145,29 +150,32 @@ namespace Pandora_Modules {
static Module_Kind
parseModuleKindFromString (string kind);
void setInterval (int interval);
int getInterval ();
void setInterval (int interval);
int getInterval ();
TiXmlElement *getXml ();
TiXmlElement *getXml ();
virtual void run ();
virtual void run ();
virtual void setOutput (string output);
virtual void setOutput (string output, SYSTEMTIME *system_time);
virtual void setOutput (string output);
virtual void setOutput (string output,
SYSTEMTIME *system_time);
string getName () const;
string getDescription () const;
string getTypeString () const;
Module_Type getTypeInt () const;
Module_Type getModuleType () const;
Module_Kind getModuleKind () const;
string getName () const;
string getDescription () const;
string getTypeString () const;
string getLatestOutput () const;
Module_Type getTypeInt () const;
Module_Type getModuleType () const;
Module_Kind getModuleKind () const;
void setType (string type);
void setKind (string kind);
void setDescription (string description);
void setMax (int value);
void setMin (int value);
void setType (string type);
void setKind (string kind);
void setDescription (string description);
void setMax (int value);
void setMin (int value);
void setAsync (bool async);
};
}

View File

@ -53,6 +53,7 @@ using namespace Pandora_Strutils;
#define TOKEN_SOURCE ("module_source ")
#define TOKEN_EVENTTYPE ("module_eventtype ")
#define TOKEN_PATTERN ("module_pattern ")
#define TOKEN_ASYNC ("module_async")
string
parseLine (string line, string token) {
@ -87,7 +88,8 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
string module_interval, module_proc, module_service;
string module_freedisk, module_cpuusage, module_odbc;
string module_odbc_query, module_dsn, module_freememory;
string module_logevent, module_source, module_eventtype, module_pattern;
string module_logevent, module_source, module_eventtype;
string module_pattern, module_async;
Pandora_Module *module;
bool numeric;
Module_Type type;
@ -104,10 +106,10 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module_odbc = "";
module_odbc_query = "";
module_odbc = "";
module_logevent = "";
module_source = "";
module_eventtype = "";
module_pattern = "";
module_logevent = "";
module_source = "";
module_eventtype = "";
module_pattern = "";
stringtok (tokens, definition, "\n");
@ -160,18 +162,21 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_odbc_query == "") {
module_odbc_query = parseLine (line, TOKEN_ODBC_QUERY);
}
if (module_logevent == "") {
module_logevent = parseLine (line, TOKEN_LOGEVENT);
}
if (module_source == "") {
module_source = parseLine (line, TOKEN_SOURCE);
}
if (module_eventtype == "") {
module_eventtype = parseLine (line, TOKEN_EVENTTYPE);
}
if (module_pattern == "") {
module_pattern = parseLine (line, TOKEN_PATTERN);
}
if (module_logevent == "") {
module_logevent = parseLine (line, TOKEN_LOGEVENT);
}
if (module_source == "") {
module_source = parseLine (line, TOKEN_SOURCE);
}
if (module_eventtype == "") {
module_eventtype = parseLine (line, TOKEN_EVENTTYPE);
}
if (module_pattern == "") {
module_pattern = parseLine (line, TOKEN_PATTERN);
}
if (module_async == "") {
module_async = parseLine (line, TOKEN_ASYNC);
}
iter++;
}
@ -222,6 +227,10 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module->setDescription (module_description);
}
if (module_async != "") {
module->setAsync (true);
}
type = Pandora_Module::parseModuleTypeFromString (module_type);
switch (type) {
case TYPE_GENERIC_DATA:
@ -258,7 +267,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
}
}
if (module_min != "") {
try {
try {
int value = strtoint (module_min);
module->setMin (value);

View File

@ -80,9 +80,27 @@ Pandora_Modules::Pandora_Module_List::Pandora_Module_List (string filename) {
file.close ();
current = new std::list<Pandora_Module *>::iterator ();
*current = modules->begin ();
(*current) = modules->begin ();
}
/**
* Creates an empty module list object.
*/
Pandora_Modules::Pandora_Module_List::Pandora_Module_List () {
this->modules = new list<Pandora_Module *> ();
current = new std::list<Pandora_Module *>::iterator ();
(*current) = modules->begin ();
}
/**
* Adds a module object to a list object.
*/
void
Pandora_Modules::Pandora_Module_List::addModule (Pandora_Module *module) {
modules->push_back (module);
}
/**
* Destroy the list.
*

View File

@ -43,11 +43,15 @@ namespace Pandora_Modules {
void parseModuleDefinition (string definition);
public:
Pandora_Module_List (string filename);
Pandora_Module_List ();
~Pandora_Module_List ();
Pandora_Module * getCurrentValue ();
/* Add a module to the list */
void addModule (Pandora_Module *module);
/* Move to the first element of the list */
void goFirst ();

View File

@ -20,8 +20,10 @@
*/
#include "pandora_module_service.h"
#include "pandora_module_list.h"
#include "../windows/pandora_wmi.h"
#include "../pandora_strutils.h"
#include "../pandora_windows_service.h"
#include <algorithm>
#include <cctype>
@ -44,6 +46,82 @@ Pandora_Module_Service::Pandora_Module_Service (string name, string service_name
this->service_name.begin (), (int (*) (int)) tolower);
this->setKind (module_service_str);
this->thread = 0;
}
string
Pandora_Module_Service::getServiceName () const {
return this->service_name;
}
#define BUFFER_SIZE (16384)
void
async_run (Pandora_Module_Service *module) {
HANDLE event_log;
HANDLE event;
DWORD result;
int res;
string str_res;
BYTE buffer[BUFFER_SIZE];
EVENTLOGRECORD *record;
DWORD read;
DWORD needed;
int event_id;
bool service_event;
string prev_res;
Pandora_Module_List *modules;
prev_res = module->getLatestOutput ();
modules = new Pandora_Module_List ();
modules->addModule (module);
while (1) {
event_log = OpenEventLog (NULL, "Service Control Manager");
if (event_log == NULL) {
pandoraLog ("Could not open event log for %s.",
module->getServiceName ().c_str ());
return;
}
event = CreateEvent (NULL, FALSE, FALSE, NULL);
NotifyChangeEventLog (event_log, event);
result = WaitForSingleObject (event, 10000);
if (result == 0) {
service_event = false;
record = (EVENTLOGRECORD *) buffer;
while (ReadEventLog (event_log,
EVENTLOG_FORWARDS_READ | EVENTLOG_SEQUENTIAL_READ,
0, record, BUFFER_SIZE, &read, &needed)) {
if (record->EventType != EVENTLOG_INFORMATION_TYPE)
continue;
event_id = record->EventID & 0x0000ffff;
/* Those numbers are the code for service start/stopping */
if (event_id == 7035 || event_id == 7036) {
service_event = true;
break;
}
}
if (service_event) {
res = Pandora_Wmi::isServiceRunning (module->getServiceName ());
str_res = inttostr (res);
if (str_res != prev_res) {
module->setOutput (str_res);
prev_res = str_res;
pandoraLog ("Service \"%s\" changed status to: %d",
module->getServiceName ().c_str (), res);
Pandora_Windows_Service::getInstance ()->sendXml (modules);
}
}
}
CloseHandle (event);
CloseEventLog (event_log);
}
delete modules;
}
void
@ -58,4 +136,12 @@ Pandora_Module_Service::run () {
res = Pandora_Wmi::isServiceRunning (this->service_name);
this->setOutput (inttostr (res));
/* Launch thread if it's asynchronous */
if (this->async) {
this->thread = CreateThread (NULL, 0,
(LPTHREAD_START_ROUTINE) async_run,
this, 0, NULL);
this->async = false;
}
}

View File

@ -32,10 +32,12 @@ namespace Pandora_Modules {
class Pandora_Module_Service : public Pandora_Module {
private:
string service_name;
HANDLE thread;
public:
Pandora_Module_Service (string name, string service_name);
void run ();
void run ();
string getServiceName () const;
};
}

View File

@ -40,23 +40,30 @@ using namespace Pandora_Strutils;
string enabled_values[] = {"enabled", "1", "on", "yes", "si", "", "ok", ""};
Pandora_Windows_Service::Pandora_Windows_Service ()
: Windows_Service (NULL, NULL, NULL) {
this->setInitFunction ((void (Windows_Service::*) ())
&Pandora_Windows_Service::pandora_init);
this->setRunFunction ((void (Windows_Service::*) ())
&Pandora_Windows_Service::pandora_run);
this->started = false;
}
/**
* Creates a new Pandora_Windows_Service.
* Set Pandora service Windows properties.
*
* @param svc_name Internal service name
* @param svc_display_name Service name that will appear in the
* Windows service administration tool.
* @param svc_description Long description of the service.
*/
Pandora_Windows_Service::Pandora_Windows_Service (const char * svc_name,
const char * svc_display_name,
const char * svc_description)
: Windows_Service (svc_name, svc_display_name, svc_description) {
this->setInitFunction ((void (Windows_Service::*) ())
&Pandora_Windows_Service::pandora_init);
this->setRunFunction ((void (Windows_Service::*) ())
&Pandora_Windows_Service::pandora_run);
void
Pandora_Windows_Service::setValues (const char * svc_name,
const char * svc_display_name,
const char * svc_description) {
this->service_name = (char *) svc_name;
this->service_display_name = (char *) svc_display_name;
this->service_description = (char *) svc_description;
execution_number = 0;
this->modules = NULL;
this->conf = NULL;
@ -79,6 +86,23 @@ Pandora_Windows_Service::~Pandora_Windows_Service () {
pandoraLog ("Pandora agent stopped");
}
Pandora_Windows_Service *
Pandora_Windows_Service::getInstance () {
static Pandora_Windows_Service *service = NULL;
if (service != NULL)
return service;
service = new Pandora_Windows_Service ();
return service;
}
void
Pandora_Windows_Service::start () {
this->started = true;
}
bool
is_enabled (string value) {
int i = 0;
@ -161,7 +185,7 @@ Pandora_Windows_Service::getXmlHeader () {
// Get current time
ctime = time(0);
ctime_tm = localtime(&ctime);
ctime_tm = localtime(&ctime);
sprintf (timestamp, "%d-%02d-%02d %02d:%02d:%02d", ctime_tm->tm_year + 1900,
ctime_tm->tm_mon + 1, ctime_tm->tm_mday, ctime_tm->tm_hour,
@ -604,15 +628,93 @@ Pandora_Windows_Service::checkConfig () {
}
void
Pandora_Windows_Service::pandora_run () {
Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
TiXmlDeclaration *decl;
TiXmlDocument *doc;
TiXmlElement *local_xml, *agent;
string xml_filename, random_integer;
string tmp_filename, tmp_filepath;
string encoding;
bool saved;
TiXmlDocument *doc;
TiXmlElement *local_xml, *agent;
string xml_filename, random_integer;
string tmp_filename, tmp_filepath;
string encoding;
bool saved;
static HANDLE mutex = 0;
if (mutex == 0) {
mutex = CreateMutex (NULL, FALSE, NULL);
}
/* Wait for the mutex to be opened */
WaitForSingleObject (mutex, INFINITE);
pandoraLog ("aasdfasdf");
agent = getXmlHeader ();
if (modules != NULL) {
modules->goFirst ();
while (! modules->isLast ()) {
Pandora_Module *module;
module = modules->getCurrentValue ();
local_xml = module->getXml ();
if (local_xml != NULL) {
agent->InsertEndChild (*local_xml);
delete local_xml;
}
modules->goNext ();
}
}
/* Generate temporal filename */
random_integer = inttostr (rand());
tmp_filename = conf->getValue ("agent_name");
if (tmp_filename == "") {
tmp_filename = Pandora_Windows_Info::getSystemName ();
}
tmp_filename += "." + random_integer + ".data";
xml_filename = conf->getValue ("temporal");
if (xml_filename[xml_filename.length () - 1] != '\\') {
xml_filename += "\\";
}
tmp_filepath = xml_filename + tmp_filename;
/* Copy the XML to temporal file */
encoding = conf->getValue ("encoding");
if (encoding == "") {
encoding = "ISO-8859-1";
}
pandoraDebug ("Copying XML on %s", tmp_filepath.c_str ());
decl = new TiXmlDeclaration( "1.0", encoding.c_str(), "" );
doc = new TiXmlDocument (tmp_filepath);
doc->InsertEndChild (*decl);
doc->InsertEndChild (*agent);
saved = doc->SaveFile();
delete doc;
delete agent;
if (!saved) {
pandoraLog ("Error when saving the XML in %s",
tmp_filepath.c_str ());
return;
}
/* Only send if debug is not activated */
if (getPandoraDebug () == false) {
this->copyDataFile (tmp_filename);
try {
Pandora_File::removeFile (tmp_filepath);
} catch (Pandora_File::Delete_Error e) {
}
}
ReleaseMutex (mutex);
}
void
Pandora_Windows_Service::pandora_run () {
pandoraDebug ("Run begin");
/* Check for configuration changes */
@ -635,74 +737,12 @@ Pandora_Windows_Service::pandora_run () {
}
}
this->elapsed_transfer_time += interval;
this->elapsed_transfer_time += this->interval;
if (this->elapsed_transfer_time >= this->transfer_interval) {
agent = getXmlHeader ();
if (this->modules != NULL) {
this->modules->goFirst ();
while (! this->modules->isLast ()) {
Pandora_Module *module;
module = this->modules->getCurrentValue ();
local_xml = module->getXml ();
if (local_xml != NULL) {
agent->InsertEndChild (*local_xml);
delete local_xml;
}
this->modules->goNext ();
}
}
this->elapsed_transfer_time = 0;
/* Generate temporal filename */
random_integer = inttostr (rand());
tmp_filename = conf->getValue ("agent_name");
if (tmp_filename == "") {
tmp_filename = Pandora_Windows_Info::getSystemName ();
}
tmp_filename += "." + random_integer + ".data";
xml_filename = conf->getValue ("temporal");
if (xml_filename[xml_filename.length () - 1] != '\\') {
xml_filename += "\\";
}
tmp_filepath = xml_filename + tmp_filename;
/* Copy the XML to temporal file */
encoding = conf->getValue ("encoding");
if (encoding == "") {
encoding = "ISO-8859-1";
}
pandoraDebug ("Copying XML on %s", tmp_filepath.c_str ());
decl = new TiXmlDeclaration( "1.0", encoding.c_str(), "" );
doc = new TiXmlDocument (tmp_filepath);
doc->InsertEndChild (*decl);
doc->InsertEndChild (*agent);
saved = doc->SaveFile();
delete doc;
delete agent;
if (!saved) {
pandoraLog ("Error when saving the XML in %s",
tmp_filepath.c_str ());
return;
}
/* Only send if debug is not activated */
if (getPandoraDebug () == false) {
this->copyDataFile (tmp_filename);
try {
Pandora_File::removeFile (tmp_filepath);
} catch (Pandora_File::Delete_Error e) {
}
}
this->sendXml (this->modules);
}
/* Get the interval value (in minutes) */

View File

@ -44,6 +44,7 @@ namespace Pandora {
long interval;
long elapsed_transfer_time;
long transfer_interval;
bool started;
TiXmlElement *getXmlHeader ();
void copyDataFile (string filename);
@ -59,14 +60,22 @@ namespace Pandora {
void recvTentacleDataFile (string host,
string filename);
void checkConfig ();
Pandora_Windows_Service ();
public:
void pandora_run ();
void pandora_init ();
public:
Pandora_Windows_Service (const char * svc_name,
const char * svc_display_name,
const char * svc_description);
static Pandora_Windows_Service *getInstance ();
~Pandora_Windows_Service ();
void setValues (const char *svc_name,
const char *svc_display_name,
const char *svc_description);
void start ();
void sendXml (Pandora_Module_List *modules);
};
}

View File

@ -35,10 +35,11 @@
* Notice: A program should have only one object of this class.
*/
class Windows_Service {
private:
protected:
char *service_name;
char *service_display_name;
char *service_description;
private:
HANDLE stop_event;
int sleep_time;
SC_HANDLE sc_service;