2010-09-20 Ramon Novoa <rnovoa@artica.es>

* lib/PandoraFMS/Core.pm, lib/PandoraFMS/DataServer.pm: Update module
	  configuration from XML data files.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3262 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2010-09-20 17:01:27 +00:00
parent dfac299c4a
commit 72d209dee9
3 changed files with 87 additions and 48 deletions

View File

@ -1,3 +1,8 @@
2010-09-20 Ramon Novoa <rnovoa@artica.es>
* lib/PandoraFMS/Core.pm, lib/PandoraFMS/DataServer.pm: Update module
configuration from XML data files.
2010-09-20 Sergio Martin <sergio.martin@artica.es> 2010-09-20 Sergio Martin <sergio.martin@artica.es>
* util/pandora_manage.pl: Improved the last feature * util/pandora_manage.pl: Improved the last feature

View File

@ -686,7 +686,7 @@ Process Pandora module.
=cut =cut
########################################################################## ##########################################################################
sub pandora_process_module ($$$$$$$$$;$) { sub pandora_process_module ($$$$$$$$$;$) {
my ($pa_config, $dataObject, $agent, $module, $module_type, my ($pa_config, $data_object, $agent, $module, $module_type,
$timestamp, $utimestamp, $server_id, $dbh, $extra_macros) = @_; $timestamp, $utimestamp, $server_id, $dbh, $extra_macros) = @_;
logger($pa_config, "Processing module '" . $module->{'nombre'} . "' for agent " . (defined ($agent) && $agent ne '' ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 10); logger($pa_config, "Processing module '" . $module->{'nombre'} . "' for agent " . (defined ($agent) && $agent ne '' ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 10);
@ -712,9 +712,9 @@ sub pandora_process_module ($$$$$$$$$;$) {
} }
# Process data # Process data
my $processed_data = process_data ($dataObject, $module, $module_type, $utimestamp, $dbh); my $processed_data = process_data ($data_object, $module, $module_type, $utimestamp, $dbh);
if (! defined ($processed_data)) { if (! defined ($processed_data)) {
logger($pa_config, "Received invalid data '" . $dataObject->{'data'} . "' from agent '" . $agent->{'nombre'} . "' module '" . $module->{'nombre'} . "' agent " . (defined ($agent) ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 3); logger($pa_config, "Received invalid data '" . $data_object->{'data'} . "' from agent '" . $agent->{'nombre'} . "' module '" . $module->{'nombre'} . "' agent " . (defined ($agent) ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 3);
pandora_update_module_on_error ($pa_config, $module, $dbh); pandora_update_module_on_error ($pa_config, $module, $dbh);
return; return;
} }
@ -783,7 +783,7 @@ sub pandora_process_module ($$$$$$$$$;$) {
# Save module data. Async and log4x modules are not compressed. # Save module data. Async and log4x modules are not compressed.
if ($module_type =~ m/(async)|(log4x)/ || $save == 1) { if ($module_type =~ m/(async)|(log4x)/ || $save == 1) {
save_module_data ($dataObject, $module, $module_type, $utimestamp, $dbh); save_module_data ($data_object, $module, $module_type, $utimestamp, $dbh);
} }
} }
@ -1081,7 +1081,6 @@ sub pandora_create_module ($$$$$$$$$$) {
$max = 0 if ($max eq ''); $max = 0 if ($max eq '');
$min = 0 if ($min eq ''); $min = 0 if ($min eq '');
$post_process = 0 if ($post_process eq ''); $post_process = 0 if ($post_process eq '');
$description = 'N/A' if ($description eq '');
my $module_id = db_insert($dbh, 'INSERT INTO tagente_modulo (`id_agente`, `id_tipo_modulo`, `nombre`, `max`, `min`, `post_process`, `descripcion`, `module_interval`, `id_modulo`) my $module_id = db_insert($dbh, 'INSERT INTO tagente_modulo (`id_agente`, `id_tipo_modulo`, `nombre`, `max`, `min`, `post_process`, `descripcion`, `module_interval`, `id_modulo`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_id, $module_type_id, $module_name, $max, $min, $post_process, $description, $interval); VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_id, $module_type_id, $module_name, $max, $min, $post_process, $description, $interval);
@ -1417,13 +1416,13 @@ sub subst_alert_macros ($$) {
# Process module data. # Process module data.
########################################################################## ##########################################################################
sub process_data ($$$$$) { sub process_data ($$$$$) {
my ($dataObject, $module, $module_type, $utimestamp, $dbh) = @_; my ($data_object, $module, $module_type, $utimestamp, $dbh) = @_;
if ($module_type eq "log4x") { if ($module_type eq "log4x") {
return log4x_get_severity_num($dataObject); return log4x_get_severity_num($data_object);
} }
my $data = $dataObject->{'data'}; my $data = $data_object->{'data'};
# String data # String data
if ($module_type =~ m/_string$/) { if ($module_type =~ m/_string$/) {
@ -1452,7 +1451,7 @@ sub process_data ($$$$$) {
# Not an error, no previous data # Not an error, no previous data
if (!defined($data)){ if (!defined($data)){
$dataObject->{'data'} = 0; $data_object->{'data'} = 0;
return 0; return 0;
} }
#return 0 unless defined ($data); #return 0 unless defined ($data);
@ -1468,7 +1467,7 @@ sub process_data ($$$$$) {
# Format data # Format data
$data = sprintf("%.2f", $data); $data = sprintf("%.2f", $data);
$dataObject->{'data'} = $data; $data_object->{'data'} = $data;
return $data; return $data;
} }
@ -1507,8 +1506,8 @@ sub process_inc_data ($$$$) {
} }
sub log4x_get_severity_num($) { sub log4x_get_severity_num($) {
my ($dataObject) = @_; my ($data_object) = @_;
my $data = $dataObject->{'severity'}; my $data = $data_object->{'severity'};
return undef unless defined ($data); return undef unless defined ($data);
# The severity is a word, so we need to translate to numbers # The severity is a word, so we need to translate to numbers
@ -1647,7 +1646,7 @@ sub generate_status_event ($$$$$$$) {
# Saves module data to the DB. # Saves module data to the DB.
########################################################################## ##########################################################################
sub save_module_data ($$$$$) { sub save_module_data ($$$$$) {
my ($dataObject, $module, $module_type, $utimestamp, $dbh) = @_; my ($data_object, $module, $module_type, $utimestamp, $dbh) = @_;
if ($module_type eq "log4x") { if ($module_type eq "log4x") {
#<module> #<module>
@ -1664,12 +1663,12 @@ sub save_module_data ($$$$$) {
db_do($dbh, $sql, db_do($dbh, $sql,
$module->{'id_agente_modulo'}, $utimestamp, $module->{'id_agente_modulo'}, $utimestamp,
$dataObject->{'severity'}, $data_object->{'severity'},
$dataObject->{'message'}, $data_object->{'message'},
$dataObject->{'stacktrace'} $data_object->{'stacktrace'}
); );
} else { } else {
my $data = $dataObject->{'data'}; my $data = $data_object->{'data'};
my $table = ($module_type =~ m/_string/) ? 'tagente_datos_string' : 'tagente_datos'; my $table = ($module_type =~ m/_string/) ? 'tagente_datos_string' : 'tagente_datos';
db_do($dbh, 'INSERT INTO ' . $table . ' (id_agente_modulo, datos, utimestamp) db_do($dbh, 'INSERT INTO ' . $table . ' (id_agente_modulo, datos, utimestamp)

View File

@ -136,6 +136,7 @@ sub data_consumer ($$) {
my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ()); my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ());
my $file_name = $pa_config->{'incomingdir'}; my $file_name = $pa_config->{'incomingdir'};
my $xml_err;
# Fix path # Fix path
$file_name .= "/" unless (substr ($file_name, -1, 1) eq '/'); $file_name .= "/" unless (substr ($file_name, -1, 1) eq '/');
@ -155,7 +156,8 @@ sub data_consumer ($$) {
# Invalid XML # Invalid XML
if ($@) { if ($@) {
sleep (10); $xml_err = $@;
sleep (1);
next; next;
} }
@ -168,7 +170,7 @@ sub data_consumer ($$) {
} }
rename($file_name, $file_name . '_BADXML'); rename($file_name, $file_name . '_BADXML');
pandora_event ($pa_config, "Unable to process XML data file ($file_name)", 0, 0, 0, 0, 0, 'error', 0, $dbh); pandora_event ($pa_config, "Unable to process XML data file '$file_name': $xml_err", 0, 0, 0, 0, 0, 'error', 0, $dbh);
} }
############################################################################### ###############################################################################
@ -395,6 +397,20 @@ sub process_module_data ($$$$$$$$$) {
return; return;
} }
# Get module parameters, matching column names in tagente_modulo
my $module_conf;
$module_conf->{'max'} = get_tag_value ($data, 'max', 0);
$module_conf->{'min'} = get_tag_value ($data, 'min', 0);
$module_conf->{'descripcion'} = get_tag_value ($data, 'description', '');
$module_conf->{'post_process'} = get_tag_value ($data, 'post_process', 0);
$module_conf->{'module_interval'} = get_tag_value ($data, 'module_interval', 1);
# Calculate the module interval in seconds
$module_conf->{'module_interval'} *= $interval;
# Allow , as a decimal separator
$module_conf->{'post_process'} =~ s/,/./;
# Get module data or create it if it does not exist # Get module data or create it if it does not exist
$ModuleSem->down (); $ModuleSem->down ();
my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND nombre = ?', $agent->{'id_agente'}, $module_name); my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND nombre = ?', $agent->{'id_agente'}, $module_name);
@ -421,25 +437,23 @@ sub process_module_data ($$$$$$$$$) {
return; return;
} }
# Get min/max/description/post process
my $max = get_tag_value ($data, 'max', 0);
my $min = get_tag_value ($data, 'min', 0);
my $description = get_tag_value ($data, 'description', '');
my $post_process = get_tag_value ($data, 'post_process', 0);
# Allow , as a decimal separator
$post_process =~ s/,/./;
# Create the module # Create the module
pandora_create_module ($pa_config, $agent->{'id_agente'}, $module_id, $module_name, pandora_create_module ($pa_config, $agent->{'id_agente'}, $module_id, $module_name,
$max, $min, $post_process, $description, $interval, $dbh); $module_conf->{'max'}, $module_conf->{'min'}, $module_conf->{'post_process'},
$module_conf->{'descripcion'}, $module_conf->{'module_interval'}, $dbh);
$module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND nombre = ?', $agent->{'id_agente'}, $module_name); $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND nombre = ?', $agent->{'id_agente'}, $module_name);
if (! defined ($module)) { if (! defined ($module)) {
logger($pa_config, "Could not create module '$module_name' for agent '$agent_name'.", 3); logger($pa_config, "Could not create module '$module_name' for agent '$agent_name'.", 3);
$ModuleSem->up (); $ModuleSem->up ();
return; return;
} }
} else {
# Update module configuration if in learning mode
if ($agent->{'modo'} eq '1') {
update_module_configuration ($pa_config, $dbh, $module, $module_conf);
} }
}
$ModuleSem->up (); $ModuleSem->up ();
# Module disabled! # Module disabled!
@ -463,45 +477,66 @@ sub process_module_data ($$$$$$$$$) {
return; return;
} }
#my $value = get_tag_value ($data, 'data', ''); #my $value = get_tag_value ($data, 'data', '');
my $data_object = get_module_data($data, $agent_name, $module_name, $module_type); my $data_object = get_module_data($data, $module_type);
my $extra_macros = get_macros_for_data($data, $agent_name, $module_name, $module_type); my $extra_macros = get_macros_for_data($data, $module_type);
pandora_process_module ($pa_config, $data_object, $agent, $module, $module_type, $timestamp, $utimestamp, $server_id, $dbh, $extra_macros); pandora_process_module ($pa_config, $data_object, $agent, $module, $module_type, $timestamp, $utimestamp, $server_id, $dbh, $extra_macros);
} }
##########################################################################
# Retrieve module data from the XML tree.
##########################################################################
sub get_module_data($$){ sub get_module_data($$){
my ($data, $agent_name, $module_name, $module_type) = @_; my ($data, $module_type) = @_;
my %data_object; my %data_object;
if ($module_type eq "log4x") { # Log4x modules hava extended information
if ($module_type eq 'log4x') {
foreach my $attr ('severity','message', 'stacktrace'){ foreach my $attr ('severity','message', 'stacktrace'){
$data_object{$attr} = get_tag_value ($data, $attr, ''); $data_object{$attr} = get_tag_value ($data, $attr, '');
} }
} else { } else {
# Default case $data_object{'data'} = get_tag_value ($data, 'data', '');
my $value = get_tag_value ($data, 'data', '');
$data_object{'data'} = $value;
} }
#return $value;
return \%data_object; return \%data_object;
} }
##########################################################################
# Retrieve module data from the XML tree.
##########################################################################
sub get_macros_for_data($$){ sub get_macros_for_data($$){
my ($data, $agent_name, $module_name, $module_type) = @_; my ($data, $module_type) = @_;
my %macros; my %macros;
if ($module_type eq "log4x") { if ($module_type eq 'log4x') {
foreach my $attr ('severity','message', 'stacktrace'){ foreach my $attr ('severity','message', 'stacktrace') {
my $macro = "_" . $attr . "_"; $macros{'_' . $attr . '_'} = get_tag_value ($data, $attr, '');
$macros{$macro} = get_tag_value ($data, $attr, '');
} }
} else {
} }
return \%macros; return \%macros;
} }
##########################################################################
# Update module configuration in tagente_modulo if necessary.
##########################################################################
sub update_module_configuration ($$$$) {
my ($pa_config, $dbh, $module, $module_conf) = @_;
# Update if at least one of the configuration tokens has changed
if ($module->{'min'} != $module_conf->{'min'} || $module->{'max'} != $module_conf->{'max'} ||
$module->{'descripcion'} ne $module_conf->{'descripcion'} || $module->{'post_process'} != $module_conf->{'post_process'} ||
$module->{'module_interval'} != $module_conf->{'module_interval'}) {
logger($pa_config, "Updating configuration for module '" . $module->{'nombre'} . "'.", 10);
db_do ($dbh, 'UPDATE tagente_modulo SET min = ?, max = ?, descripcion = ?, post_process = ?, module_interval = ?
WHERE id_agente_modulo = ?', $module_conf->{'min'}, $module_conf->{'max'}, $module_conf->{'descripcion'},
$module_conf->{'post_process'}, $module_conf->{'module_interval'}, $module->{'id_agente_modulo'});
return;
}
}
1; 1;
__END__ __END__