From 8bccd52b4d4a8829314bf6efc507522c637c6730 Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Fri, 13 Jan 2012 09:43:29 +0000 Subject: [PATCH] 2012-01-13 Sergio Martin * util/pandora_manage.pl: Added update_alert_template option git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5364 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 5 ++ pandora_server/util/pandora_manage.pl | 114 +++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index f90f7de5d3..adf2b65d6b 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,8 @@ +2012-01-13 Sergio Martin + + * util/pandora_manage.pl: Added update_alert_template + option + 2012-01-12 Sergio Martin * util/pandora_manage.pl: Added options to add diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 870f448374..2a612656b1 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -206,6 +206,23 @@ else { } } +########################################################################## +## Delete an alert template. +########################################################################## +sub pandora_delete_alert_template ($$) { +my ($dbh, $template_name) = @_; + +# Delete the alert_template +my $return = db_do ($dbh, 'DELETE FROM talert_templates WHERE name = ?', safe_input($template_name)); + +if($return eq '0E0') { + return -1; +} +else { + return 0; +} +} + ########################################################################## ## Assign a profile to the given user/group. ########################################################################## @@ -338,6 +355,16 @@ sub pandora_update_user_from_hash ($$$$) { return $user_id; } +########################################################################## +## Update an alert template from hash +########################################################################## +sub pandora_update_alert_template_from_hash ($$$$) { + my ($parameters, $where_column, $where_value, $dbh) = @_; + + my $template_id = db_process_update($dbh, 'talert_templates', $parameters, $where_column, $where_value); + return $template_id; +} + ############################################################################### # Get list of all downed agents ############################################################################### @@ -491,6 +518,8 @@ sub help_screen{ help_screen_line('--create_policy_snmp_module', ' [ ]', 'Add snmp network module to policy'); help_screen_line('--create_policy_plugin_module', ' [ ]', 'Add plug-in module to policy'); help_screen_line('--create_alert_template', ' [ ]', 'Create alert template'); + help_screen_line('--delete_alert_template', '', 'Delete alert template'); + help_screen_line('--update_alert_template', ' ', 'Update a field of an alert template'); print "\n"; exit; @@ -662,7 +691,6 @@ sub cli_create_alert_template() { $parameters{'id_alert_action'} = $id_alert_action unless $id_alert_action <= 0; $parameters{'id_group'} = $group_id; - $parameters{'priority'} = defined ($priority) ? $priority : ''; $parameters{'field1'} = defined ($field1) ? safe_input($field1) : ''; $parameters{'field2'} = defined ($field2) ? safe_input($field2) : ''; $parameters{'field3'} = defined ($field3) ? safe_input($field3) : ''; @@ -1471,6 +1499,68 @@ sub cli_user_update() { pandora_update_user_from_hash ($update, 'id_user', safe_input($user_id), $dbh); } +############################################################################## +# Update an alert template. +# Related option: --update_alert_template +############################################################################## + +sub cli_alert_template_update() { + my ($template_name,$field,$new_value) = @ARGV[2..4]; + + my $template_id = pandora_get_alert_template_id ($dbh, $template_name); + exist_check($template_id,'alert template',$template_name); + + if($field eq 'matches_value' || $field eq 'value' || $field eq 'min_value' || + $field eq 'max_value' || $field eq 'type' || $field eq 'time_threshold' || + $field eq 'time_from' || $field eq 'time_to' || $field eq 'monday' || + $field eq 'tuesday' || $field eq 'wednesday' || $field eq 'thursday' || + $field eq 'friday' || $field eq 'saturday' || $field eq 'sunday' || + $field eq 'min_alerts' || $field eq 'max_alerts' || $field eq 'recovery_notify') { + # Fields admited, no changes + } + elsif($field eq 'name' || $field eq 'description' || $field eq 'field1' || $field eq 'field2' || $field eq 'field3' || $field eq 'recovery_field2' || $field eq 'recovery_field3') { + $new_value = safe_input($new_value); + } + elsif($field eq 'priority') { + if($new_value < 0 || $new_value > 4) { + print "[ERROR] Field '$field' is out of interval (0-4)\n\n"; + exit; + } + } + elsif($field eq 'default_action') { + # Check if exist + my $id_alert_action = get_action_id ($dbh, safe_input($new_value)); + if($id_alert_action == -1) { + print "[ERROR] Alert action '$new_value' doesnt exist\n\n"; + exit; + } + $new_value = $id_alert_action; + $field = 'id_alert_action'; + } + elsif($field eq 'group_name') { + # Check if exist + my $id_group = get_group_id($dbh, $new_value); + if($id_group == -1) { + print "[ERROR] Group '$new_value' doesnt exist\n\n"; + exit; + } + $new_value = $id_group; + $field = 'id_group'; + } + else { + print "[ERROR] Field '$field' doesnt exist\n\n"; + exit; + } + + print "[INFO] Updating field '$field' in user '$template_name'\n\n"; + + my $update; + + $update->{$field} = $new_value; + + pandora_update_alert_template_from_hash ($update, 'id', $template_id, $dbh); +} + ############################################################################## # Add a profile to a User in a Group # Related option: --add_profile_to_user @@ -1515,6 +1605,20 @@ sub cli_delete_user() { exist_check($result,'user',$user_name); } +############################################################################## +# Delete an alert_template. +# Related option: --delete_user +############################################################################## + +sub cli_delete_alert_template() { + my $template_name = @ARGV[2]; + + print "[INFO] Deleting template '$template_name' \n\n"; + + my $result = pandora_delete_alert_template($dbh,$template_name); + exist_check($result,'alert template',$template_name); +} + ############################################################################## # Create profile. # Related option: --create_profile @@ -2173,6 +2277,14 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 19, 15); cli_create_alert_template(); } + elsif ($param eq '--delete_alert_template') { + param_check($ltotal, 1); + cli_delete_alert_template(); + } + elsif ($param eq '--update_alert_template') { + param_check($ltotal, 3); + cli_alert_template_update(); + } else { print "[ERROR] Invalid option '$param'.\n\n"; $param = '';