diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index e6d247b421..1d800fe386 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -10142,4 +10142,147 @@ function api_set_metaconsole_synch($keys) { } +function api_set_apply_module_template($id_template, $id_agent, $thrash3, $thrash4) { + + if (isset ($id_template)) { + // Take agent data + $row = db_get_row ("tagente", "id_agente", $id_agent); + + if ($row !== false) { + $intervalo = $row["intervalo"]; + $nombre_agente = $row["nombre"]; + $direccion_agente =$row["direccion"]; + $ultima_act = $row["ultimo_contacto"]; + $ultima_act_remota =$row["ultimo_contacto_remoto"]; + $comentarios = $row["comentarios"]; + $id_grupo = $row["id_grupo"]; + $id_os= $row["id_os"]; + $os_version = $row["os_version"]; + $agent_version = $row["agent_version"]; + $disabled= $row["disabled"]; + } + else { + return; + } + + $id_np = $id_template; + $name_template = db_get_value ('name', 'tnetwork_profile', 'id_np', $id_np); + $npc = db_get_all_rows_field_filter ("tnetwork_profile_component", "id_np", $id_np); + + if ($npc === false) { + $npc = array (); + } + + $success_count = $error_count = 0; + $modules_already_added = array(); + + foreach ($npc as $row) { + $nc = db_get_all_rows_field_filter ("tnetwork_component", "id_nc", $row["id_nc"]); + + if ($nc === false) { + $nc = array (); + } + foreach ($nc as $row2) { + // Insert each module from tnetwork_component into agent + $values = array( + 'id_agente' => $id_agent, + 'id_tipo_modulo' => $row2["type"], + 'descripcion' => __('Created by template ').$name_template. ' . '.$row2["description"], + 'max' => $row2["max"], + 'min' => $row2["min"], + 'module_interval' => $row2["module_interval"], + 'tcp_port' => $row2["tcp_port"], + 'tcp_send' => $row2["tcp_send"], + 'tcp_rcv' => $row2["tcp_rcv"], + 'snmp_community' => $row2["snmp_community"], + 'snmp_oid' => $row2["snmp_oid"], + 'ip_target' => $direccion_agente, + 'id_module_group' => $row2["id_module_group"], + 'id_modulo' => $row2["id_modulo"], + 'plugin_user' => $row2["plugin_user"], + 'plugin_pass' => $row2["plugin_pass"], + 'plugin_parameter' => $row2["plugin_parameter"], + 'unit' => $row2["unit"], + 'max_timeout' => $row2["max_timeout"], + 'max_retries' => $row2["max_retries"], + 'id_plugin' => $row2['id_plugin'], + 'post_process' => $row2['post_process'], + 'dynamic_interval' => $row2['dynamic_interval'], + 'dynamic_max' => $row2['dynamic_max'], + 'dynamic_min' => $row2['dynamic_min'], + 'dynamic_two_tailed' => $row2['dynamic_two_tailed'], + 'min_warning' => $row2['min_warning'], + 'max_warning' => $row2['max_warning'], + 'str_warning' => $row2['str_warning'], + 'min_critical' => $row2['min_critical'], + 'max_critical' => $row2['max_critical'], + 'str_critical' => $row2['str_critical'], + 'critical_inverse' => $row2['critical_inverse'], + 'warning_inverse' => $row2['warning_inverse'], + 'critical_instructions' => $row2['critical_instructions'], + 'warning_instructions' => $row2['warning_instructions'], + 'unknown_instructions' => $row2['unknown_instructions'], + 'id_category' => $row2['id_category'], + 'macros' => $row2['macros'], + 'each_ff' => $row2['each_ff'], + 'min_ff_event' => $row2['min_ff_event'], + 'min_ff_event_normal' => $row2['min_ff_event_normal'], + 'min_ff_event_warning' => $row2['min_ff_event_warning'], + 'min_ff_event_critical' => $row2['min_ff_event_critical'] + ); + + $name = $row2["name"]; + + // Put tags in array if the component has to add them later + if(!empty($row2["tags"])) { + $tags = explode(',', $row2["tags"]); + } + else { + $tags = array(); + } + + // Check if this module exists in the agent + $module_name_check = db_get_value_filter('id_agente_modulo', 'tagente_modulo', array('delete_pending' => 0, 'nombre' => $name, 'id_agente' => $id_agent)); + + if ($module_name_check !== false) { + $modules_already_added[] = $row2["name"]; + $error_count++; + } + else { + $id_agente_modulo = modules_create_agent_module($id_agent, $name, $values); + + if ($id_agente_modulo === false) { + $error_count++; + } + else { + if(!empty($tags)) { + // Creating tags + $tag_ids = array(); + foreach ($tags as $tag_name) { + $tag_id = tags_get_id($tag_name); + + //If tag exists in the system we store to create it + $tag_ids[] = $tag_id; + } + + tags_insert_module_tag ($id_agente_modulo, $tag_ids); + } + + $success_count++; + } + } + } + } + if ($error_count > 0) { + if (empty($modules_already_added)) + ui_print_error_message(__('Error adding modules') . sprintf(' (%s)', $error_count)); + else + ui_print_error_message(__('Error adding modules. The following errors already exists: ') . implode(', ', $modules_already_added)); + } + if ($success_count > 0) + ui_print_success_message(__('Modules successfully added')); + } + +} + ?> diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 90db5fa2f7..c07d838da8 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -36,7 +36,7 @@ use Encode::Locale; Encode::Locale::decode_argv; # version: define current version -my $version = "7.0NG.719 PS180226"; +my $version = "7.0NG.719 PS180219"; # save program name for logging my $progname = basename($0); @@ -6066,6 +6066,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 3, 2); cli_export_visual_console(); } + elsif ($param eq '--apply_module_template') { + param_check($ltotal, 2, 2); + cli_apply_module_template(); + } else { print_log "[ERROR] Invalid option '$param'.\n\n"; $param = ''; @@ -6436,3 +6440,95 @@ sub cli_add_tag_to_module() { my $result = api_call(\%conf, 'set', 'add_tag_module', $module_id, $tag_id); print "\n$result\n"; } + +sub cli_apply_module_template() { + my ($id_template, $id_agent) = @ARGV[2..3]; + + my @row = get_db_rows ($dbh,"select * from tagente where id_agente = ".$id_agent); + + return if (scalar (@row) == 0); + + my $name_template = get_db_value ($dbh,'select name from tnetwork_profile where id_np = '.$id_template); + + my @npc = get_db_rows($dbh,"select * from tnetwork_profile_component where id_np = ".$id_template); + + foreach my $component (@npc) { + + my @template_values = get_db_rows ($dbh,"SELECT * FROM tnetwork_component where id_nc = ".$component->{'id_nc'}); + + return if (scalar (@template_values) == 0); + + foreach my $element (@template_values) { + my $agent_values; + $agent_values->{'id_agente'} = $id_agent; + $agent_values->{'id_tipo_modulo'} = $element->{"type"}; + $agent_values->{'descripcion'} = 'Created by template '.$name_template.' '.$element->{"description"}; + $agent_values->{'max'} = $element->{"max"}; + $agent_values->{'min'} = $element->{"min"}; + $agent_values->{'module_interval'} = $element->{"module_interval"}; + $agent_values->{'tcp_port'} = $element->{"tcp_port"}; + $agent_values->{'tcp_send'} = $element->{"tcp_send"}; + $agent_values->{'tcp_rcv'} = $element->{"tcp_rcv"}; + $agent_values->{'snmp_community'} = $element->{"snmp_community"}; + $agent_values->{'snmp_oid'} = $element->{"snmp_oid"}; + $agent_values->{'ip_target'} = $row[0]->{"direccion"}; + $agent_values->{'id_module_group'} = $element->{"id_module_group"}; + $agent_values->{'id_modulo'} = $element->{"id_modulo"}; + $agent_values->{'plugin_user'} = $element->{"plugin_user"}; + $agent_values->{'plugin_pass'} = $element->{"plugin_pass"}; + $agent_values->{'plugin_parameter'} = $element->{"plugin_parameter"}; + $agent_values->{'unit'} = $element->{"unit"}; + $agent_values->{'max_timeout'} = $element->{"max_timeout"}; + $agent_values->{'max_retries'} = $element->{"max_retries"}; + $agent_values->{'id_plugin'} = $element->{"id_plugin"}; + $agent_values->{'post_process'} = $element->{"post_process"}; + $agent_values->{'dynamic_interval'} = $element->{"dynamic_interval"}; + $agent_values->{'dynamic_max'} = $element->{"dynamic_max"}; + $agent_values->{'dynamic_min'} = $element->{"dynamic_min"}; + $agent_values->{'dynamic_two_tailed'} = $element->{"dynamic_two_tailed"}; + $agent_values->{'min_warning'} = $element->{"min_warning"}; + $agent_values->{'max_warning'} = $element->{"max_warning"}; + $agent_values->{'str_warning'} = $element->{"str_warning"}; + $agent_values->{'min_critical'} = $element->{"min_critical"}; + $agent_values->{'max_critical'} = $element->{"max_critical"}; + $agent_values->{'str_critical'} = $element->{"str_critical"}; + $agent_values->{'critical_inverse'} = $element->{"critical_inverse"}; + $agent_values->{'warning_inverse'} = $element->{"warning_inverse"}; + $agent_values->{'critical_instructions'} = $element->{"critical_instructions"}; + $agent_values->{'warning_instructions'} = $element->{"warning_instructions"}; + $agent_values->{'unknown_instructions'} = $element->{"unknown_instructions"}; + $agent_values->{'id_category'} = $element->{"id_category"}; + $agent_values->{'macros'} = $element->{"macros"}; + $agent_values->{'each_ff'} = $element->{"each_ff"}; + $agent_values->{'min_ff_event'} = $element->{"min_ff_event"}; + $agent_values->{'min_ff_event_normal'} = $element->{"min_ff_event_normal"}; + $agent_values->{'min_ff_event_warning'} = $element->{"min_ff_event_warning"}; + $agent_values->{'min_ff_event_critical'} = $element->{"min_ff_event_critical"}; + $agent_values->{'nombre'} = $element->{"name"}; + + my @tags; + if($element->{"tags"} ne '') { + @tags = split(',', $element->{"tags"}); + } + + my $module_name_check = get_db_value ($dbh,'select id_agente_modulo from tagente_modulo where delete_pending = 0 and nombre ="'.$agent_values->{'nombre'}.'" and id_agente = '.$id_agent); + + if (!defined($module_name_check)) { + + my $id_agente_modulo = pandora_create_module_from_hash(\%conf,$agent_values,$dbh); + + if ($id_agente_modulo != -1) { + + foreach my $tag_name (@tags) { + + my $tag_id = get_db_value($dbh,'select id_tag from ttag where name = "'.$tag_name.'"'); + + db_do($dbh,'insert into ttag_module (id_tag,id_agente_modulo) values ("'.$tag_id.'","'.$id_agente_modulo.'")'); + + } + } + } + } + } +} +