From 5eb469008c0818cf143ae62d3e184a8aa9c36ffe Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Thu, 9 Dec 2010 15:42:58 +0000 Subject: [PATCH] 2010-12-09 Sergio Martin * lib/PandoraFMS/Core.pm util/pandora_manage.pl util/pandora_sync_db.pl: Added agents and modules existence checks on source and destination in sync db script. Added messages in execution time git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3656 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 8 ++++ pandora_server/lib/PandoraFMS/Core.pm | 59 ++++++++++++++++++++++++++ pandora_server/util/pandora_manage.pl | 55 ------------------------ pandora_server/util/pandora_sync_db.pl | 27 ++++++++++-- 4 files changed, 90 insertions(+), 59 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index da63133d4d..b4007b4ee4 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,11 @@ +2010-12-09 Sergio Martin + + * lib/PandoraFMS/Core.pm + util/pandora_manage.pl + util/pandora_sync_db.pl: Added agents and modules + existence checks on source and destination in + sync db script. Added messages in execution time + 2010-12-09 Sergio Martin * util/recon_scripts/snmpdevices.pl: Fixed a typo from the diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index c2b6391943..be696257bd 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -132,6 +132,8 @@ our @EXPORT = qw( pandora_create_incident pandora_create_module pandora_create_module_from_hash + pandora_delete_agent + pandora_delete_module pandora_evaluate_alert pandora_evaluate_compound_alert pandora_evaluate_snmp_alerts @@ -1118,6 +1120,27 @@ sub pandora_create_module ($$$$$$$$$$) { return $module_id; } +########################################################################## +## Delete a module given its id. +########################################################################## +sub pandora_delete_module ($$) { + my ($dbh, $module_id) = @_; + + # Delete Graphs, layouts & reports + db_do ($dbh, 'DELETE FROM tgraph_source WHERE id_agent_module = ?', $module_id); + db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agente_modulo = ?', $module_id); + db_do ($dbh, 'DELETE FROM treport_content WHERE id_agent_module = ?', $module_id); + + # Delete the module state + db_do ($dbh, 'DELETE FROM tagente_estado WHERE id_agente_modulo = ?', $module_id); + + # Delete templates asociated to the module + db_do ($dbh, 'DELETE FROM talert_template_modules WHERE id_agent_module = ?', $module_id); + + # Set pending delete the module + db_do ($dbh, 'UPDATE tagente_modulo SET disabled = 1, delete_pending = 1 WHERE id_agente_modulo = ?', $module_id); +} + ########################################################################## ## Create an agent module from hash ########################################################################## @@ -1193,6 +1216,42 @@ sub pandora_create_agent ($$$$$$$$$$;$$$$$) { return $agent_id; } +########################################################################## +## Delete an agent given its id. +########################################################################## +sub pandora_delete_agent ($$;$) { + my ($dbh, $agent_id, $conf) = @_; + my $agent_name = get_agent_name($dbh, $agent_id); + + # Delete from all their policies + enterprise_hook('pandora_delete_agent_from_policies', [$agent_id, $dbh]); + + # Delete the agent + db_do ($dbh, 'DELETE FROM tagente WHERE id_agente = ?', $agent_id); + + # Delete agent access data + db_do ($dbh, 'DELETE FROM tagent_access WHERE id_agent = ?', $agent_id); + + # Delete addresses + db_do ($dbh, 'DELETE FROM taddress_agent WHERE id_ag = ?', $agent_id); + + my @modules = get_db_rows ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ?', $agent_id); + + if(defined $conf) { + # Delete the conf files + if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { + unlink($conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + } + if (-e $conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5') { + unlink($conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5'); + } + } + + foreach my $module (@modules) { + pandora_delete_module ($dbh, $module->{'id_agente_modulo'}); + } +} + ########################################################################## =head2 C<< pandora_event (I<$pa_config>, I<$evento>, I<$id_grupo>, I<$id_agente>, I<$severity>, I<$id_alert_am>, I<$id_agentmodule>, I<$event_type>, I<$event_status>, I<$dbh>) >> diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 1f1c1d6852..99978a3bbc 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -174,61 +174,6 @@ sub pandora_manage_init ($) { help_screen () if ($conf->{'pandora_path'} =~ m/--*h\w*\z/i ); } -########################################################################## -## Delete a module given its id. -########################################################################## -sub pandora_delete_module ($$) { - my ($dbh, $module_id) = @_; - - # Delete Graphs, layouts & reports - db_do ($dbh, 'DELETE FROM tgraph_source WHERE id_agent_module = ?', $module_id); - db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agente_modulo = ?', $module_id); - db_do ($dbh, 'DELETE FROM treport_content WHERE id_agent_module = ?', $module_id); - - # Delete the module state - db_do ($dbh, 'DELETE FROM tagente_estado WHERE id_agente_modulo = ?', $module_id); - - # Delete templates asociated to the module - db_do ($dbh, 'DELETE FROM talert_template_modules WHERE id_agent_module = ?', $module_id); - - # Set pending delete the module - db_do ($dbh, 'UPDATE tagente_modulo SET disabled = 1, delete_pending = 1 WHERE id_agente_modulo = ?', $module_id); -} - -########################################################################## -## Delete an agent given its id. -########################################################################## -sub pandora_delete_agent ($$$) { - my ($dbh, $agent_id, $conf) = @_; - my $agent_name = get_agent_name($dbh, $agent_id); - - # Delete from all their policies - enterprise_hook('pandora_delete_agent_from_policies', [$agent_id, $dbh]); - - # Delete the agent - db_do ($dbh, 'DELETE FROM tagente WHERE id_agente = ?', $agent_id); - - # Delete agent access data - db_do ($dbh, 'DELETE FROM tagent_access WHERE id_agent = ?', $agent_id); - - # Delete addresses - db_do ($dbh, 'DELETE FROM taddress_agent WHERE id_ag = ?', $agent_id); - - my @modules = get_db_rows ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ?', $agent_id); - - # Delete the conf files - if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { - unlink($conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); - } - if (-e $conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5') { - unlink($conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5'); - } - - foreach my $module (@modules) { - pandora_delete_module ($dbh, $module->{'id_agente_modulo'}); - } -} - ########################################################################## ## Create a template module. ########################################################################## diff --git a/pandora_server/util/pandora_sync_db.pl b/pandora_server/util/pandora_sync_db.pl index b247256835..52edbd3481 100755 --- a/pandora_server/util/pandora_sync_db.pl +++ b/pandora_server/util/pandora_sync_db.pl @@ -58,11 +58,30 @@ my $dbh_dest = db_connect ('mysql', $conf{'dbname_dest'}, $conf{'dbhost_dest'}, my $history_dbh = ($conf{'_history_db_enabled'} eq '1') ? db_connect ('mysql', $conf{'_history_db_name'}, $conf{'_history_db_host'}, '3306', $conf{'_history_db_user'}, $conf{'_history_db_pass'}) : undef; -# Build the comparation arrays (the id_agents must be compare first, doesnt touch) +print "\n[*] Preparing agents correlation.\n"; + +# Build the comparison arrays (the id_agents must be compare first) my $id_agent_comparation = enterprise_hook('sync_compare_id_agents', [$dbh_source, $dbh_dest, \$errors_agents]); my @id_agent_comparation = @{$id_agent_comparation}; + +print "\n[*] Checking destination agents missed on source.\n"; + +my $agents_deleted = enterprise_hook('sync_delete_dst_missed_agents', [$dbh_source, $dbh_dest, \$errors_agents]); + +print "\n[*] $agents_deleted agents deleted on destination.\n" unless $agents_deleted == 0; + +print "\n[*] Preparing modules correlation.\n"; + my $id_agentmodule_comparation = enterprise_hook('sync_compare_id_agent_modules', [$dbh_source, $dbh_dest, $id_agent_comparation, \$errors_modules]); my @id_agentmodule_comparation = @{$id_agentmodule_comparation}; + +print "\n[*] Checking destination modules missed on source.\n"; +my $modules_deleted = enterprise_hook('sync_delete_dst_missed_agent_modules', [$dbh_source, $dbh_dest, $id_agent_comparation, \$errors_modules]); + +print "\n[*] $modules_deleted modules deleted on destination.\n" unless $modules_deleted == 0; + +print "\n[*] Preparing servers correlation.\n"; + my $id_server_export_comparation = enterprise_hook('sync_compare_id_server_export', [$dbh_source, $dbh_dest, \$errors_exportservers]); my @id_server_export_comparation = @{$id_server_export_comparation}; my $id_server_comparation = enterprise_hook('sync_compare_id_server', [$dbh_source, $dbh_dest, \$errors_servers]); @@ -215,14 +234,14 @@ sub pandora_sync_main ($$$) { print "\n[*] Nothing to do. Exiting !\n\n"; } else { - print "\n[W] $errors errors in synchronization.\n\n"; + print "\n[W] $errors errors fixed in synchronization.\n\n"; print "Summary: \n"; if($errors_agents > 0) { - print "- $errors_agents Agents unsynchronized.\n"; + print "- $errors_agents Agents unsynchronized fixed.\n"; } if($errors_modules > 0) { - print "- $errors_modules Modules unsynchronized.\n"; + print "- $errors_modules Modules unsynchronized fixed.\n"; } if($errors_servers > 0) { print "- $errors_servers Servers unsynchronized.\n";