From 14e38c0003a0ca3bf2d32e34c66e391a2f0be1df Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Fri, 15 Feb 2013 10:27:13 +0000 Subject: [PATCH] 2013-02-15 Sergio Martin * lib/PandoraFMS/Core.pm lib/PandoraFMS/ReconServer.pm util/pandora_manage.pl: Encapsulate the code of create a module from a network component. Unify calls of CLI and Recon Server to dont break module counts git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7660 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 8 +++ pandora_server/lib/PandoraFMS/Core.pm | 61 ++++++++++++++++++++ pandora_server/lib/PandoraFMS/ReconServer.pm | 19 ++---- pandora_server/util/pandora_manage.pl | 20 ++----- 4 files changed, 77 insertions(+), 31 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 8e794aa56e..ceffb5a453 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,11 @@ +2013-02-15 Sergio Martin + + * lib/PandoraFMS/Core.pm + lib/PandoraFMS/ReconServer.pm + util/pandora_manage.pl: Encapsulate the code of create + a module from a network component. Unify calls of CLI + and Recon Server to dont break module counts + 2013-02-14 Sergio Martin * lib/PandoraFMS/Core.pm diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 49fc55d883..616bbdac6f 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -138,6 +138,8 @@ our @EXPORT = qw( pandora_create_incident pandora_create_module pandora_create_module_from_hash + pandora_create_module_from_network_component + pandora_create_module_tags pandora_create_template_module pandora_create_template_module_action pandora_delete_agent @@ -2216,6 +2218,44 @@ sub pandora_delete_module ($$;$) { db_do ($dbh, 'UPDATE tagente SET total_count=total_count-1 WHERE id_agente=' . $module->{'id_agente'}); } +########################################################################## +## Create an agent module from network component +########################################################################## +sub pandora_create_module_from_network_component ($$$$) { + my ($pa_config, $component, $id_agent, $dbh) = @_; + + my $addr = get_agent_address($dbh, $id_agent); + + logger($pa_config, "Processing network component '" . safe_output ($component->{'name'}) . "' for agent $addr.", 10); + + # The modules are created enabled and with the flag activated to force first execution + $component->{'flag'} = 1; + $component->{'disabled'} = 0; + + # Set the agent id + $component->{'id_agente'} = $id_agent; + + # Delete the fields that will not be inserted in the modules table + delete $component->{'id_nc'}; + $component->{'nombre'} = $component->{'name'}; + delete $component->{'name'}; + $component->{'descripcion'} = $component->{'description'}; + delete $component->{'description'}; + delete $component->{'id_group'}; + my $component_tags = $component->{'tags'}; + delete $component->{'tags'}; + $component->{'id_tipo_modulo'} = $component->{'type'}; + delete $component->{'type'}; + $component->{'ip_target'} = $addr; + + my $module_id = pandora_create_module_from_hash($pa_config, $component, $dbh); + + # Propagate the tags to the module + pandora_create_module_tags ($pa_config, $dbh, $module_id, $component_tags); + + logger($pa_config, 'Creating module ' . safe_output ($component->{'nombre'}) . " (ID $module_id) for agent $addr from network component.", 10); +} + ########################################################################## ## Create an agent module from hash ########################################################################## @@ -2313,6 +2353,27 @@ sub pandora_get_config_value ($$) { return (defined ($config_value) ? $config_value : ""); } +########################################################################## +=head2 C<< pandora_create_module_tags (I<$pa_config>, I<$dbh>, I<$id_agent_module>, I<$serialized_tags>) >> + +Associate tags in a module. The tags are passed separated by commas + +=cut +########################################################################## + +sub pandora_create_module_tags ($$$$) { + my ($pa_config, $dbh, $id_agent_module, $serialized_tags) = @_; + + if($serialized_tags eq '') { + return 0; + } + + foreach my $tag_name (split (',', $serialized_tags)) { + my $tag_id = get_db_value ($dbh, "SELECT id_tag FROM ttag WHERE name = ?", $tag_name); + db_insert ($dbh, 'id_tag', "INSERT INTO ttag_module (`id_tag`, `id_agente_modulo`) VALUES (?, ?)", $tag_id, $id_agent_module); + } +} + ########################################################################## =head2 C<< pandora_create_agent (I<$pa_config>, I<$server_name>, I<$agent_name>, I<$address>, I<$group_id>, I<$parent_id>, I<$os_id>, I<$description>, I<$interval>, I<$dbh>, [I<$timezone_offset>], [I<$longitude>], [I<$latitude>], [I<$altitude>], [I<$position_description>], [I<$custom_id>], [I<$url_address>]) >> diff --git a/pandora_server/lib/PandoraFMS/ReconServer.pm b/pandora_server/lib/PandoraFMS/ReconServer.pm index b01442b9bc..ac1c1674bf 100644 --- a/pandora_server/lib/PandoraFMS/ReconServer.pm +++ b/pandora_server/lib/PandoraFMS/ReconServer.pm @@ -443,29 +443,18 @@ sub create_network_profile_modules { my @np_components = get_db_rows ($dbh, 'SELECT * FROM tnetwork_profile_component WHERE id_np = ?', $np_id); foreach my $np_component (@np_components) { - # Get network component data - my $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component wHERE id_nc = ?', $np_component->{'id_nc'}); + my $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component WHERE id_nc = ?', $np_component->{'id_nc'}); + if (! defined ($component)) { logger($pa_config, "Network component ID " . $np_component->{'id_nc'} . " for agent $addr not found.", 3); next; } - logger($pa_config, "Processing network component '" . safe_output ($component->{'name'}) . "' for agent $addr.", 10); - # Use snmp_community from network task instead the component snmp_community $component->{'snmp_community'} = safe_output ($snmp_community); - - # Create the module - my $module_id = db_insert ($dbh, 'id_agente_modulo', 'INSERT INTO tagente_modulo (id_agente, id_tipo_modulo, descripcion, nombre, max, min, module_interval, tcp_port, tcp_send, tcp_rcv, snmp_community, snmp_oid, ip_target, id_module_group, flag, disabled, plugin_user, plugin_pass, plugin_parameter, max_timeout, id_modulo, min_warning, max_warning, str_warning, min_critical, max_critical, str_critical, min_ff_event, id_plugin, post_process, critical_instructions, warning_instructions, unknown_instructions) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', - $agent_id, $component->{'type'}, $component->{'description'}, $component->{'name'}, $component->{'max'}, $component->{'min'}, $component->{'module_interval'}, $component->{'tcp_port'}, $component->{'tcp_send'}, $component->{'tcp_rcv'}, $component->{'snmp_community'}, - $component->{'snmp_oid'}, $addr, $component->{'id_module_group'}, $component->{'plugin_user'}, $component->{'plugin_pass'}, $component->{'plugin_parameter'}, $component->{'max_timeout'}, $component->{'id_modulo'}, $component->{'min_warning'}, $component->{'max_warning'}, $component->{'str_warning'}, $component->{'min_critical'}, $component->{'max_critical'}, $component->{'str_critical'}, $component->{'min_ff_event'}, $component->{'id_plugin'}, $component->{'post_process'}, $component->{'critical_instructions'}, $component->{'warning_instructions'}, $component->{'unknown_instructions'}); - - # An entry in tagente_estado is necessary for the module to work - db_do ($dbh, 'INSERT INTO tagente_estado (`id_agente_modulo`, `id_agente`, `last_try`, current_interval) VALUES (?, ?, \'1970-01-01 00:00:00\', ?)', $module_id, $agent_id, $component->{'module_interval'}); - - logger($pa_config, 'Creating module ' . safe_output ($component->{'name'}) . " for agent $addr from network component.", 10); + + pandora_create_module_from_network_component($pa_config, $component, $agent_id, $dbh); } } diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 69fdd8a733..3fb59589da 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -1034,29 +1034,17 @@ sub cli_create_network_module_from_component() { my $agent_id = get_agent_id($dbh,$agent_name); exist_check($agent_id,'agent',$agent_name); - - my $addr = get_agent_address($dbh,$agent_id); - - my $nc_id = pandora_get_network_component_id($dbh,$component_name); + + my $nc_id = pandora_get_network_component_id($dbh, $component_name); exist_check($nc_id,'network component',$component_name); my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); non_exist_check($module_exists, 'module name', $component_name); # Get network component data - my $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component wHERE id_nc = ?', $nc_id); + my $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component WHERE id_nc = ?', $nc_id); - # Create the module - my $module_id = db_insert ($dbh, 'id_agente_modulo', 'INSERT INTO tagente_modulo (id_agente, id_tipo_modulo, descripcion, nombre, max, min, module_interval, tcp_port, tcp_send, tcp_rcv, snmp_community, snmp_oid, ip_target, id_module_group, flag, disabled, plugin_user, plugin_pass, plugin_parameter, max_timeout, id_modulo, min_warning, max_warning, str_warning, min_critical, max_critical, str_critical, min_ff_event, id_plugin, post_process) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', - $agent_id, $component->{'type'}, $component->{'description'}, safe_input($component->{'name'}), $component->{'max'}, $component->{'min'}, $component->{'module_interval'}, $component->{'tcp_port'}, $component->{'tcp_send'}, $component->{'tcp_rcv'}, $component->{'snmp_community'}, - $component->{'snmp_oid'}, $addr, $component->{'id_module_group'}, $component->{'plugin_user'}, $component->{'plugin_pass'}, $component->{'plugin_parameter'}, $component->{'max_timeout'}, $component->{'id_modulo'}, $component->{'min_warning'}, $component->{'max_warning'}, $component->{'str_warning'}, $component->{'min_critical'}, $component->{'max_critical'}, $component->{'str_critical'}, $component->{'min_ff_event'}, $component->{'id_plugin'}, $component->{'post_process'}); - - # An entry in tagente_estado is necessary for the module to work - db_do ($dbh, 'INSERT INTO tagente_estado (`id_agente_modulo`, `id_agente`, `last_try`, current_interval) VALUES (?, ?, \'1970-01-01 00:00:00\', ?)', $module_id, $agent_id, $component->{'module_interval'}); - - logger($conf, 'Creating module ' . $component->{'name'} . " for agent $agent_name from network component '" . $component->{'name'} . "'.", 10); - + pandora_create_module_from_network_component ($conf, $component, $agent_id, $dbh); } ##############################################################################