From f68ff5749070aa649f65561e02ed29794ff8e698 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Mon, 29 Jun 2015 18:45:15 +0200 Subject: [PATCH] New feature: bulk operations with plugins. Ticket #2301. --- .../godmode/massive/massive_edit_plugins.php | 990 ++++++++++++++++++ .../godmode/massive/massive_operations.php | 73 +- pandora_console/godmode/menu.php | 1 + pandora_console/godmode/servers/plugin.php | 217 ++-- 4 files changed, 1182 insertions(+), 99 deletions(-) create mode 100644 pandora_console/godmode/massive/massive_edit_plugins.php diff --git a/pandora_console/godmode/massive/massive_edit_plugins.php b/pandora_console/godmode/massive/massive_edit_plugins.php new file mode 100644 index 0000000000..e25d9ff53e --- /dev/null +++ b/pandora_console/godmode/massive/massive_edit_plugins.php @@ -0,0 +1,990 @@ +", $plugin['description']); + } + if (isset($plugin['macros'])) { + $macros = json_decode($plugin['macros'], true); + if (!empty($macros)) { + $macros = array_values($macros); + + if (!empty($macros)) { + $plugin['macros'] = $macros; + } + } + } + + echo json_encode($plugin); + return; + } + + if ($get_agents) { + $sql = "SELECT ta.id_agente, ta.nombre AS agent_name, + tam.nombre AS module_name + FROM tagente ta + INNER JOIN tagente_modulo tam + ON ta.id_agente = tam.id_agente + AND tam.id_plugin = $plugin_id + ORDER BY ta.nombre, tam.nombre"; + $result = db_get_all_rows_sql($sql); + if (empty($result)) $result = array(); + + $agents = array(); + + $current_element = array(); + foreach ($result as $key => $value) { + $id = (int) $value['id_agente']; + $name = $value['agent_name']; + $module_name = $value['module_name']; + + if (!empty($current_element) && $current_element['id'] !== $id) { + $agents[] = $current_element; + $current_element = array(); + } + + $current_element['id'] = $id; + $current_element['name'] = $name; + + if (!isset($current_element['module_names'])) + $current_element['module_names'] = array(); + $current_element['module_names'][] = $module_name; + } + if (!empty($current_element)) { + $agents[] = $current_element; + } + + echo json_encode($agents); + return; + } + + if ($get_module_plugin_macros) { + $fields = array('macros'); + $filter = array( + 'id_plugin' => $plugin_id, + 'id_agente' => $agent_ids, + 'nombre' => $module_names + ); + $module_plugin_macros = db_get_all_rows_filter('tagente_modulo', $filter, $fields); + if (empty($module_plugin_macros)) $module_plugin_macros = array(); + + $module_plugin_macros = array_reduce($module_plugin_macros, function($carry, $item) { + + $macros = json_decode($item['macros'], true); + if (!empty($macros)) { + $macros = array_values($macros); + if (!empty($macros)) { + $carry[] = $macros; + } + } + + return $carry; + + }, array()); + + echo json_encode($module_plugin_macros); + return; + } + + return; +} + +$update = (bool) get_parameter('update'); + +if ($update) { + try { + $plugin = db_get_row('tplugin', 'id', $plugin_id); + // Macros retrieved from the plugin definition + $plugin_macros = array(); + if (isset($plugin['macros'])) { + $plugin_macros = json_decode($plugin['macros'], true); + if (!empty($plugin_macros)) { + $plugin_macros = array_values($plugin_macros); + } + } + + // Error + if (empty($plugin_macros)) + throw new Exception(__('Error retrieving the plugin macros')); + + // Macros returned by the form + $macros = get_parameter('macros', array()); + + // Error + if (empty($macros)) + throw new Exception(__('Error retrieving the modified macros')); + + $fields = array('id_agente_modulo', 'macros'); + $filter = array( + 'id_plugin' => $plugin_id, + 'id_agente' => $agent_ids, + 'nombre' => $module_names + ); + $module_plugin_macros = db_get_all_rows_filter('tagente_modulo', $filter, $fields); + if (empty($module_plugin_macros)) $module_plugin_macros = array(); + + // Error + if (empty($module_plugin_macros)) + throw new Exception(__('Error retrieving the module plugin macros')); + + // Begin transaction + // db_process_sql_begin(); + $errors = 0; + $count = 0; + + foreach ($module_plugin_macros as $item) { + + $module_id = $item['id_agente_modulo']; + $module_macros_str = $item['macros']; + // Macros retrieved from the agent module + $module_macros = json_decode($module_macros_str, true); + + // Error + if (empty($module_macros)) + throw new Exception(__('Error retrieving the module plugin macros data')); + + // Get the new module plugin macros + $result_macros = array_map(function ($item) use ($macros, $module_macros) { + + $result = array( + 'macro' => $item['macro'], + 'desc' => $item['desc'], + 'help' => $item['help'], + 'hide' => $item['hide'] + ); + + // Get the default value os the module plugin macro + $default = array_reduce($module_macros, function ($carry, $module_macro) use ($result) { + + if (isset($module_macro['macro']) && $module_macro['macro'] == $result['macro']) { + $carry = $module_macro['value']; + } + + return $carry; + + }, ''); + + set_if_defined($result['value'], $macros[$item['macro']]); + set_unless_defined($result['value'], $default); + + return $result; + + }, $plugin_macros); + + // Error + if (empty($result_macros)) + throw new Exception(__('Error building the new macros')); + + $module_macros = json_encode($result_macros); + if (empty($module_macros)) { + $module_macros = $module_macros_str; + } + + $values = array('macros' => $module_macros); + $where = array('id_agente_modulo' => $module_id); + // $result = db_process_sql_update('tagente_modulo', $values, $where, 'AND', false); + $result = db_process_sql_update('tagente_modulo', $values, $where); + + if (!$result) + $errors++; + else + $count += $result; + + } + + // if (!$errors) { + // db_process_sql_commit(); + // } + // else { + // db_process_sql_rollback(); + // } + + // Result message + ui_print_info_message(sprintf(__('%d modules updated'), $count)); + } + catch (Exception $e) { + ui_print_error_message($e->getMessage()); + } +} + +$table = new StdClass(); +$table->id = 'massive_plugin_edition'; +$table->width = '100%'; +$table->rowstyle = array(); +$table->data = array(); + +// Plugins +$filter = array('order' => 'name'); +$fields = array('id', 'name'); +$plugins = db_get_all_rows_filter('tplugin', $filter, $fields); + +if (empty($plugins)) { + ui_print_empty_data(__('There are not registered plugins')); + return; +} + +$plugins_aux = array(); +foreach ($plugins as $plugin) { + $plugins_aux[$plugin['id']] = $plugin['name']; +} +$plugins = $plugins_aux; +unset($plugins_aux); + +$plugins_select = html_print_select ($plugins, 'plugin_id', + $plugin_id, '', __('None'), 0, true, false, false); + +$row = array(); +$row[] = '' . __('Plugin') . ''; +$row[] = $plugins_select; + +$table->data['plugin-ids-row'] = $row; + +// Agents & modules +$row = array(); + +// Agents +$agents_select = html_print_select ($agent_ids, 'agent_ids[]', + false, '', '', 0, true, true, false); + +$row[] = '' . __('Agents') . ''; +$row[] = $agents_select; + +// Modules +// $modules_select = html_print_select ($module_ids, 'module_ids', +// false, '', '', 0, true, true, false); +$modules_select = html_print_select ($module_names, 'module_names[]', + false, '', '', 0, true, true, false); + +$row[] = '' . _('Modules') . ''; +$row[] = $modules_select; + +$table->rowstyle['agents-modules-row'] = 'vertical-align: top; display: none'; +$table->data['agents-modules-row'] = $row; + +echo '
'; + +html_print_table($table); + +echo "
"; +html_print_input_hidden('update', 1); +html_print_submit_button (__('Update'), 'upd-btn', false, 'class="sub upd"'); +echo "
"; + +echo '
'; + +?> + + diff --git a/pandora_console/godmode/massive/massive_operations.php b/pandora_console/godmode/massive/massive_operations.php index 923b2fa9cd..67faa3e41a 100755 --- a/pandora_console/godmode/massive/massive_operations.php +++ b/pandora_console/godmode/massive/massive_operations.php @@ -58,6 +58,10 @@ $options_modules = array( 'edit_modules' => __('Bulk module edit'), 'copy_modules' => __('Bulk module copy')); +$options_plugins = array( + 'edit_plugins' => __('Bulk plugin edit') + ); + if (! check_acl ($config['id_user'], 0, "PM")) { unset($options_modules['edit_modules']); } @@ -107,6 +111,9 @@ elseif (in_array($option, array_keys($options_snmp))) { elseif (in_array($option, array_keys($options_satellite))) { $tab = 'massive_satellite'; } +elseif (in_array($option, array_keys($options_plugins))) { + $tab = 'massive_plugins'; +} else { $option = ''; } @@ -133,6 +140,9 @@ switch ($tab) { case 'massive_satellite': $options = $options_satellite; break; + case 'massive_plugins': + $options = $options_plugins; + break; } // Set the default option of the category @@ -140,35 +150,30 @@ if ($option == '') { $option = array_shift(array_keys($options)); } -$alertstab = array( - 'text' => '' - . html_print_image ('images/op_alerts.png', true, - array ('title' => __('Alerts operations'))) - . '', - 'active' => $tab == 'massive_alerts'); +$alertstab = array('text' => '' + . html_print_image ('images/op_alerts.png', true, + array ('title' => __('Alerts operations'))) + . '', 'active' => $tab == 'massive_alerts'); -$userstab = array( - 'text' => '' - . html_print_image ('images/op_workspace.png', true, - array ('title' => __('Users operations'))) - . '', - 'active' => $tab == 'massive_users'); +$userstab = array('text' => '' + . html_print_image ('images/op_workspace.png', true, + array ('title' => __('Users operations'))) + . '', 'active' => $tab == 'massive_users'); -$agentstab = array( - 'text' => '' - . html_print_image ('images/bricks.png', true, - array ('title' => __('Agents operations'))) - . '', - 'active' => $tab == 'massive_agents'); - -$modulestab = array( - 'text' => '' - . html_print_image ('images/brick.png', true, - array ('title' => __('Modules operations'))) - . '', - 'active' => $tab == 'massive_modules'); +$agentstab = array('text' => '' + . html_print_image ('images/bricks.png', true, + array ('title' => __('Agents operations'))) + . '', 'active' => $tab == 'massive_agents'); +$modulestab = array('text' => '' + . html_print_image ('images/brick.png', true, + array ('title' => __('Modules operations'))) + . '', 'active' => $tab == 'massive_modules'); +$pluginstab = array('text' => '' + . html_print_image ('images/plugin.png', true, + array ('title' => __('Plugins operations'))) + . '', 'active' => $tab == 'massive_plugins'); $policiestab = enterprise_hook('massive_policies_tab'); @@ -189,6 +194,7 @@ if ($satellitetab == ENTERPRISE_NOT_HOOK) $onheader = array(); $onheader['massive_agents'] = $agentstab; $onheader['massive_modules'] = $modulestab; +$onheader['massive_plugins'] = $pluginstab; if (check_acl ($config['id_user'], 0, "PM")) { $onheader['user_agents'] = $userstab; } @@ -221,8 +227,7 @@ $submit_template_standby = get_parameter('id_alert_template_standby'); $submit_add = get_parameter('crtbutton'); echo '
'; -echo html_print_image("images/wait.gif", true, array("border" => '0')) . - '
'; +echo html_print_image("images/wait.gif", true, array("border" => '0')) . '
'; echo '' . __('Please wait...') . ''; echo '
'; ?> @@ -254,19 +259,14 @@ echo ''; "; echo '
'; -echo ''; -echo ''; -echo '
'; +echo ''; -echo ''; -echo ''; -echo '
'; echo __("Action"); -echo ''; +echo ''; html_print_select($options, 'option', $option, 'this.form.submit()', '', 0, false, false, false); if ($option == 'edit_agents' || $option == 'edit_modules') ui_print_help_tip(__("The blank fields will not be updated")); -echo '
'; +echo '
'; echo '
'; echo "
"; @@ -310,6 +310,9 @@ switch ($option) { case 'copy_modules': require_once ('godmode/massive/massive_copy_modules.php'); break; + case 'edit_plugins': + require_once ('godmode/massive/massive_edit_plugins.php'); + break; default: if (!enterprise_hook('massive_operations', array($option))) { require_once ('godmode/massive/massive_config.php'); diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index 0c2debac22..2785ba13c4 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -105,6 +105,7 @@ if (check_acl ($config['id_user'], 0, "PM")) { $sub2 = array (); $sub2["godmode/massive/massive_operations&tab=massive_agents"]["text"] = __('Agents operations'); $sub2["godmode/massive/massive_operations&tab=massive_modules"]["text"] = __('Modules operations'); + $sub2["godmode/massive/massive_operations&tab=massive_plugins"]["text"] = __('Plugins operations'); if (check_acl ($config['id_user'], 0, "PM")) { $sub2["godmode/massive/massive_operations&tab=massive_users"]["text"] = __('Users operations'); } diff --git a/pandora_console/godmode/servers/plugin.php b/pandora_console/godmode/servers/plugin.php index 9a1f038f83..c2b6dc7803 100644 --- a/pandora_console/godmode/servers/plugin.php +++ b/pandora_console/godmode/servers/plugin.php @@ -312,7 +312,7 @@ if (($create != "") OR ($view != "")) { $data[0] = __('Plugin command'); $data[1] = ''; if ($locked) { - $data[1] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf')); + $data[1] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf lock')); } $data[1] .= ' '; $data[1] .= html_print_image('images/file.png', true); @@ -323,7 +323,7 @@ if (($create != "") OR ($view != "")) { $data[0] = __('Plug-in parameters').ui_print_help_icon ('plugin_parameters', true); $data[1] = ''; if ($locked) { - $data[1] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf')); + $data[1] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf lock')); } $table->data['plugin_parameters'] = $data; @@ -358,7 +358,7 @@ if (($create != "") OR ($view != "")) { $i = 1; while (1) { // Always print at least one macro - if((!isset($macros[$i]) || $macros[$i]['desc'] == '') && $i > 1) { + if ((!isset($macros[$i]) || $macros[$i]['desc'] == '') && $i > 1) { break; } $macro_desc_name = 'field'.$i.'_desc'; @@ -372,19 +372,19 @@ if (($create != "") OR ($view != "")) { $macro_hide_value_name = 'field'.$i.'_hide'; $macro_hide_value_value = 0; - if(isset($macros[$i]['desc'])) { + if (isset($macros[$i]['desc'])) { $macro_desc_value = $macros[$i]['desc']; } - if(isset($macros[$i]['help'])) { + if (isset($macros[$i]['help'])) { $macro_help_value = $macros[$i]['help']; } - if(isset($macros[$i]['value'])) { + if (isset($macros[$i]['value'])) { $macro_value_value = $macros[$i]['value']; } - if(isset($macros[$i]['hide'])) { + if (isset($macros[$i]['hide'])) { $macro_hide_value_value = $macros[$i]['hide']; // Decrypt hidden macros. @@ -394,15 +394,15 @@ if (($create != "") OR ($view != "")) { $datam = array (); $datam[0] = __('Description')." ($macro_name)"; $datam[0] .= html_print_input_hidden($macro_name_name, $macro_name, true); - $datam[1] = html_print_input_text_extended ($macro_desc_name, $macro_desc_value, 'text-'.$macro_desc_name, '', 30, 255, $locked, '', "class='command_advanced_conf'", true); - if($locked) { - $datam[1] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf')); + $datam[1] = html_print_input_text_extended ($macro_desc_name, $macro_desc_value, 'text-'.$macro_desc_name, '', 30, 255, $locked, '', "class='command_macro'", true); + if ($locked) { + $datam[1] .= html_print_image('images/lock.png', true, array('class' => 'command_macro lock')); } $datam[2] = __('Default value')." ($macro_name)"; - $datam[3] = html_print_input_text_extended ($macro_value_name, $macro_value_value, 'text-'.$macro_value_name, '', 30, 255, $locked, '', "class='command_component command_advanced_conf'", true); - if($locked) { - $datam[3] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf')); + $datam[3] = html_print_input_text_extended ($macro_value_name, $macro_value_value, 'text-'.$macro_value_name, '', 30, 255, $locked, '', "class='command_component command_macro'", true); + if ($locked) { + $datam[3] .= html_print_image('images/lock.png', true, array('class' => 'command_macro lock')); } $table->data['plugin_'.$next_name_number] = $datam; @@ -413,7 +413,7 @@ if (($create != "") OR ($view != "")) { $datam = array (); $datam[0] = __('Hide value') . ui_print_help_tip(__('This field will show up as dots like a password'), true); - $datam[1] = html_print_checkbox_extended ($macro_hide_value_name, 1, $macro_hide_value_value, 0, '', array('class' => 'command_advanced_conf'), true, 'checkbox-'.$macro_hide_value_name); + $datam[1] = html_print_checkbox_extended ($macro_hide_value_name, 1, $macro_hide_value_value, 0, '', array('class' => 'command_macro'), true, 'checkbox-'.$macro_hide_value_name); $table->data['plugin_'.$next_name_number] = $datam; $next_name_number++; @@ -423,10 +423,10 @@ if (($create != "") OR ($view != "")) { $datam = array (); $datam[0] = __('Help')." ($macro_name)


"; $tadisabled = $locked === true ? ' disabled' : ''; - $datam[1] = html_print_textarea ($macro_help_name, 6, 100, $macro_help_value, 'class="command_advanced_conf" style="width: 97%;"' . $tadisabled, true); + $datam[1] = html_print_textarea ($macro_help_name, 6, 100, $macro_help_value, 'class="command_macro" style="width: 97%;"' . $tadisabled, true); - if($locked) { - $datam[1] .= html_print_image('images/lock.png', true, array('class' => 'command_advanced_conf')); + if ($locked) { + $datam[1] .= html_print_image('images/lock.png', true, array('class' => 'command_macro lock')); } $datam[1] .= "


"; @@ -435,22 +435,41 @@ if (($create != "") OR ($view != "")) { $i++; } + // Add/Delete buttons + $datam = array (); + $datam[0] = '
' . + '' . + __('Add macro') . + '' . ' ' . + html_print_image('images/add.png',true) . + ''; + $datam[0] .= ''; + $datam[0] .= ''; + if (!$locked) { - $datam = array (); - $datam[0] = ''.__('Add macro').' '.html_print_image('images/add.png',true).''; - $datam[0] .= ''; - $datam[0] .= ''; $delete_macro_style = ''; - if($i <= 2) { + if ($i <= 2) { $delete_macro_style = 'display:none;'; } - $datam[2] = '
'.__('Delete macro').' '.html_print_image('images/delete.png',true).'
'; + $datam[2] = '
' . + '' . + '' . + __('Delete macro') . + '' . ' ' . + html_print_image('images/delete.png',true) . + '' . + '
'; $table->colspan['plugin_action'][0] = 2; - $table->rowstyle['plugin_action'] = 'text-align:center'; $table->colspan['plugin_action'][2] = 2; - $table->data['plugin_action'] = $datam; } + else { + $table->colspan['plugin_action'][0] = 4; + } + + $table->rowstyle['plugin_action'] = 'text-align:center'; + $table->data['plugin_action'] = $datam; + if (defined("METACONSOLE")) { $table->width = '100%'; @@ -749,37 +768,31 @@ ui_require_javascript_file('pandora_modules'); ?>