2010-11-04 Sergio Martin <sergio.martin@artica.es>

* lib/PandoraFMS/DB.pm
	lib/PandoraFMS/Core.pm
	util/pandora_manage.pl: Improved the policies
	appliement with checks and the external
	alerts addition



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3522 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
zarzuelo 2010-11-04 17:35:37 +00:00
parent f0f2d1be41
commit 195fc478ed
4 changed files with 152 additions and 27 deletions

View File

@ -1,3 +1,11 @@
2010-11-04 Sergio Martin <sergio.martin@artica.es>
* lib/PandoraFMS/DB.pm
lib/PandoraFMS/Core.pm
util/pandora_manage.pl: Improved the policies
appliement with checks and the external
alerts addition
2010-11-03 Ramon Novoa <rnovoa@artica.es>
* lib/PandoraFMS/Tools.pm: limpia_cadena removes unwanted

View File

@ -149,6 +149,7 @@ our @EXPORT = qw(
pandora_server_keep_alive
pandora_update_agent
pandora_update_module_on_error
pandora_update_module_from_hash
pandora_update_server
pandora_group_statistics
pandora_server_statistics
@ -1096,7 +1097,7 @@ sub pandora_create_module ($$$$$$$$$$) {
}
##########################################################################
## Create an agent module
## Create an agent module from hash
##########################################################################
sub pandora_create_module_from_hash ($$$) {
my ($pa_config, $parameters, $dbh) = @_;
@ -1110,6 +1111,19 @@ sub pandora_create_module_from_hash ($$$) {
return $module_id;
}
##########################################################################
## Update an agent module from hash
##########################################################################
sub pandora_update_module_from_hash ($$$$$) {
my ($pa_config, $parameters, $where_column, $where_value, $dbh) = @_;
logger($pa_config, "Updating module '$parameters->{'nombre'}' for agent ID $parameters->{'id_agente'}.", 10);
my $module_id = db_process_update($dbh, 'tagente_modulo', $parameters, $where_column, $where_value);
return $module_id;
}
##########################################################################
=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>]) >>

View File

@ -32,6 +32,7 @@ our @EXPORT = qw(
db_disconnect
db_do
db_process_insert
db_process_update
db_insert
db_update
get_action_id
@ -359,20 +360,56 @@ sub db_process_insert($$$;@) {
exit;
}
my $columns_string = join(',',@columns_array);
# Generate the '?' simbols to the Query like '(?,?,?,?,?)'
my $wildcards = '';
for (my $i=0; $i<=$#values_array; $i++) {
if(!defined($values_array[$i])) {
$values_array[$i] = '';
}
if($i > 0 && $i <= $#values_array) {
$wildcards = $wildcards.',';
}
$wildcards = $wildcards.'?';
}
$wildcards = '('.$wildcards.')';
my $columns_string = join(',',@columns_array);
my $res = db_insert ($dbh, "INSERT INTO $table (".$columns_string.") VALUES ".$wildcards, @values_array);
return $res;
}
##########################################################################
## SQL update.
##########################################################################
sub db_process_update($$$$$;@) {
my ($dbh, $table, $parameters, $where_column, $where_value, @values) = @_;
my @columns_array = keys %$parameters;
my @values_array = values %$parameters;
if(!defined($table) || $#columns_array == -1) {
return -1;
exit;
}
my $values_string = "'".join("','",@values_array)."'";
my $fields = '';
for (my $i=0; $i<=$#values_array; $i++) {
if(!defined($values_array[$i])) {
$values_array[$i] = '';
}
if($i > 0 && $i <= $#values_array) {
$fields = $fields.',';
}
$fields = $fields." $columns_array[$i] = ?";
}
my $query = "INSERT INTO $table ($columns_string) VALUES ($values_string)";
push(@values_array, $where_value);
$dbh->do($query, undef, @values);
return $dbh->{'mysql_insertid'};
my $res = db_update ($dbh, "UPDATE $table SET$fields WHERE $where_column = ?", @values_array);
return $res;
}
##########################################################################

View File

@ -232,13 +232,15 @@ sub pandora_delete_agent ($$$) {
##########################################################################
## Create a template module.
##########################################################################
sub pandora_create_template_module ($$$$) {
my ($pa_config, $id_agent_module, $id_alert_template, $dbh) = @_;
sub pandora_create_template_module ($$$$;$) {
my ($pa_config, $id_agent_module, $id_alert_template, $dbh, $id_policy_alerts) = @_;
$id_policy_alerts = 0 unless defined $id_policy_alerts;
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`) VALUES ($id_agent_module, $id_alert_template)");
$dbh->do("INSERT INTO talert_template_modules (`id_agent_module`, `id_alert_template`, `id_policy_alerts`) VALUES ($id_agent_module, $id_alert_template, $id_policy_alerts)");
return $dbh->{'mysql_insertid'};
}
@ -256,17 +258,23 @@ sub pandora_delete_template_module ($$) {
}
##########################################################################
## Asociate a module to a template.
## Delete a policy template module action.
##########################################################################
sub pandora_delete_template_module_action ($$$) {
my ($dbh, $module_id, $template_id, $action_id) = @_;
my $template_module_id = get_template_module_id ($dbh, $module_id, $template_id);
return -1 if ($template_module_id == -1);
my ($dbh, $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.
##########################################################################
@ -298,7 +306,7 @@ else {
}
##########################################################################
## Asociate a module to a template.
## Create a template action.
##########################################################################
sub pandora_create_template_module_action ($$$) {
my ($pa_config, $parameters, $dbh) = @_;
@ -432,6 +440,15 @@ sub pandora_validate_event_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;
}
###############################################################################
###############################################################################
@ -1364,25 +1381,42 @@ sub pandora_manage_main ($$$) {
delete $module->{'configuration_data'};
my $id_module = get_agent_module_id ($dbh, $module->{'nombre'}, $module->{'id_agente'});
# If the module doesn't exist we create it, otherwise we update it
if($id_module == -1) {
# Create module
my $id_module = pandora_create_module_from_hash ($conf, $module, $dbh);
$id_module = pandora_create_module_from_hash ($conf, $module, $dbh);
}
else {
# Update module
pandora_update_module_from_hash ($conf, $module, 'id_agente_modulo', $id_module, $dbh);
}
# Get policy alerts and create it on created modules
my $array_pointer_ale = enterprise_hook('get_policy_module_alerts',[$dbh, $policy_id, $module->{'id_policy_module'}]);
foreach my $alert (@{$array_pointer_ale}) {
my $id_alert_template_module = pandora_create_template_module ($conf, $id_module, $alert->{'id_alert_template'}, $dbh);
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($id_alert_template_module == -1) {
$id_alert_template_module = pandora_create_template_module ($conf, $id_module, $alert->{'id_alert_template'}, $dbh, $alert->{'id'});
}
# Get policy alert actions and create it on modules created
my $array_pointer_aleact = enterprise_hook('get_policy_alert_actions',[$dbh, $alert->{'id'}]);
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'};
$alert_action->{'id_alert_template_module'} = $id_alert_template_module;
pandora_create_template_module_action ($conf, $alert_action, $dbh);
}
}
@ -1391,6 +1425,7 @@ sub pandora_manage_main ($$$) {
# Flag applyed the agent
enterprise_hook('pandora_apply_agent_policy',[$policy_id, $id_agent, $dbh]);
}
# Get policy collections and link it on created modules
@ -1406,9 +1441,40 @@ sub pandora_manage_main ($$$) {
}
if($collection_data ne '') {
enterprise_hook('pandora_delete_collection_agent_from_info',[$conf, $agent_name, $policy_id]);
enterprise_hook('pandora_create_collection_conf_info',[$conf, $policy_name, $collection_data,$agent_name,$dbh]);
}
}
# Get extern policy alerts and create it on modules
my $array_pointer_ale_ext = enterprise_hook('get_policy_module_alerts',[$dbh, $policy_id, 0]);
foreach my $alert (@{$array_pointer_ale_ext}) {
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}) {
my $id_alert_template_module = get_alert_template_module_id($dbh, $module_id->{'id_agente_modulo'}, $alert->{'id_alert_template'});
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'});
}
# Get policy alert actions and create it on modules created
my $array_pointer_aleact = enterprise_hook('get_policy_alert_actions',[$dbh, $alert->{'id'}]);
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'};
$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') {