From b2889542509d54a0fe68a2eb98499c8d5e351c12 Mon Sep 17 00:00:00 2001 From: pabloconcepcion Date: Thu, 18 Feb 2010 17:49:48 +0000 Subject: [PATCH] =?UTF-8?q?2010-02-18=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 use the new tables (tgis_data_history and tgis_data_status). * man/man3/PandoraFMS::Core.pm.3: Updated manpage with the new documentation git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2379 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 8 + pandora_server/lib/PandoraFMS/Core.pm | 185 +++++++++++------- pandora_server/man/man3/PandoraFMS::Core.pm.3 | 34 ++-- 3 files changed, 132 insertions(+), 95 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 85ccc06af0..9c54222822 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,11 @@ +2010-02-18 Pablo de la ConcepciĆ³n + + * lib/PandoraFMS/Core.pm: Modified to use the new tables + (tgis_data_history and tgis_data_status). + + * man/man3/PandoraFMS::Core.pm.3: Updated manpage with the new + documentation + 2010-02-18 Sancho Lerena * conf/pandora_server.conf: New option, self_monitoring. diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 1c6f5afc3d..a1820d4cbf 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -878,60 +878,52 @@ sub pandora_update_agent ($$$$$$$;$$$$$) { return; } - #Test if we have received the optional position parameters - if (!defined ($longitude) || !defined ($latitude ) || !defined ($altitude) || $pa_config->{'activate_gis'} == 0 ){ - if ( defined ($timezone_offset)) { + 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 { + 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); - } - # Without valid positional paremeters or desactivated gis system there is nothing more to do here - return; + $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); } - - # 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; - } + my $update_gis_data= get_db_value ($dbh, 'SELECT update_gis_data FROM tagente WHERE id_agente = ?', $agent_id); + #Test if we have received the optional position parameters + if (defined ($longitude) && defined ($latitude) && $pa_config->{'activate_gis'} == 1 && $update_gis_data == 1 ){ + # Get the last position to see if it has moved. + my $last_agent_position= get_db_single_row ($dbh, 'SELECT * FROM tgis_data_status WHERE tagente_id_agente = ?', $agent_id); + if(defined($last_agent_position)) { - logger($pa_config, "Old Agent data: last-longitude = ". $last_agent_info->{'last_longitude'}. " ID: $agent_id ", 10); + logger($pa_config, "Old Agent data: current_longitude=". $last_agent_position->{'current_longitude'}. " current_latitude=" + .$last_agent_position->{'current_latitude'}. " current_altitude=". $last_agent_position->{'current_altitude'}. " ID: $agent_id ", 10); - # If the agent has moved outside the range stablised as location error - if (distance_moved($pa_config, $last_agent_info->{'last_longitude'}, $last_agent_info->{'last_latitude'}, $last_agent_info->{'last_altitude'}, $longitude, $latitude, $altitude) > $pa_config->{'location_error'}){ - if (defined($position_description)) { - # Save the agent data in the agent table - save_agent_position($pa_config, $timestamp, $last_agent_info->{'last_longitude'},$last_agent_info->{'last_latitude'}, $last_agent_info->{'last_altitude'}, $position_description, $agent_id, $dbh); + # If the agent has moved outside the range stablised as location error + if (distance_moved($pa_config, $last_agent_position->{'current_longitude'}, $last_agent_position->{'current_latitude'}, + $last_agent_position->{'current_altitude'}, $longitude, $latitude, $altitude) > $pa_config->{'location_error'}) { + #Archive the old position and save new one as status + archive_agent_position($pa_config, $last_agent_position->{'start_timestamp'},$timestamp,$last_agent_position->{'stored_longitude'}, $last_agent_position->{'stored_latitude'}, + $last_agent_position->{'stored_altitude'},$last_agent_position->{'description'}, $last_agent_position->{'number_of_packages'},$agent_id, $dbh); + if(!defined($altitude) ) { + $altitude = 0; + } + # Save the agent position in the tgis_data_status table + update_agent_position($pa_config, $longitude, $latitude, $altitude, $agent_id, $dbh, $longitude, $latitude, $altitude, $timestamp, $position_description); + } + else { #the agent has not moved enougth so just update the status table + update_agent_position ($pa_config, $longitude, $latitude, $altitude, $agent_id, $dbh); + } } else { - # Save the agent data in the agent table - save_agent_position($pa_config, $timestamp, $last_agent_info->{'last_longitude'},$last_agent_info->{'last_latitude'}, $last_agent_info->{'last_altitude'},'' , $agent_id, $dbh); + logger($pa_config, "There was not previous positional data, storing first positioal status",8); + save_agent_position($pa_config, $longitude, $latitude, $altitude, $agent_id, $dbh, $timestamp, $position_description); } - # 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 = ?, - last_longitude = ?, last_latitude =?, last_altitude = ? WHERE id_agente = ?', - $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $timezone_offset, $longitude, $latitude, $altitude, $agent_id); - } - else { - # Update the end timestamp for the agent - update_agent_position($pa_config, $timestamp, $agent_id, $dbh); - # Update the table tagente with all the new data but the location - 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 { + logger($pa_config, "Agent id $agent_id positional data ignored (update_gis_data = $update_gis_data)",10); + } + } ########################################################################## @@ -1042,23 +1034,24 @@ sub pandora_create_agent ($$$$$$$$$$$;$$$$$) { $description = "Created by $server_name" unless ($description ne ''); my $agent_id; # Test if the optional positional parameters are defined or GIS is disabled - if (!defined ($timezone_offset) || !defined ($longitude) || !defined ($latitude ) || !defined ($altitude) || $pa_config->{'activate_gis'} == 0){ + if (!defined ($timezone_offset) ) { $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); + `timezone_offset`, `modo` ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_name, $address, + $description, $group_id, $os_id, $server_name, $interval, $parent_id, $timezone_offset); + } + if (defined ($longitude) && defined ($latitude ) && $pa_config->{'activate_gis'} == 1 ) { + if (!defined($altitude)) { + $altitude = 0; + } # Save the first position my $utimestamp = time (); my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime ($utimestamp)); - if (defined($position_description)) { - save_agent_position($pa_config, $timestamp, $longitude, $latitude, $altitude, $position_description, $agent_id, $dbh); - } - else { - save_agent_position($pa_config, $timestamp, $longitude, $latitude, $altitude, '', $agent_id, $dbh); - } + + save_agent_position($pa_config, $longitude, $latitude, $altitude, $agent_id, $dbh, $timestamp,$position_description) ; } logger ($pa_config, "Server '$server_name' CREATED agent '$agent_name' address '$address'.", 10); @@ -1647,42 +1640,86 @@ sub pandora_inhibit_alerts ($$$) { return 0; } - ########################################################################## -=head2 C<< update_agent_position (I<$pa_config>, I<$timestamp>, I<$agent_id>, I<$dbh>) >> +=head2 C<< save_agent_position (I<$pa_config>, I<$current_longitude>, I<$current_latitude>, + I<$current_altitude>, I<$agent_id>, I<$dbh>, [I<$start_timestamp>], [I<$description>]) >> -# Updates agent's I and I in B table +Saves a new agent GIS information record in B table. =cut ########################################################################## -sub update_agent_position($$$$) { - my ($pa_config, $timestamp, $agent_id, $dbh) = @_; - - logger($pa_config, "Updating agent end_timestamp: end_timestamp=$timestamp agent_id=$agent_id", 10); - - # Find the last data from the received agent - my $agent_position = get_db_single_row ($dbh, 'SELECT * FROM tgis_data WHERE tagente_id_agente = ? ORDER BY start_timestamp DESC LIMIT 1', $agent_id ); - return unless (defined ($agent_position)); - - # Upadate the timestap of the received agent - db_do ($dbh, 'UPDATE tgis_data SET end_timestamp = ?, number_of_packages = number_of_packages +1 WHERE id_tgis_data = ?', $timestamp, $agent_position->{'id_tgis_data'}); - +sub save_agent_position($$$$$$;$$) { + my ($pa_config, $current_longitude, $current_latitude, $current_altitude, $agent_id, $dbh, $start_timestamp, $description) = @_; + + if (!defined($description)) { + $description = ''; + } + logger($pa_config, "Updating agent position: longitude=$current_longitude, latitude=$current_latitude, altitude=$current_altitude, start_timestamp=$start_timestamp agent_id=$agent_id", 10); + + if (defined($start_timestamp)) { + # Upadate the timestap of the received agentk + db_insert ($dbh, 'INSERT INTO tgis_data_status (tagente_id_agente, current_longitude , current_latitude, current_altitude, + stored_longitude , stored_latitude, stored_altitude, start_timestamp, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', + $agent_id, $current_longitude, $current_latitude, $current_altitude, $current_longitude, + $current_latitude, $current_altitude, $start_timestamp, $description); + } + else { + # Upadate the data of the received agent using the default timestamp + db_insert ($dbh, 'INSERT INTO tgis_data_status (tagente_id_agente, current_longitude , current_latitude, current_altitude, + stored_longitude , stored_latitude, stored_altitude, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ', + $agent_id, $current_longitude, $current_latitude, $current_altitude, $current_longitude, + $current_latitude, $current_altitude, , $description); + } } ########################################################################## -=head2 C<< save_agent_position (I<$pa_config>, I<$timestamp>, I<$longitude>, I<$latitude>, I<$altitude>, I<$description>, I<$agent_id>, I<$dbh>) >> - -Saves the last position of an agent in the B table +=head2 C<< update_agent_position (I<$pa_config>, I<$current_longitude>, I<$current_latitude>, I<$current_altitude>, + I<$agent_id>, I<$dbh>, [I<$stored_longitude>], [I<$stored_latitude>], [I<$stored_altitude>], [I<$start_timestamp>], [I<$description>]) >> + +Updates agent GIS information in B table. =cut ########################################################################## -sub save_agent_position($$$$$$$$) { - my ($pa_config, $timestamp, $longitude, $latitude, $altitude, $description, $agent_id, $dbh) = @_; +sub update_agent_position($$$$$$;$$$$$) { + my ($pa_config, $current_longitude, $current_latitude, $current_altitude, + $agent_id, $dbh, $stored_longitude, $stored_latitude, $stored_altitude, $start_timestamp, $description) = @_; + if (defined($stored_longitude) && defined($stored_latitude) && defined($start_timestamp) ) { + # Upadate all the position data of the agent + logger($pa_config, "Updating agent position: current_longitude=$current_longitude, current_latitude=$current_latitude, + current_altitude=$current_altitude, stored_longitude=$stored_longitude, stored_latitude=$stored_latitude, + stored_altitude=$stored_altitude, start_timestamp=$start_timestamp, agent_id=$agent_id", 10); + db_do ($dbh, 'UPDATE tgis_data_status SET current_longitude = ?, current_latitude = ?, current_altitude = ?, + stored_longitude = ?,stored_latitude = ?,stored_altitude = ?, start_timestamp = ?, description = ?, + number_of_packages = 1 WHERE tagente_id_agente = ?', + $current_longitude, $current_latitude, $current_altitude, $stored_longitude, $stored_latitude, + $stored_altitude, $start_timestamp, $description, $agent_id); + } + else { + logger($pa_config, "Updating agent position: longitude=$current_longitude, latitude=$current_latitude, altitude=$current_altitude, agent_id=$agent_id", 10); + # Upadate the timestap of the received agent + db_do ($dbh, 'UPDATE tgis_data_status SET current_longitude = ?, current_latitude = ?, current_altitude = ?, + number_of_packages = number_of_packages + 1 WHERE tagente_id_agente = ?', + $current_longitude, $current_latitude, $current_altitude, $agent_id); + } +} - logger($pa_config, "Saving new agent position: timestamp=$timestamp longitude=$longitude latitude=$latitude altitude=$altitude", 10); +########################################################################## +=head2 C<< archive_agent_position (I<$pa_config>, I<$start_timestamp>, I<$end_timestamp>, I<$longitude>, I<$latitude>, I<$altitude>, I<$description>, +I<$number_packages>, I<$agent_id>, I<$dbh>) >> + +Archives the last position of an agent in the B table - db_insert($dbh, 'INSERT INTO tgis_data (`longitude`, `latitude`, `altitude`, `tagente_id_agente`, `start_timestamp`, `end_timestamp`, `description`) VALUES (?, ?, ?, ?, ?, ?, ?)', - $longitude, $latitude, $altitude, $agent_id, $timestamp, $timestamp, $description); +=cut +########################################################################## +sub archive_agent_position($$$$$$$$$$) { + my ($pa_config, $start_timestamp, $end_timestamp, $longitude, $latitude, + $altitude, $description, $number_packages, $agent_id, $dbh) = @_; + + logger($pa_config, "Saving new agent position: start_timestamp=$start_timestamp longitude=$longitude latitude=$latitude altitude=$altitude", 10); + + db_insert($dbh, 'INSERT INTO tgis_data_history (`longitude`, `latitude`, `altitude`, `tagente_id_agente`, `start_timestamp`, + `end_timestamp`, `description`, `number_of_packages`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', + $longitude, $latitude, $altitude, $agent_id, $start_timestamp, $end_timestamp, $description, $number_packages); } diff --git a/pandora_server/man/man3/PandoraFMS::Core.pm.3 b/pandora_server/man/man3/PandoraFMS::Core.pm.3 index b1ed8b53f6..cdc62c4336 100644 --- a/pandora_server/man/man3/PandoraFMS::Core.pm.3 +++ b/pandora_server/man/man3/PandoraFMS::Core.pm.3 @@ -124,7 +124,7 @@ .\" ======================================================================== .\" .IX Title "PandoraFMS::Core 3" -.TH PandoraFMS::Core 3 "2010-02-15" "perl v5.10.0" "User Contributed Perl Documentation" +.TH PandoraFMS::Core 3 "2010-02-18" "perl v5.10.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -315,23 +315,15 @@ Returns: .el .SS "\f(CWpandora_ping_latency (\fP\f(CI$pa_config\fP\f(CW, \fP\f(CI$host\fP\f(CW)\fP" .IX Subsection "pandora_ping_latency ($pa_config, $host)" Ping the given host. Returns the average round-trip time. -.ie n .SS """update_agent_position (\fI$pa_config\fP, \fI$timestamp\fP, \fI$agent_id\fP, \fI$dbh\fP)""" -.el .SS "\f(CWupdate_agent_position (\fP\f(CI$pa_config\fP\f(CW, \fP\f(CI$timestamp\fP\f(CW, \fP\f(CI$agent_id\fP\f(CW, \fP\f(CI$dbh\fP\f(CW)\fP" -.IX Subsection "update_agent_position ($pa_config, $timestamp, $agent_id, $dbh)" -# Updates agent's \fIend_timestamp\fR and \fInumber_of_packages\fR in \fBtgis_data\fR table -.ie n .SS """save_agent_position (\fI$pa_config\fP, \fI$timestamp\fP, \fI$longitude\fP, \fI$latitude\fP, \fI$altitude\fP, \fI$description\fP, \fI$agent_id\fP, \fI$dbh\fP)""" -.el .SS "\f(CWsave_agent_position (\fP\f(CI$pa_config\fP\f(CW, \fP\f(CI$timestamp\fP\f(CW, \fP\f(CI$longitude\fP\f(CW, \fP\f(CI$latitude\fP\f(CW, \fP\f(CI$altitude\fP\f(CW, \fP\f(CI$description\fP\f(CW, \fP\f(CI$agent_id\fP\f(CW, \fP\f(CI$dbh\fP\f(CW)\fP" -.IX Subsection "save_agent_position ($pa_config, $timestamp, $longitude, $latitude, $altitude, $description, $agent_id, $dbh)" -Saves the last position of an agent in the \fBtgis_data\fR table -.SH "DEPENDENCIES" -.IX Header "DEPENDENCIES" -\&\s-1DBI\s0, XML::Simple, HTML::Entities, Time::Local, \s-1POSIX\s0, PandoraFMS::DB, PandoraFMS::Config, PandoraFMS::Tools, PandoraFMS::GIS -.SH "LICENSE" -.IX Header "LICENSE" -This is released under the \s-1GNU\s0 Lesser General Public License. -.SH "SEE ALSO" -.IX Header "SEE ALSO" -\&\s-1DBI\s0, XML::Simple, HTML::Entities, Time::Local, \s-1POSIX\s0, PandoraFMS::DB, PandoraFMS::Config, PandoraFMS::Tools, PandoraFMS::GIS -.SH "COPYRIGHT" -.IX Header "COPYRIGHT" -Copyright (c) 2005\-2010 Artica Soluciones Tecnologicas S.L +.ie n .SS """save_agent_position (\fI$pa_config\fP, \fI$current_longitude\fP, \fI$current_latitude\fP, \fI$current_altitude\fP, \fI$agent_id\fP, \fI$dbh\fP, [\fI$start_timestamp\fP], [\fI$description\fP])""" +.el .SS "\f(CWsave_agent_position (\fP\f(CI$pa_config\fP\f(CW, \fP\f(CI$current_longitude\fP\f(CW, \fP\f(CI$current_latitude\fP\f(CW, \fP\f(CI$current_altitude\fP\f(CW, \fP\f(CI$agent_id\fP\f(CW, \fP\f(CI$dbh\fP\f(CW, [\fP\f(CI$start_timestamp\fP\f(CW], [\fP\f(CI$description\fP\f(CW])\fP" +.IX Subsection "save_agent_position ($pa_config, $current_longitude, $current_latitude, $current_altitude, $agent_id, $dbh, [$start_timestamp], [$description])" +Saves a new agent \s-1GIS\s0 information record in \fBtgis_data_status\fR table. +.ie n .SS """update_agent_position (\fI$pa_config\fP, \fI$current_longitude\fP, \fI$current_latitude\fP, \fI$current_altitude\fP, \fI$agent_id\fP, \fI$dbh\fP, [\fI$stored_longitude\fP], [\fI$stored_latitude\fP], [\fI$stored_altitude\fP], [\fI$start_timestamp\fP], [\fI$description\fP])""" +.el .SS "\f(CWupdate_agent_position (\fP\f(CI$pa_config\fP\f(CW, \fP\f(CI$current_longitude\fP\f(CW, \fP\f(CI$current_latitude\fP\f(CW, \fP\f(CI$current_altitude\fP\f(CW, \fP\f(CI$agent_id\fP\f(CW, \fP\f(CI$dbh\fP\f(CW, [\fP\f(CI$stored_longitude\fP\f(CW], [\fP\f(CI$stored_latitude\fP\f(CW], [\fP\f(CI$stored_altitude\fP\f(CW], [\fP\f(CI$start_timestamp\fP\f(CW], [\fP\f(CI$description\fP\f(CW])\fP" +.IX Subsection "update_agent_position ($pa_config, $current_longitude, $current_latitude, $current_altitude, $agent_id, $dbh, [$stored_longitude], [$stored_latitude], [$stored_altitude], [$start_timestamp], [$description])" +Updates agent \s-1GIS\s0 information in \fBtgis_data_status\fR table. +.ie n .SS """archive_agent_position (\fI$pa_config\fP, \fI$start_timestamp\fP, \fI$end_timestamp\fP, \fI$longitude\fP, \fI$latitude\fP, \fI$altitude\fP, \fI$description\fP, \fI$number_packages\fP, \fI$agent_id\fP, \fI$dbh\fP)""" +.el .SS "\f(CWarchive_agent_position (\fP\f(CI$pa_config\fP\f(CW, \fP\f(CI$start_timestamp\fP\f(CW, \fP\f(CI$end_timestamp\fP\f(CW, \fP\f(CI$longitude\fP\f(CW, \fP\f(CI$latitude\fP\f(CW, \fP\f(CI$altitude\fP\f(CW, \fP\f(CI$description\fP\f(CW, \fP\f(CI$number_packages\fP\f(CW, \fP\f(CI$agent_id\fP\f(CW, \fP\f(CI$dbh\fP\f(CW)\fP" +.IX Subsection "archive_agent_position ($pa_config, $start_timestamp, $end_timestamp, $longitude, $latitude, $altitude, $description, $number_packages, $agent_id, $dbh)" +Archives the last position of an agent in the \fBtgis_data_history\fR table