2011-08-29 Ramon Novoa <rnovoa@artica.es>

* modules/pandora_module_regexp.cc,
	  modules/pandora_module_regexp.h,
	  modules/pandora_module_factory.cc: Check file size to detect if the
	  file was truncated. Added a new configuration token to avoid seeking
	  to the eof.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4844 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
ramonn 2011-08-29 17:17:33 +00:00
parent 235c446437
commit 2f09c48527
4 changed files with 50 additions and 12 deletions

View File

@ -1,3 +1,11 @@
2011-08-29 Ramon Novoa <rnovoa@artica.es>
* modules/pandora_module_regexp.cc,
modules/pandora_module_regexp.h,
modules/pandora_module_factory.cc: Check file size to detect if the
file was truncated. Added a new configuration token to avoid seeking
to the eof.
2011-08-29 Dario Rodriguez <dario.rodriguez@artica.es>
* modules/pandora_module_factory.cc: Fixed a bug collecting some module

View File

@ -92,6 +92,7 @@ using namespace Pandora_Strutils;
#define TOKEN_CRONTAB ("module_crontab ")
#define TOKEN_CRONINTERVAL ("module_cron_interval ")
#define TOKEN_PRECONDITION ("module_precondition ")
#define TOKEN_NOSEEKEOF ("module_noseekeof ")
string
parseLine (string line, string token) {
@ -137,7 +138,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
string module_plugin, module_save, module_condition, module_precondition;
string module_crontab, module_cron_interval, module_post_process;
string module_min_critical, module_max_critical, module_min_warning, module_max_warning;
string module_disabled, module_min_ff_event;
string module_disabled, module_min_ff_event, module_noseekeof;
Pandora_Module *module;
bool numeric;
Module_Type type;
@ -190,6 +191,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module_max_warning = "";
module_disabled = "";
module_min_ff_event = "";
module_noseekeof = "";
stringtok (tokens, definition, "\n");
@ -359,6 +361,9 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_cron_interval == "") {
module_cron_interval = parseLine (line, TOKEN_CRONINTERVAL);
}
if (module_noseekeof == "") {
module_noseekeof = parseLine (line, TOKEN_NOSEEKEOF);
}
iter++;
}
@ -447,7 +452,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
} else if (module_tcpcheck != "") {
module = new Pandora_Module_Tcpcheck (module_name, module_tcpcheck, module_port, module_timeout);
} else if (module_regexp != "") {
module = new Pandora_Module_Regexp (module_name, module_regexp, module_pattern);
module = new Pandora_Module_Regexp (module_name, module_regexp, module_pattern, (unsigned char) atoi (module_noseekeof.c_str ()));
} else if (module_plugin != "") {
module = new Pandora_Module_Plugin (module_name, module_plugin);
} else {

View File

@ -36,11 +36,12 @@ using namespace Pandora_Modules;
* @param source File to search.
* @param pattern Regular expression to match.
*/
Pandora_Module_Regexp::Pandora_Module_Regexp (string name, string source, string pattern)
Pandora_Module_Regexp::Pandora_Module_Regexp (string name, string source, string pattern, unsigned char no_seek_eof)
: Pandora_Module (name) {
this->source = source;
this->no_seek_eof = no_seek_eof;
// Compile the regular expression
if (regcomp (&this->regexp, pattern.c_str (), REG_EXTENDED) != 0) {
pandoraLog ("Invalid regular expression %s", pattern.c_str ());
@ -48,12 +49,15 @@ Pandora_Module_Regexp::Pandora_Module_Regexp (string name, string source, string
// Open the file and skip to the end
this->file.open (source.c_str ());
if (this->file.is_open ()) {
if (this->file.is_open () && no_seek_eof == 0) {
this->file.seekg (0, ios_base::end);
} else {
pandoraLog ("Error opening file %s", source.c_str ());
}
// Set file size to 0
this->size = 0;
this->setKind (module_regexp_str);
}
@ -71,7 +75,8 @@ Pandora_Module_Regexp::run () {
string line;
ostringstream output;
Module_Type type;
struct stat file_stat;
type = this->getTypeInt ();
// Run
@ -83,9 +88,18 @@ Pandora_Module_Regexp::run () {
// Check if the file could be opened
if (! file.is_open () || ! file.good ()) {
this->restart ();
return;
this->restart (this->no_seek_eof);
}
// Check if the file was truncated
if (this->no_seek_eof == 0 && stat (this->source.c_str (), &file_stat ) == 0) {
if (file_stat.st_size < this->size) {
this->restart (1);
}
// Save current file size
this->size = file_stat.st_size;
}
// Read new lines
count = 0;
@ -119,17 +133,24 @@ Pandora_Module_Regexp::run () {
// Clear the EOF flag
file.clear ();
// Next time the file will be opened again and read from the start
if (this->no_seek_eof == 1) {
this->file.close();
}
}
/**
* Closes, re-opens and seeks to the end of the current file.
*/
void
Pandora_Module_Regexp::restart () {
Pandora_Module_Regexp::restart (unsigned char no_seek_eof) {
this->file.close ();
this->file.open (this->source.c_str ());
if (this->file.is_open ()) {
this->file.seekg (0, ios_base::end);
if (no_seek_eof == 0) {
this->file.seekg (0, ios_base::end);
}
return;
}

View File

@ -25,6 +25,7 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include "pandora_module.h"
#include "boost/regex.h"
@ -40,10 +41,13 @@ namespace Pandora_Modules {
string source;
ifstream file;
regex_t regexp;
void restart ();
off_t size;
unsigned char no_seek_eof;
void restart (unsigned char seek_eof);
public:
Pandora_Module_Regexp (string name, string source, string pattern);
Pandora_Module_Regexp (string name, string source, string pattern, unsigned char no_seek_eof);
virtual ~Pandora_Module_Regexp ();
void run ();
};