From a3f1d2b8b19f8a5257ddb825aea3f58e01751689 Mon Sep 17 00:00:00 2001 From: pabloconcepcion Date: Thu, 21 Jan 2010 14:58:38 +0000 Subject: [PATCH] =?UTF-8?q?2010-01-21=20=20Pablo=20de=20la=20Concepci?= =?UTF-8?q?=C3=B3n=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lib/PandoraFMS/Core.pm: Modified to unify create and update functions using optinal parameters as Ramon sugested. * lib/PandoraFMS/DataServer.pm: Modified to ignore invalid positional data, use the unified functions from Core.pm, and store the agent_timestamp modified by the agent_timezone. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2292 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 9 ++ pandora_server/lib/PandoraFMS/Core.pm | 95 ++++++++++----------- pandora_server/lib/PandoraFMS/DataServer.pm | 54 ++++++++---- 3 files changed, 89 insertions(+), 69 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index f5f51cfa98..bddcee6c1e 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,12 @@ +2010-01-21 Pablo de la ConcepciĆ³n + + * lib/PandoraFMS/Core.pm: Modified to unify create and update functions + using optinal parameters as Ramon sugested. + + * lib/PandoraFMS/DataServer.pm: Modified to ignore invalid positional data, + use the unified functions from Core.pm, and store the agent_timestamp + modified by the agent_timezone. + 2010-01-20 Ramon Novoa * util/pandora_xml_stress.pl: Fixed the script. Was not working. diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 07dff944b9..d645ae91a2 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -38,7 +38,6 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( pandora_audit pandora_create_agent - pandora_create_agent_gis pandora_create_incident pandora_create_module pandora_evaluate_alert @@ -60,7 +59,6 @@ our @EXPORT = qw( pandora_reset_server pandora_server_keep_alive pandora_update_agent - pandora_update_agent_gis pandora_update_module_on_error pandora_update_server @@ -709,35 +707,13 @@ sub pandora_update_server ($$$$$;$$) { WHERE id_server = ?', $status, $timestamp, $pa_config->{'pandora_master'}, $num_threads, $queue_size, $server->{'id_server'}); } -########################################################################## -# Update last contact field in agent table -########################################################################## -sub pandora_update_agent ($$$$$$$) { - my ($pa_config, $agent_timestamp, $agent_id, $os_version, - $agent_version, $agent_interval, $dbh) = @_; - - my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime()); - - pandora_access_update ($pa_config, $agent_id, $dbh); - - # No update for interval field (some old agents don't support it) - if ($agent_interval == -1){ - db_do($dbh, 'UPDATE tagente SET agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ?, WHERE id_agente = ?', - $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); - return; - } - - db_do ($dbh, 'UPDATE tagente SET intervalo = ?, agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ?, WHERE id_agente = ?', - $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); -} - ########################################################################## # Update last contact, timezone and position fields in agent table ########################################################################## -sub pandora_update_agent_gis ($$$$$$$$$$$) { +sub pandora_update_agent ($$$$$$$;$$$$) { my ($pa_config, $agent_timestamp, $agent_id, $os_version, - $agent_version, $agent_interval, $timezone_offset, - $longitude, $latitude, $altitude, $dbh) = @_; + $agent_version, $agent_interval, $dbh, $timezone_offset, + $longitude, $latitude, $altitude) = @_; my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime()); @@ -749,11 +725,35 @@ sub pandora_update_agent_gis ($$$$$$$$$$$) { $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); return; } - + + #Test if we have received the optional position parameters + if (!defined ($longitude) || !defined ($latitude ) || !defined ($altitude)){ + if ( defined ($timezone_offset)) { + # Update the table tagente with all the new data + db_do ($dbh, 'UPDATE tagente SET intervalo = ?, agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ?, + timezone_offset = ? WHERE id_agente = ?', $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $timezone_offset, $agent_id); + } + else { + # Update the table tagente with all the new data + db_do ($dbh, 'UPDATE tagente SET intervalo = ?, agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ? WHERE id_agente = ?', + $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); + } + return; + } + + # If there is positional data logger($pa_config, "Agent id $agent_id",10); # Get the last position to see if it has moved. my $last_agent_info= get_db_single_row ($dbh, 'SELECT * FROM tagente WHERE id_agente = ?', $agent_id); + # if the the flag update_gis_data is 0 the positional data is ignored. + if ($last_agent_info->{'update_gis_data'} == 0){ + logger($pa_config, "Agent id $agent_id positional data ignored",10); + db_do($dbh, 'UPDATE tagente SET intervalo = ?, agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ?, WHERE id_agente = ?', + $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); + return; + } + logger($pa_config, "Old Agent data: last-longitude = ". $last_agent_info->{'last_longitude'}. " ID: $agent_id ", 10); # If the agent has moved outside the range stablised as @@ -879,40 +879,33 @@ sub pandora_create_module ($$$$$$$$$$) { return $module_id; } -########################################################################## -# Create a new entry in tagente. -########################################################################## -sub pandora_create_agent ($$$$$$$$$$$) { - my ($pa_config, $server_name, $agent_name, $address, - $address_id, $group_id, $parent_id, $os_id, - $description, $interval, $dbh) = @_; - - logger ($pa_config, "Server '$server_name' creating agent '$agent_name' address '$address'.", 10); - - $description = "Created by $server_name" unless ($description ne ''); - - my $agent_id = db_insert ($dbh, 'INSERT INTO tagente (`nombre`, `direccion`, `comentarios`, `id_grupo`, `id_os`, `server_name`, `intervalo`, `id_parent`, `modo`) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_name, $address, $description, $group_id, $os_id, $server_name, $interval, $parent_id); - - pandora_event ($pa_config, "Agent [$agent_name] created by $server_name", $pa_config->{'autocreate_group'}, $agent_id, 2, 0, 0, 'new_agent', $dbh); - return $agent_id; -} ########################################################################## # Create a new entry in tagente with position information ########################################################################## -sub pandora_create_agent_gis ($$$$$$$$$$$$$$$) { +sub pandora_create_agent ($$$$$$$$$$$;$$$$) { my ($pa_config, $server_name, $agent_name, $address, $address_id, $group_id, $parent_id, $os_id, - $description, $interval, $timezone_offset, - $longitude, $latitude, $altitude, $dbh) = @_; + $description, $interval, $dbh, $timezone_offset, + $longitude, $latitude, $altitude) = @_; logger ($pa_config, "Server '$server_name' creating agent '$agent_name' address '$address'.", 10); $description = "Created by $server_name" unless ($description ne ''); - - my $agent_id = db_insert ($dbh, 'INSERT INTO tagente (`nombre`, `direccion`, `comentarios`, `id_grupo`, `id_os`, `server_name`, `intervalo`, `id_parent`, + my $agent_id; + # Test if the optional positional parameters are defined + if (!defined ($timezone_offset) || !defined ($longitude) || !defined ($latitude ) || !defined ($altitude)){ + $agent_id = db_insert ($dbh, 'INSERT INTO tagente (`nombre`, `direccion`, `comentarios`, `id_grupo`, `id_os`, `server_name`, `intervalo`, `id_parent`, `modo`) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_name, $address, $description, $group_id, $os_id, $server_name, $interval, $parent_id); + } + else { + $agent_id = db_insert ($dbh, 'INSERT INTO tagente (`nombre`, `direccion`, `comentarios`, `id_grupo`, `id_os`, `server_name`, `intervalo`, `id_parent`, `timezone_offset`, `last_longitude`, `last_latitude`, `last_altitude`,`modo` ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_name, $address, $description, $group_id, $os_id, $server_name, $interval, $parent_id, $timezone_offset, $longitude, $latitude, $altitude); + # Save the first position + my $utimestamp = time (); + my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime ($utimestamp)); + save_agent_position($pa_config, $timestamp, $longitude, $latitude, $altitude, $agent_id, $dbh); + } logger ($pa_config, "Server '$server_name' CREATED agent '$agent_name' address '$address'.", 10); pandora_event ($pa_config, "Agent [$agent_name] created by $server_name", $pa_config->{'autocreate_group'}, $agent_id, 2, 0, 0, 'new_agent', $dbh); diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index 53f13e8ea3..5f3060a2a0 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -166,17 +166,11 @@ sub process_xml_data ($$$$$) { if ($timezone_offset !~ /[-+]?[0-9,11,12]/) { $timezone_offset = 0; # Default value } - # Longitude must be a real number - if ($longitude !~ /[-+]?[0-9]*\.?[0-9]+/) { - $longitude = 0.0; # Default value - } - # Latitude must be a real number - if ($latitude !~ /[-+]?[0-9]*\.?[0-9]+/) { - $latitude = 0.0; # Default value - } - # Altitude must be a real number - if ($altitude !~ /[-+]?[0-9]*\.?[0-9]+/) { - $altitude = 0.0; # Default value + + my $valid_position_data = 1; + # If position data are not valid should be ignored + if ($longitude !~ /[-+]?[0-9]*\.?[0-9]+/ || $latitude !~ /[-+]?[0-9]*\.?[0-9]+/ || $altitude !~ /[-+]?[0-9]*\.?[0-9]+/) { + $valid_position_data = 0; } logger($pa_config, "Getting GIS Data=timezone_offset=$timezone_offset longitude=$longitude latitude=$latitude altitude=$altitude", 10); @@ -191,7 +185,18 @@ sub process_xml_data ($$$$$) { if ( $data->{'timestamp'} =~ /AUTO/ ){ $timestamp = strftime ("%Y/%m/%d %H:%M:%S", localtime()); } + else { + # Modify the timestamp with the timezone_offset + logger($pa_config, "Unmodified timestamp = $timestamp", 5); + $timestamp =~ /(\d+)\/(\d+)\/(\d+) +(\d+):(\d+):(\d+)/; + #my $last_fired = ($1 > 0) ? + my $utimestamp = timelocal($6, $5, $4, $3, $2 - 1, $1 - 1900) + ($timezone_offset * 3600); + logger($pa_config, "Seconds timestamp = $timestamp modified timestamp in seconds $utimestamp with timezone_offset = $timezone_offset", 5); + $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp)); + logger($pa_config, "Modified timestamp = $timestamp with timezone_offset = $timezone_offset", 5); + } + # Check some variables $interval = 300 if (! defined ($interval) || $interval eq ''); $os_version = 'N/A' if (! defined ($os_version) || $os_version eq ''); @@ -215,9 +220,16 @@ sub process_xml_data ($$$$$) { $description = $data->{'description'} if (defined ($data->{'description'})); # Create the agent - logger($pa_config, "Creating agent $agent_name at long: $longitude lat: $latitude alt: $altitude", 5); - $agent_id = pandora_create_agent_gis ($pa_config, $pa_config->{'servername'}, $agent_name, '', 0, $group_id, 0, $os, $description, $interval, $timezone_offset, - $longitude, $latitude, $altitude, $dbh); + if ($valid_position_data == 1) { + logger($pa_config, "Creating agent $agent_name at long: $longitude lat: $latitude alt: $altitude", 5); + $agent_id = pandora_create_agent($pa_config, $pa_config->{'servername'}, $agent_name, '', 0, $group_id, 0, $os, + $description, $interval, $dbh, $timezone_offset, $longitude, $latitude, $altitude); + } + else { # Ignore agent positional data + logger($pa_config, "Creating agent $agent_name", 5); + $agent_id = pandora_create_agent($pa_config, $pa_config->{'servername'}, $agent_name, '', 0, $group_id, 0, $os, + $description, $interval, $dbh, $timezone_offset); + } if (! defined ($agent_id)) { $AgentSem->up (); return; @@ -225,10 +237,16 @@ sub process_xml_data ($$$$$) { } $AgentSem->up (); - logger($pa_config, "Updating agent $agent_name at long: $longitude lat: $latitude alt: $altitude", 5); - # Update agent information including position information - pandora_update_agent_gis ($pa_config, $timestamp, $agent_id, $os_version, $agent_version, $interval, $timezone_offset, $longitude, $latitude, $altitude, $dbh); - + if ($valid_position_data == 1) { + logger($pa_config, "Updating agent $agent_name at long: $longitude lat: $latitude alt: $altitude", 5); + # Update agent information including position information + pandora_update_agent($pa_config, $timestamp, $agent_id, $os_version, $agent_version, $interval, $dbh, $timezone_offset, $longitude, $latitude, $altitude); + } + else { + logger($pa_config, "Updating agent $agent_name", 5); + # Update agent information without position information + pandora_update_agent($pa_config, $timestamp, $agent_id, $os_version, $agent_version, $interval, $dbh, $timezone_offset); + } pandora_module_keep_alive ($pa_config, $agent_id, $agent_name, $server_id, $dbh); # Process modules