From 1a16f4e4d7b559af096a5b02042c81f137178eec Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Mon, 20 Sep 2010 14:25:44 +0000 Subject: [PATCH] 2010-09-20 Sergio Martin * util/pandora_manage.pl: Added to CLI the deletion of all modules without policy from config file git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3259 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 5 + pandora_server/util/pandora_manage.pl | 135 ++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index f77bb03cb9..9b9c46d65f 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,8 @@ +2010-09-20 Sergio Martin + + * util/pandora_manage.pl: Added to CLI the deletion + of all modules without policy from config file + 2010-09-20 Sergio Martin * util/pandora_manage.pl: Added to CLI the deletion diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index f8124f9ddb..0deac09466 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -373,6 +373,127 @@ sub pandora_delete_module_data ($$) { return 1; } +########################################################################## +## Delete a module from conf file +########################################################################## +sub pandora_delete_module_from_conf ($$) { + my ($conf_file, $module_name) = @_; + + my $found = 0; + my $skip = 0; + my $new_txt = ""; + + # $found = 0 means the search to the first module and the search between modules + # $found = 1 means found the module_begin and wait for the module_name + # $found = 2 means found the module_name required and search to the end of this module + + open (FILE, $conf_file); + +# Alternative way with regular expresion + +# my @txt_array = ; +# my $txt = join ('', @txt_array); + +# $txt =~ s/module_begin(\s)*(\r\n|\n)module_name $module_name(.|\r\n|\n)*?module_end([^\n])*//g; + + while () { + my ($line) = split("\t"); + + if(($found == 1) && ($line =~ 'module_name '.$module_name)) { + $skip = 1; + $found = 2; + } + elsif($found == 1) { + $found = 0; + } + + if(($found == 0) && ($line =~ 'module_begin')) { + $found = 1; + } + + if(($found == 2) && ($line =~ 'module_end')){ + $skip = 1; + $found = -1; + } + + if($found == 2) { + $skip = 1; + } + + if($skip == 0) { + $new_txt = $new_txt . $line; + } + + $skip = 0; + } + + close(FILE); + + open FILE, "> ".$conf_file; + print FILE "$new_txt"; + + close(FILE); + + pandora_clean_blank_lines_conf($conf_file); +} + +########################################################################## +## Delete all modules without policy from conf +########################################################################## +sub pandora_delete_not_policy_modules ($) { + my $conf_file = shift; + + my $found = 0; + my $skip = 0; + my $new_txt = ""; + + # $found = 0 means the search to the first module and the search between modules + # $found = 1 means found the module_begin and wait for the module_name + # $found = -1 means found a policy tag and wait for the end of policy tags + + open (FILE, $conf_file); + while () { + my ($line) = split("\t"); + + if($found == 1) { + $skip = 1; + } + + if(($found == 1) && ($line =~ 'module_end')){ + $found = 0; + } + + if(($found == -1) && ($line =~ '#END')) { + $found = 0; + } + + if(($found == 0) && ($line =~ '#INI')) { + $found = -1; + $skip = 0; + } + + if(($found == 0) && ($line =~ 'module_begin')) { + $skip = 1; + $found = 1; + } + + if($skip == 0) { + $new_txt = $new_txt . $line; + } + + $skip = 0; + } + + close(FILE); + + open FILE, "> ".$conf_file; + print FILE "$new_txt"; + + close(FILE); + + pandora_clean_blank_lines_conf($conf_file); +} + ########################################################################## ## Delete a module from conf file ########################################################################## @@ -577,6 +698,7 @@ sub help_screen{ help_screen_line('--validate_event', ' ', 'Validate events'); help_screen_line('--create_incident', ' <description> <origin> <status> <priority 0 for Informative, 1 for Low, 2 for Medium, 3 for Serious, 4 for Very serious or 5 for Maintenance> <group> [<owner>]', 'Create incidents'); help_screen_line('--delete_data', '-m <module_name> <agent_name> | -a <agent_name> | -g <group_name>', 'Delete historic data of a module, the modules of an agent or the modules of the agents of a group'); + help_screen_line('--delete_not_policy_modules', 'Delete all modules without policy from configuration file', '<agent_conf_file>'); print "\n"; exit; } @@ -875,6 +997,13 @@ sub pandora_manage_main ($$$) { } } + elsif ($param =~ m/--delete_not_policy_modules/i) { + param_check($ltotal, 1); + my $conf_file = @ARGV[2]; + + print "[INFO] Deleting modules without policy from conf file \n\n"; + pandora_delete_not_policy_modules($conf_file); + } elsif ($param =~ m/--create_template_module/i) { param_check($ltotal, 3); my ($template_name,$module_name,$agent_name) = @ARGV[2..4]; @@ -1184,6 +1313,12 @@ sub pandora_manage_main ($$$) { exit; } } + else { + print "[ERROR] Invalid option '$param'.\n\n"; + $param = ''; + help_screen (); + exit; + } } print "[W] Nothing to do. Exiting !\n\n";