2013-02-15 Sergio Martin <sergio.martin@artica.es>

* 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
This commit is contained in:
zarzuelo 2013-02-15 10:27:13 +00:00
parent 468d08d909
commit 14e38c0003
4 changed files with 77 additions and 31 deletions

View File

@ -1,3 +1,11 @@
2013-02-15 Sergio Martin <sergio.martin@artica.es>
* 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 <sergio.martin@artica.es>
* lib/PandoraFMS/Core.pm

View File

@ -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>]) >>

View File

@ -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);
}
}

View File

@ -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);
}
##############################################################################