2011-05-09 Sergio Martin <sergio.martin@artica.es>

* lib/PandoraFMS/DB.pm
	lib/PandoraFMS/Core.pm
	util/pandora_manage.pl: Moved some functions from CLI to common
	files



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4317 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
zarzuelo 2011-05-09 09:42:01 +00:00
parent 98c4bd8c68
commit 59c2600490
4 changed files with 249 additions and 219 deletions

View File

@ -1,3 +1,10 @@
2011-05-09 Sergio Martin <sergio.martin@artica.es>
* lib/PandoraFMS/DB.pm
lib/PandoraFMS/Core.pm
util/pandora_manage.pl: Moved some functions from CLI to common
files
2011-05-07 Sancho Lerena <slerena@artica.es> 2011-05-07 Sancho Lerena <slerena@artica.es>
* lib/PandoraFMS/ReconServer.pm: Ported changes from 3.2 on Recon. * lib/PandoraFMS/ReconServer.pm: Ported changes from 3.2 on Recon.

View File

@ -132,7 +132,10 @@ our @EXPORT = qw(
pandora_create_incident pandora_create_incident
pandora_create_module pandora_create_module
pandora_create_module_from_hash pandora_create_module_from_hash
pandora_create_template_module
pandora_create_template_module_action
pandora_delete_agent pandora_delete_agent
pandora_delete_all_template_module_actions
pandora_delete_module pandora_delete_module
pandora_evaluate_alert pandora_evaluate_alert
pandora_evaluate_compound_alert pandora_evaluate_compound_alert
@ -156,6 +159,7 @@ our @EXPORT = qw(
pandora_update_module_on_error pandora_update_module_on_error
pandora_update_module_from_hash pandora_update_module_from_hash
pandora_update_server pandora_update_server
pandora_update_template_module
pandora_group_statistics pandora_group_statistics
pandora_server_statistics pandora_server_statistics
pandora_self_monitoring pandora_self_monitoring
@ -1030,6 +1034,79 @@ sub pandora_update_agent ($$$$$$$;$$$$$$) {
} }
##########################################################################
=head2 C<< pandora_create_template_module(I<$pa_config>, I<$id_agent_module>, I<$id_alert_template>, I<$dbh>, I<$id_policy_alerts>, I<$disabled>, I<$standby>) >>
Create a template module.
=cut
##########################################################################
sub pandora_create_template_module ($$$$;$$$) {
my ($pa_config, $id_agent_module, $id_alert_template, $dbh, $id_policy_alerts, $disabled, $standby) = @_;
$id_policy_alerts = 0 unless defined $id_policy_alerts;
$disabled = 0 unless defined $disabled;
$standby = 0 unless defined $standby;
my $module_name = get_module_name($dbh, $id_agent_module);
logger($pa_config, "Creating alert of template '$id_alert_template' on agent module '$module_name'.", 10);
$dbh->do("INSERT INTO talert_template_modules (`id_agent_module`, `id_alert_template`, `id_policy_alerts`, `disabled`, `standby`) VALUES ($id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby)");
return $dbh->{'mysql_insertid'};
}
##########################################################################
=head2 C<< pandora_update_template_module(I<$pa_config>, I<$id_alert>, I<$dbh>, I<$id_policy_alerts>, I<$disabled>, I<$standby>) >>
Update a template module.
=cut
##########################################################################
sub pandora_update_template_module ($$$;$$$) {
my ($pa_config, $id_alert, $dbh, $id_policy_alerts, $disabled, $standby) = @_;
$id_policy_alerts = 0 unless defined $id_policy_alerts;
$disabled = 0 unless defined $disabled;
$standby = 0 unless defined $standby;
#my $module_name = get_module_name($dbh, $id_agent_module);
#logger($pa_config, "Update alert of template '$id_alert_template' on agent module '$module_name'.", 10);
$dbh->do("UPDATE talert_template_modules SET `id_policy_alerts` = '$id_policy_alerts', `disabled` = '$disabled', `standby` = '$standby' WHERE id = $id_alert");
return $dbh->{'mysql_insertid'};
}
##########################################################################
=head2 C<< pandora_create_template_module(I<$pa_config>, I<$parameters>, I<$dbh>) >>
Create a template action.
=cut
##########################################################################
sub pandora_create_template_module_action ($$$) {
my ($pa_config, $parameters, $dbh) = @_;
logger($pa_config, "Creating module alert action to alert '$parameters->{'id_alert_template_module'}'.", 10);
my $action_id = db_process_insert($dbh, 'id', 'talert_template_module_actions', $parameters);
return $action_id;
}
##########################################################################
=head2 C<< pandora_delete_all_template_module_actions(I<$dbh>, I<$template_module_id>) >>
Delete all actions of policy template module.
=cut
##########################################################################
sub pandora_delete_all_template_module_actions ($$) {
my ($dbh, $template_module_id) = @_;
return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ?', $template_module_id);
}
########################################################################## ##########################################################################
=head2 C<< pandora_update_agent_address(I<$pa_config>, I<$agent_id>, I<$address>, I<$dbh>) >> =head2 C<< pandora_update_agent_address(I<$pa_config>, I<$agent_id>, I<$address>, I<$dbh>) >>

View File

@ -42,6 +42,7 @@ our @EXPORT = qw(
get_agent_id get_agent_id
get_agent_name get_agent_name
get_agent_module_id get_agent_module_id
get_alert_template_module_id
get_db_rows get_db_rows
get_db_single_row get_db_single_row
get_db_value get_db_value
@ -398,6 +399,16 @@ sub db_update ($$;@) {
return $rows; return $rows;
} }
##########################################################################
## Return alert template-module ID given the module and template ids.
##########################################################################
sub get_alert_template_module_id ($$$) {
my ($dbh, $id_module, $id_template) = @_;
my $rc = get_db_value ($dbh, "SELECT id FROM talert_template_modules WHERE id_agent_module = ? AND id_alert_template = ?", $id_module, $id_template);
return defined ($rc) ? $rc : -1;
}
########################################################################## ##########################################################################
## SQL insert. Returns the ID of the inserted row. ## SQL insert. Returns the ID of the inserted row.
########################################################################## ##########################################################################

View File

@ -174,41 +174,6 @@ sub pandora_manage_init ($) {
help_screen () if ($conf->{'pandora_path'} =~ m/--*h\w*\z/i ); help_screen () if ($conf->{'pandora_path'} =~ m/--*h\w*\z/i );
} }
##########################################################################
## Create a template module.
##########################################################################
sub pandora_create_template_module ($$$$;$$$) {
my ($pa_config, $id_agent_module, $id_alert_template, $dbh, $id_policy_alerts, $disabled, $standby) = @_;
$id_policy_alerts = 0 unless defined $id_policy_alerts;
$disabled = 0 unless defined $disabled;
$standby = 0 unless defined $standby;
my $module_name = get_module_name($dbh, $id_agent_module);
logger($pa_config, "Creating alert of template '$id_alert_template' on agent module '$module_name'.", 10);
$dbh->do("INSERT INTO talert_template_modules (`id_agent_module`, `id_alert_template`, `id_policy_alerts`, `disabled`, `standby`) VALUES ($id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby)");
return $dbh->{'mysql_insertid'};
}
##########################################################################
## Update a template module.
##########################################################################
sub pandora_update_template_module ($$$;$$$) {
my ($pa_config, $id_alert, $dbh, $id_policy_alerts, $disabled, $standby) = @_;
$id_policy_alerts = 0 unless defined $id_policy_alerts;
$disabled = 0 unless defined $disabled;
$standby = 0 unless defined $standby;
#my $module_name = get_module_name($dbh, $id_agent_module);
#logger($pa_config, "Update alert of template '$id_alert_template' on agent module '$module_name'.", 10);
$dbh->do("UPDATE talert_template_modules SET `id_policy_alerts` = '$id_policy_alerts', `disabled` = '$disabled', `standby` = '$standby' WHERE id = $id_alert");
return $dbh->{'mysql_insertid'};
}
########################################################################## ##########################################################################
## Delete a template module. ## Delete a template module.
########################################################################## ##########################################################################
@ -231,15 +196,6 @@ sub pandora_delete_template_module_action ($$$) {
return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ? AND id_alert_action = ?', $template_module_id, $action_id); return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ? AND id_alert_action = ?', $template_module_id, $action_id);
} }
##########################################################################
## Delete all actions of policy template module
##########################################################################
sub pandora_delete_all_template_module_actions ($$) {
my ($dbh, $template_module_id) = @_;
return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ?', $template_module_id);
}
########################################################################## ##########################################################################
## Create a user. ## Create a user.
########################################################################## ##########################################################################
@ -270,19 +226,6 @@ else {
} }
} }
##########################################################################
## Create a template action.
##########################################################################
sub pandora_create_template_module_action ($$$) {
my ($pa_config, $parameters, $dbh) = @_;
logger($pa_config, "Creating module alert action to alert '$parameters->{'id_alert_template_module'}'.", 10);
my $action_id = db_process_insert($dbh, 'id', 'talert_template_module_actions', $parameters);
return $action_id;
}
########################################################################## ##########################################################################
## Assign a profile to the given user/group. ## Assign a profile to the given user/group.
########################################################################## ##########################################################################
@ -405,16 +348,6 @@ sub pandora_validate_event_filter ($$$$$$$$$) {
db_do ($dbh, "UPDATE tevento SET estado = 1 WHERE estado = 0".$filter); db_do ($dbh, "UPDATE tevento SET estado = 1 WHERE estado = 0".$filter);
} }
##########################################################################
## Return alert template-module ID given the module and template ids.
##########################################################################
sub get_alert_template_module_id ($$$) {
my ($dbh, $id_module, $id_template) = @_;
my $rc = get_db_value ($dbh, "SELECT id FROM talert_template_modules WHERE id_agent_module = ? AND id_alert_template = ?", $id_module, $id_template);
return defined ($rc) ? $rc : -1;
}
############################################################################### ###############################################################################
############################################################################### ###############################################################################
# PRINT HELP AND CHECK ERRORS FUNCTIONS # PRINT HELP AND CHECK ERRORS FUNCTIONS
@ -1295,159 +1228,161 @@ sub pandora_manage_main ($$$) {
my $policy_id = enterprise_hook('get_policy_id',[$dbh, $policy_name]); my $policy_id = enterprise_hook('get_policy_id',[$dbh, $policy_name]);
exist_check($policy_id,'policy',$policy_name); exist_check($policy_id,'policy',$policy_name);
my $blocked_policies = enterprise_hook('pandora_block_policies', [$dbh]); enterprise_hook('pandora_apply_policy', [$dbh, $conf, $policy_id]);
if($blocked_policies eq '0E0') {
print "[ERROR] The policies are blocked in other terminal.\n\n";
exit;
}
# Get the agents #~ my $blocked_policies = enterprise_hook('pandora_block_policies', [$dbh]);
my $array_pointer_ag = enterprise_hook('get_policy_agents',[$dbh, $policy_id]); #~
#~ if($blocked_policies eq '0E0') {
if(!defined($array_pointer_ag)) { #~ print "[ERROR] The policies are blocked in other terminal.\n\n";
print "[ERROR] This option is not available in OPEN version.\n\n"; #~ exit;
exit; #~ }
} #~
#~ # Get the agents
print "[INFO] Applying policy '$policy_name'\n\n"; #~ my $array_pointer_ag = enterprise_hook('get_policy_agents',[$dbh, $policy_id]);
#~
foreach my $agent (@{$array_pointer_ag}) { #~ if(!defined($array_pointer_ag)) {
my $id_agent = $agent->{'id_agent'}; #~ print "[ERROR] This option is not available in OPEN version.\n\n";
my $agent_name = get_agent_name($dbh, $id_agent); #~ exit;
#~ }
# Get the modules #~
my $array_pointer_mod = enterprise_hook('get_policy_modules',[$dbh, $policy_id]); #~ print "[INFO] Applying policy '$policy_name'\n\n";
#~
if(!defined($array_pointer_mod)) { #~ foreach my $agent (@{$array_pointer_ag}) {
print "[ERROR] This option is not available in OPEN version.\n\n"; #~ my $id_agent = $agent->{'id_agent'};
exit; #~ my $agent_name = get_agent_name($dbh, $id_agent);
} #~
#~ # Get the modules
foreach my $module (@{$array_pointer_mod}) { #~ my $array_pointer_mod = enterprise_hook('get_policy_modules',[$dbh, $policy_id]);
# Adapt the fields from tpolicy_modules to tagente_modulos #~
$module->{'id_agente'} = $id_agent; #~ if(!defined($array_pointer_mod)) {
#~ print "[ERROR] This option is not available in OPEN version.\n\n";
$module->{'id_policy_module'} = $module->{'id'}; #~ exit;
delete $module->{'id'}; #~ }
#~
$module->{'descripcion'} = $module->{'description'}; #~ foreach my $module (@{$array_pointer_mod}) {
delete $module->{'description'}; #~ # Adapt the fields from tpolicy_modules to tagente_modulos
#~ $module->{'id_agente'} = $id_agent;
$module->{'nombre'} = $module->{'name'}; #~
delete $module->{'name'}; #~ $module->{'id_policy_module'} = $module->{'id'};
#~ delete $module->{'id'};
$module->{'id_modulo'} = $module->{'id_module'}; #~
delete $module->{'id_module'}; #~ $module->{'descripcion'} = $module->{'description'};
#~ delete $module->{'description'};
delete $module->{'id_policy'}; #~
#~ $module->{'nombre'} = $module->{'name'};
#Store the conf data #~ delete $module->{'name'};
$configuration_data .= safe_output("\n\n$module->{'configuration_data'}"); #~
#~ $module->{'id_modulo'} = $module->{'id_module'};
delete $module->{'configuration_data'}; #~ delete $module->{'id_module'};
#~
my $id_module = get_agent_module_id ($dbh, $module->{'nombre'}, $module->{'id_agente'}); #~ delete $module->{'id_policy'};
#~
# If the module doesn't exist we create it, otherwise we update it #~ #Store the conf data
#~ $configuration_data .= safe_output("\n\n$module->{'configuration_data'}");
if($id_module == -1) { #~
# Create module #~ delete $module->{'configuration_data'};
$id_module = pandora_create_module_from_hash ($conf, $module, $dbh); #~
} #~ my $id_module = get_agent_module_id ($dbh, $module->{'nombre'}, $module->{'id_agente'});
else { #~
# Update module #~ # If the module doesn't exist we create it, otherwise we update it
pandora_update_module_from_hash ($conf, $module, 'id_agente_modulo', $id_module, $dbh); #~
} #~ if($id_module == -1) {
#~ # Create module
# Get policy alerts and create it on created modules #~ $id_module = pandora_create_module_from_hash ($conf, $module, $dbh);
my $array_pointer_ale = enterprise_hook('get_policy_module_alerts',[$dbh, $policy_id, $module->{'id_policy_module'}]); #~ }
#~ else {
foreach my $alert (@{$array_pointer_ale}) { #~ # Update module
my $id_alert_template_module = get_alert_template_module_id($dbh, $id_module, $alert->{'id_alert_template'}); #~ pandora_update_module_from_hash ($conf, $module, 'id_agente_modulo', $id_module, $dbh);
#~ }
# Only if the template doesnt exist we create it. If exists we update it #~
if($id_alert_template_module == -1) { #~ # Get policy alerts and create it on created modules
$id_alert_template_module = pandora_create_template_module ($conf, $id_module, $alert->{'id_alert_template'}, $dbh, $alert->{'id'}, $alert->{'disabled'}, $alert->{'standby'}); #~ my $array_pointer_ale = enterprise_hook('get_policy_module_alerts',[$dbh, $policy_id, $module->{'id_policy_module'}]);
} #~
else { #~ foreach my $alert (@{$array_pointer_ale}) {
pandora_update_template_module ($conf, $id_alert_template_module, $dbh, $alert->{'id'}, $alert->{'disabled'}, $alert->{'standby'}); #~ my $id_alert_template_module = get_alert_template_module_id($dbh, $id_module, $alert->{'id_alert_template'});
} #~
#~ # Only if the template doesnt exist we create it. If exists we update it
# Get policy alert actions and create it on modules created #~ if($id_alert_template_module == -1) {
my $array_pointer_aleact = enterprise_hook('get_policy_alert_actions',[$dbh, $alert->{'id'}]); #~ $id_alert_template_module = pandora_create_template_module ($conf, $id_module, $alert->{'id_alert_template'}, $dbh, $alert->{'id'}, $alert->{'disabled'}, $alert->{'standby'});
#~ }
pandora_delete_all_template_module_actions ($dbh, $id_alert_template_module); #~ else {
#~ pandora_update_template_module ($conf, $id_alert_template_module, $dbh, $alert->{'id'}, $alert->{'disabled'}, $alert->{'standby'});
foreach my $alert_action (@{$array_pointer_aleact}) { #~ }
delete $alert_action->{'id_policy_alert'}; #~
delete $alert_action->{'id'}; #~ # Get policy alert actions and create it on modules created
$alert_action->{'id_alert_template_module'} = $id_alert_template_module; #~ my $array_pointer_aleact = enterprise_hook('get_policy_alert_actions',[$dbh, $alert->{'id'}]);
#~
pandora_create_template_module_action ($conf, $alert_action, $dbh); #~ pandora_delete_all_template_module_actions ($dbh, $id_alert_template_module);
#~
} #~ foreach my $alert_action (@{$array_pointer_aleact}) {
} #~ delete $alert_action->{'id_policy_alert'};
#~ delete $alert_action->{'id'};
#Add the conf information to the agent conf file #~ $alert_action->{'id_alert_template_module'} = $id_alert_template_module;
enterprise_hook('pandora_create_policy_conf_info',[$conf, $policy_name, $configuration_data, $agent_name, $dbh]); #~
#~ pandora_create_template_module_action ($conf, $alert_action, $dbh);
# Flag applyed the agent #~
enterprise_hook('pandora_apply_agent_policy',[$policy_id, $id_agent, $dbh]); #~ }
#~ }
} #~
#~ #Add the conf information to the agent conf file
# Get policy collections and link it on created modules #~ enterprise_hook('pandora_create_policy_conf_info',[$conf, $policy_name, $configuration_data, $agent_name, $dbh]);
my $array_pointer_col = enterprise_hook('get_policy_collections',[$dbh, $policy_id]); #~
#~ # Flag applyed the agent
my $collection_data = ''; #~ enterprise_hook('pandora_apply_agent_policy',[$policy_id, $id_agent, $dbh]);
#~
foreach my $collection (@{$array_pointer_col}) { #~ }
my $collection_name = enterprise_hook('get_collection_name',[$dbh, $collection->{'id_collection'}]); #~
#~ # Get policy collections and link it on created modules
$collection_data = "\n#file_collection $collection_name\n"; #~ my $array_pointer_col = enterprise_hook('get_policy_collections',[$dbh, $policy_id]);
$collection_data .= "\nfile_collection fc_$collection->{'id_collection'}\n\n"; #~
} #~ my $collection_data = '';
#~
if($collection_data ne '') { #~ foreach my $collection (@{$array_pointer_col}) {
enterprise_hook('pandora_delete_collection_agent_from_info',[$conf, $agent_name, $policy_id]); #~ my $collection_name = enterprise_hook('get_collection_name',[$dbh, $collection->{'id_collection'}]);
enterprise_hook('pandora_create_collection_conf_info',[$conf, $policy_name, $collection_data,$agent_name,$dbh]); #~
} #~ $collection_data = "\n#file_collection $collection_name\n";
#~ $collection_data .= "\nfile_collection fc_$collection->{'id_collection'}\n\n";
$configuration_data = ''; #~ }
$collection_data = ''; #~
} #~ if($collection_data ne '') {
#~ enterprise_hook('pandora_delete_collection_agent_from_info',[$conf, $agent_name, $policy_id]);
# Get extern policy alerts and create it on modules #~ enterprise_hook('pandora_create_collection_conf_info',[$conf, $policy_name, $collection_data,$agent_name,$dbh]);
my $array_pointer_ale_ext = enterprise_hook('get_policy_module_alerts',[$dbh, $policy_id, 0]); #~ }
#~
foreach my $alert (@{$array_pointer_ale_ext}) { #~ $configuration_data = '';
#~ $collection_data = '';
my $array_modules_id = enterprise_hook('get_policy_agents_modules_id',[$dbh, $policy_id, $alert->{'name_extern_module'}]); #~ }
#~
foreach my $module_id (@{$array_modules_id}) { #~ # Get extern policy alerts and create it on modules
my $id_alert_template_module = get_alert_template_module_id($dbh, $module_id->{'id_agente_modulo'}, $alert->{'id_alert_template'}); #~ my $array_pointer_ale_ext = enterprise_hook('get_policy_module_alerts',[$dbh, $policy_id, 0]);
#~
if($id_alert_template_module == -1) { #~ foreach my $alert (@{$array_pointer_ale_ext}) {
$id_alert_template_module = pandora_create_template_module ($conf, $module_id->{'id_agente_modulo'}, $alert->{'id_alert_template'}, $dbh, $alert->{'id'}); #~
} #~ my $array_modules_id = enterprise_hook('get_policy_agents_modules_id',[$dbh, $policy_id, $alert->{'name_extern_module'}]);
#~
# Get policy alert actions and create it on modules created #~ foreach my $module_id (@{$array_modules_id}) {
my $array_pointer_aleact = enterprise_hook('get_policy_alert_actions',[$dbh, $alert->{'id'}]); #~ my $id_alert_template_module = get_alert_template_module_id($dbh, $module_id->{'id_agente_modulo'}, $alert->{'id_alert_template'});
#~
pandora_delete_all_template_module_actions ($dbh, $id_alert_template_module); #~ if($id_alert_template_module == -1) {
#~ $id_alert_template_module = pandora_create_template_module ($conf, $module_id->{'id_agente_modulo'}, $alert->{'id_alert_template'}, $dbh, $alert->{'id'});
foreach my $alert_action (@{$array_pointer_aleact}) { #~ }
delete $alert_action->{'id_policy_alert'}; #~
delete $alert_action->{'id'}; #~ # Get policy alert actions and create it on modules created
#~ my $array_pointer_aleact = enterprise_hook('get_policy_alert_actions',[$dbh, $alert->{'id'}]);
$alert_action->{'id_alert_template_module'} = $id_alert_template_module; #~
#~ pandora_delete_all_template_module_actions ($dbh, $id_alert_template_module);
pandora_create_template_module_action ($conf, $alert_action, $dbh); #~
} #~ foreach my $alert_action (@{$array_pointer_aleact}) {
} #~ delete $alert_action->{'id_policy_alert'};
} #~ delete $alert_action->{'id'};
enterprise_hook('pandora_unblock_policies', [$dbh]); #~
#~ $alert_action->{'id_alert_template_module'} = $id_alert_template_module;
#~
#~ pandora_create_template_module_action ($conf, $alert_action, $dbh);
#~ }
#~ }
#~ }
#~ enterprise_hook('pandora_unblock_policies', [$dbh]);
} }
elsif ($param eq '--force_unblock_policies') { elsif ($param eq '--force_unblock_policies') {
enterprise_hook('pandora_unblock_policies', [$dbh]); enterprise_hook('pandora_unblock_policies', [$dbh]);