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> 2008-11-26 Esteban Sanchez <estebans@artica.es>
* pandora_strutils.cc, pandora_strutils.h, ftp/pandora_ftp_client.cc, * pandora_strutils.cc, pandora_strutils.h, ftp/pandora_ftp_client.cc,

View File

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

View File

@ -40,6 +40,7 @@ Pandora_Module::Pandora_Module (string name) {
this->max = 0; this->max = 0;
this->min = 0; this->min = 0;
this->has_limits = false; this->has_limits = false;
this->async = false;
this->data_list = NULL; this->data_list = NULL;
} }
@ -176,6 +177,21 @@ Pandora_Module::getModuleKind () const {
return this->module_kind; 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. * 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,7 +367,8 @@ Pandora_Module::getXml () {
data_element = new TiXmlElement ("data"); data_element = new TiXmlElement ("data");
element = new TiXmlElement ("value"); element = new TiXmlElement ("value");
try { try {
data_clean = strreplace (this->getDataOutput (data), "%", "%%" ); data_clean = strreplace (this->getDataOutput (data),
"%", "%%" );
} catch (Output_Error e) { } catch (Output_Error e) {
delete element; delete element;
continue; continue;
@ -427,6 +444,19 @@ Pandora_Module::setMin (int value) {
this->min = 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. * Set the module type from a string type.
* *

View File

@ -135,6 +135,11 @@ namespace Pandora_Modules {
* The description of the module. * The description of the module.
*/ */
string module_description; string module_description;
/**
* Flag to set a module as asynchronous
*/
bool async;
public: public:
Pandora_Module (string name); Pandora_Module (string name);
virtual ~Pandora_Module (); virtual ~Pandora_Module ();
@ -153,12 +158,14 @@ namespace Pandora_Modules {
virtual void run (); virtual void run ();
virtual void setOutput (string output); virtual void setOutput (string output);
virtual void setOutput (string output, SYSTEMTIME *system_time); virtual void setOutput (string output,
SYSTEMTIME *system_time);
string getName () const; string getName () const;
string getDescription () const; string getDescription () const;
string getTypeString () const; string getTypeString () const;
string getLatestOutput () const;
Module_Type getTypeInt () const; Module_Type getTypeInt () const;
Module_Type getModuleType () const; Module_Type getModuleType () const;
Module_Kind getModuleKind () const; Module_Kind getModuleKind () const;
@ -168,6 +175,7 @@ namespace Pandora_Modules {
void setDescription (string description); void setDescription (string description);
void setMax (int value); void setMax (int value);
void setMin (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_SOURCE ("module_source ")
#define TOKEN_EVENTTYPE ("module_eventtype ") #define TOKEN_EVENTTYPE ("module_eventtype ")
#define TOKEN_PATTERN ("module_pattern ") #define TOKEN_PATTERN ("module_pattern ")
#define TOKEN_ASYNC ("module_async")
string string
parseLine (string line, string token) { parseLine (string line, string token) {
@ -87,7 +88,8 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
string module_interval, module_proc, module_service; string module_interval, module_proc, module_service;
string module_freedisk, module_cpuusage, module_odbc; string module_freedisk, module_cpuusage, module_odbc;
string module_odbc_query, module_dsn, module_freememory; 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; Pandora_Module *module;
bool numeric; bool numeric;
Module_Type type; Module_Type type;
@ -172,6 +174,9 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_pattern == "") { if (module_pattern == "") {
module_pattern = parseLine (line, TOKEN_PATTERN); module_pattern = parseLine (line, TOKEN_PATTERN);
} }
if (module_async == "") {
module_async = parseLine (line, TOKEN_ASYNC);
}
iter++; iter++;
} }
@ -222,6 +227,10 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module->setDescription (module_description); module->setDescription (module_description);
} }
if (module_async != "") {
module->setAsync (true);
}
type = Pandora_Module::parseModuleTypeFromString (module_type); type = Pandora_Module::parseModuleTypeFromString (module_type);
switch (type) { switch (type) {
case TYPE_GENERIC_DATA: case TYPE_GENERIC_DATA:

View File

@ -80,9 +80,27 @@ Pandora_Modules::Pandora_Module_List::Pandora_Module_List (string filename) {
file.close (); file.close ();
current = new std::list<Pandora_Module *>::iterator (); 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. * Destroy the list.
* *

View File

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

View File

@ -20,8 +20,10 @@
*/ */
#include "pandora_module_service.h" #include "pandora_module_service.h"
#include "pandora_module_list.h"
#include "../windows/pandora_wmi.h" #include "../windows/pandora_wmi.h"
#include "../pandora_strutils.h" #include "../pandora_strutils.h"
#include "../pandora_windows_service.h"
#include <algorithm> #include <algorithm>
#include <cctype> #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->service_name.begin (), (int (*) (int)) tolower);
this->setKind (module_service_str); 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 void
@ -58,4 +136,12 @@ Pandora_Module_Service::run () {
res = Pandora_Wmi::isServiceRunning (this->service_name); res = Pandora_Wmi::isServiceRunning (this->service_name);
this->setOutput (inttostr (res)); 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 { class Pandora_Module_Service : public Pandora_Module {
private: private:
string service_name; string service_name;
HANDLE thread;
public: public:
Pandora_Module_Service (string name, string service_name); 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", ""}; 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_name Internal service name
* @param svc_display_name Service name that will appear in the * @param svc_display_name Service name that will appear in the
* Windows service administration tool. * Windows service administration tool.
* @param svc_description Long description of the service. * @param svc_description Long description of the service.
*/ */
Pandora_Windows_Service::Pandora_Windows_Service (const char * svc_name, void
Pandora_Windows_Service::setValues (const char * svc_name,
const char * svc_display_name, const char * svc_display_name,
const char * svc_description) const char * svc_description) {
: Windows_Service (svc_name, svc_display_name, svc_description) { this->service_name = (char *) svc_name;
this->setInitFunction ((void (Windows_Service::*) ()) this->service_display_name = (char *) svc_display_name;
&Pandora_Windows_Service::pandora_init); this->service_description = (char *) svc_description;
this->setRunFunction ((void (Windows_Service::*) ())
&Pandora_Windows_Service::pandora_run);
execution_number = 0; execution_number = 0;
this->modules = NULL; this->modules = NULL;
this->conf = NULL; this->conf = NULL;
@ -79,6 +86,23 @@ Pandora_Windows_Service::~Pandora_Windows_Service () {
pandoraLog ("Pandora agent stopped"); 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 bool
is_enabled (string value) { is_enabled (string value) {
int i = 0; int i = 0;
@ -604,7 +628,7 @@ Pandora_Windows_Service::checkConfig () {
} }
void void
Pandora_Windows_Service::pandora_run () { Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
TiXmlDeclaration *decl; TiXmlDeclaration *decl;
TiXmlDocument *doc; TiXmlDocument *doc;
TiXmlElement *local_xml, *agent; TiXmlElement *local_xml, *agent;
@ -612,41 +636,24 @@ Pandora_Windows_Service::pandora_run () {
string tmp_filename, tmp_filepath; string tmp_filename, tmp_filepath;
string encoding; string encoding;
bool saved; bool saved;
static HANDLE mutex = 0;
pandoraDebug ("Run begin"); if (mutex == 0) {
mutex = CreateMutex (NULL, FALSE, NULL);
/* Check for configuration changes */
this->checkConfig ();
execution_number++;
if (this->modules != NULL) {
this->modules->goFirst ();
while (! this->modules->isLast ()) {
Pandora_Module *module;
module = this->modules->getCurrentValue ();
pandoraDebug ("Run %s", module->getName ().c_str ());
module->run ();
this->modules->goNext ();
}
} }
/* Wait for the mutex to be opened */
WaitForSingleObject (mutex, INFINITE);
this->elapsed_transfer_time += interval; pandoraLog ("aasdfasdf");
if (this->elapsed_transfer_time >= this->transfer_interval) {
agent = getXmlHeader (); agent = getXmlHeader ();
if (this->modules != NULL) { if (modules != NULL) {
this->modules->goFirst (); modules->goFirst ();
while (! this->modules->isLast ()) { while (! modules->isLast ()) {
Pandora_Module *module; Pandora_Module *module;
module = this->modules->getCurrentValue (); module = modules->getCurrentValue ();
local_xml = module->getXml (); local_xml = module->getXml ();
if (local_xml != NULL) { if (local_xml != NULL) {
@ -654,11 +661,10 @@ Pandora_Windows_Service::pandora_run () {
delete local_xml; delete local_xml;
} }
this->modules->goNext (); modules->goNext ();
} }
} }
this->elapsed_transfer_time = 0;
/* Generate temporal filename */ /* Generate temporal filename */
random_integer = inttostr (rand()); random_integer = inttostr (rand());
tmp_filename = conf->getValue ("agent_name"); tmp_filename = conf->getValue ("agent_name");
@ -703,6 +709,40 @@ Pandora_Windows_Service::pandora_run () {
} catch (Pandora_File::Delete_Error e) { } catch (Pandora_File::Delete_Error e) {
} }
} }
ReleaseMutex (mutex);
}
void
Pandora_Windows_Service::pandora_run () {
pandoraDebug ("Run begin");
/* Check for configuration changes */
this->checkConfig ();
execution_number++;
if (this->modules != NULL) {
this->modules->goFirst ();
while (! this->modules->isLast ()) {
Pandora_Module *module;
module = this->modules->getCurrentValue ();
pandoraDebug ("Run %s", module->getName ().c_str ());
module->run ();
this->modules->goNext ();
}
}
this->elapsed_transfer_time += this->interval;
if (this->elapsed_transfer_time >= this->transfer_interval) {
this->elapsed_transfer_time = 0;
this->sendXml (this->modules);
} }
/* Get the interval value (in minutes) */ /* Get the interval value (in minutes) */

View File

@ -44,6 +44,7 @@ namespace Pandora {
long interval; long interval;
long elapsed_transfer_time; long elapsed_transfer_time;
long transfer_interval; long transfer_interval;
bool started;
TiXmlElement *getXmlHeader (); TiXmlElement *getXmlHeader ();
void copyDataFile (string filename); void copyDataFile (string filename);
@ -59,14 +60,22 @@ namespace Pandora {
void recvTentacleDataFile (string host, void recvTentacleDataFile (string host,
string filename); string filename);
void checkConfig (); void checkConfig ();
Pandora_Windows_Service ();
public: public:
void pandora_run (); void pandora_run ();
void pandora_init (); void pandora_init ();
public: public:
Pandora_Windows_Service (const char * svc_name, static Pandora_Windows_Service *getInstance ();
const char * svc_display_name,
const char * svc_description);
~Pandora_Windows_Service (); ~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. * Notice: A program should have only one object of this class.
*/ */
class Windows_Service { class Windows_Service {
private: protected:
char *service_name; char *service_name;
char *service_display_name; char *service_display_name;
char *service_description; char *service_description;
private:
HANDLE stop_event; HANDLE stop_event;
int sleep_time; int sleep_time;
SC_HANDLE sc_service; SC_HANDLE sc_service;