From 0cb5d0bc62b0fffe27dbf27e3717ff892bcfa94d Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Tue, 17 Jan 2012 09:30:52 +0000 Subject: [PATCH] 2012-01-17 Sergio Martin * util/pandora_manage.pl: Added the option exec_from_file to import CSV data. Fixed create_user option entities git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5375 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 6 +++ pandora_server/util/pandora_manage.pl | 72 ++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index ecbfed7bc7..55a0d7d17a 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,9 @@ +2012-01-17 Sergio Martin + + * util/pandora_manage.pl: Added the option + exec_from_file to import CSV data. Fixed create_user + option entities + 2012-01-16 Sergio Martin * lib/PandoraFMS/DB.pm: Added a missed safe input in diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index e553183b74..6cbb37e219 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -125,6 +125,7 @@ sub help_screen{ help_screen_line('--delete_alert_template', '', 'Delete alert template'); help_screen_line('--update_alert_template', ' ', 'Update a field of an alert template'); help_screen_line('--update_module', ' ', 'Update a module field'); + help_screen_line('--exec_from_file', ' ', 'Execute any CLI option with macros from CSV file'); print "\n"; exit; @@ -239,7 +240,7 @@ my ($dbh, $name, $password, $is_admin, $comments) = @_; return db_insert ($dbh, 'id_user', 'INSERT INTO tusuario (id_user, fullname, password, comments, is_admin) - VALUES (?, ?, ?, ?, ?)', $name, $name, $password, $comments, $is_admin); + VALUES (?, ?, ?, ?, ?)', safe_input($name), safe_input($name), $password, safe_input($comments), $is_admin); } ########################################################################## @@ -1456,6 +1457,9 @@ sub cli_create_user() { $comments = (defined ($comments) ? safe_input($comments) : '' ); + my $user_exists = get_user_exists ($dbh, $user_name); + non_exist_check($user_exists,'user',$user_name); + print "[INFO] Creating user '$user_name'\n\n"; pandora_create_user ($dbh, $user_name, md5($password), $is_admin, $comments); @@ -1855,6 +1859,67 @@ sub cli_module_update() { pandora_update_module_from_hash ($conf, $update, 'id_agente_modulo', $id_agent_module, $dbh); } +############################################################################## +# Exec a CLI option from file +# Related option: --exec_from_file +############################################################################## + +sub cli_exec_from_file() { + my $c = 0; + my $command = $0; + my $file; + foreach my $opt (@ARGV) { + $c++; + + # First and second are the script and conf paths + if($c < 2) { + $command = "$command $opt"; + } + # Third param is ignored, because is --exec_from_file + # Fourth param is the file path + elsif($c == 3) { + $file = $opt; + if(!(-e $file)) { + print "[ERROR] File '$file' not exists or cannot be opened\n\n"; + exit; + } + } + # Fifth parameter is the option (we add -- before it) + elsif($c == 4) { + $command = "$command --$opt"; + } + # Next parameters are the option params, we add quotes to them to avoid errors + elsif($c > 4) { + if($opt =~ m/\s/g) { + $command = $command . ' "' . $opt .'"'; + } + else { + $command = $command . ' ' . $opt; + } + } + } + + open (FILE, $file); + while () { + my $command_tr = $command; + chomp; + my @fields = split(',',$_); + $c = 0; + foreach my $field (@fields) { + $c++; + my $macro_name = "__FIELD".$c."__"; + if($field =~ m/\s/g && $field !~ m/^"/) { + $field = '"'.$field.'"'; + } + $command_tr !~ s/$macro_name/$field/g; + } + print `./$command_tr`; + } + close (FILE); + + exit; +} + ############################################################################## # Return the type of given module (data, network, snmp or plugin) ############################################################################## @@ -2233,7 +2298,7 @@ sub cli_policy_add_agent() { print "[ERROR] A problem has been ocurred adding agent '$agent_name' to policy '$policy_name'\n\n"; } else { - print "[INFO] Added agent '$agent_name' to policy '$policy_name'. Is necessary apply the policy to changes take effect.\n\n"; + print "[INFO] Added agent '$agent_name' to policy '$policy_name'. Is necessary to apply the policy in order to changes take effect.\n\n"; } } @@ -2618,6 +2683,9 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 4); cli_module_update(); } + elsif ($param eq '--exec_from_file') { + cli_exec_from_file(); + } else { print "[ERROR] Invalid option '$param'.\n\n"; $param = '';