From 2664eba7b29c0e1970162d423de420b0f09a175d Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Tue, 28 Jun 2011 10:04:45 +0000 Subject: [PATCH] 2011-06-28 Sergio Martin * lib/PandoraFMS/DB.pm lib/PandoraFMS/ReconServer.pm: Moved add_address function to DB to common uses and create add_new_address_agent function into DB library with repeated code for the same git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4492 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 7 ++++ pandora_server/lib/PandoraFMS/DB.pm | 34 +++++++++++++++++++- pandora_server/lib/PandoraFMS/ReconServer.pm | 27 ++-------------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index dbc3973752..94ca6275af 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,10 @@ +2011-06-28 Sergio Martin + + * lib/PandoraFMS/DB.pm + lib/PandoraFMS/ReconServer.pm: Moved add_address function + to DB to common uses and create add_new_address_agent function + into DB library with repeated code for the same + 2011-06-27 Junichi Satoh * lib/PandoraFMS/NetworkServer.pm: Fixed SNMP PROC modules always diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index e13edb5d92..b34a429827 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -27,7 +27,9 @@ require Exporter; our @ISA = ("Exporter"); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our @EXPORT = qw( +our @EXPORT = qw( + add_address + add_new_address_agent db_connect db_disconnect db_do @@ -39,6 +41,7 @@ our @EXPORT = qw( db_text db_update get_action_id + get_addr_id get_agent_id get_agent_address get_agent_group @@ -512,6 +515,35 @@ sub db_process_update($$$$$;@) { return $res; } +########################################################################## +# Add the given address to taddress. +########################################################################## +sub add_address ($$) { + my ($dbh, $ip_address) = @_; + + return db_insert ($dbh, 'id_a', 'INSERT INTO taddress (ip) VALUES (?)', $ip_address); +} + +########################################################################## +# Assign the new address to the agent +########################################################################## +sub add_new_address_agent ($$$) { + my ($dbh, $addr_id, $agent_id) = @_; + + db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`) + VALUES (?, ?)', $addr_id, $agent_id); +} + +########################################################################## +# Return the ID of the given address, -1 if it does not exist. +########################################################################## +sub get_addr_id ($$) { + my ($dbh, $addr) = @_; + + my $addr_id = get_db_value ($dbh, 'SELECT id_a FROM taddress WHERE ip = ?', $addr); + return (defined ($addr_id) ? $addr_id : -1); +} + ########################################################################## ## Generic SQL sentence. ########################################################################## diff --git a/pandora_server/lib/PandoraFMS/ReconServer.pm b/pandora_server/lib/PandoraFMS/ReconServer.pm index fd58c3dca2..4d07d98d67 100644 --- a/pandora_server/lib/PandoraFMS/ReconServer.pm +++ b/pandora_server/lib/PandoraFMS/ReconServer.pm @@ -288,9 +288,8 @@ sub data_consumer ($$) { } # Assign the new address to the agent - db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`) - VALUES (?, ?)', $addr_id, $agent_id); - + add_new_address_agent ($dbh, $addr_id, $agent_id); + # Create network profile modules for the agent create_network_profile_modules ($pa_config, $dbh, $agent_id, $task->{'id_network_profile'}, $addr, $task->{'snmp_community'}); @@ -432,8 +431,7 @@ sub get_host_parent ($$$$$$){ my $agent_id = pandora_create_agent ($pa_config, $pa_config->{'servername'}, $parent_name, $parent_addr, $group, $parent_parent, $id_os, '', 300, $dbh); # Assign the new address to the agent - db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`) - VALUES (?, ?)', $addr_id, $agent_id); + add_new_address_agent ($dbh, $addr_id, $agent_id); return $agent_id; } @@ -484,16 +482,6 @@ sub guess_os { return pandora_get_os ($output); } -########################################################################## -# Return the ID of the given address, -1 if it does not exist. -########################################################################## -sub get_addr_id ($$) { - my ($dbh, $addr) = @_; - - my $addr_id = get_db_value ($dbh, 'SELECT id_a FROM taddress WHERE ip = ?', $addr); - return (defined ($addr_id) ? $addr_id : -1); -} - ########################################################################## # Return the ID of the agent with the given IP. ########################################################################## @@ -518,15 +506,6 @@ sub update_recon_task ($$$) { db_do ($dbh, 'UPDATE trecon_task SET utimestamp = ?, status = ? WHERE id_rt = ?', time (), $status, $id_task); } -########################################################################## -# Add the given address to taddress. -########################################################################## -sub add_address ($$) { - my ($dbh, $ip_address) = @_; - - return db_insert ($dbh, 'id_a', 'INSERT INTO taddress (ip) VALUES (?)', $ip_address); -} - ########################################################################## # Create network profile modules for the given agent. ##########################################################################