diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index e2c645a414..10f323ffab 100644 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -75,6 +75,7 @@ ui_print_page_header( // Recursion group filter. $recursion = get_parameter('recursion', $_POST['recursion']); + // Initialize data. $id_group = (int) get_parameter('id_group'); $name = (string) get_parameter('name'); @@ -147,144 +148,9 @@ $user_groups_ad = array_keys( users_get_groups($config['id_user'], $access) ); -// Check AD permission on downtime. -$downtime_group = db_get_value( - 'id_group', - 'tplanned_downtime', - 'id', - $id_downtime -); - -if ($id_downtime > 0) { - if (!check_acl_restricted_all($config['id_user'], $downtime_group, 'AW') - && !check_acl_restricted_all($config['id_user'], $downtime_group, 'AD') - ) { - db_pandora_audit( - 'ACL Violation', - 'Trying to access downtime scheduler' - ); - include 'general/noaccess.php'; - return; - } -} - // INSERT A NEW DOWNTIME_AGENT ASSOCIATION. if ($insert_downtime_agent === 1) { - if ($downtime_group === false - || !in_array($downtime_group, $user_groups_ad) - ) { - db_pandora_audit( - 'ACL Violation', - 'Trying to access downtime scheduler' - ); - include 'general/noaccess.php'; - return; - } - - $agents = (array) get_parameter('id_agents'); - $module_names = (array) get_parameter('module'); - - $all_modules = ($modules_selection_mode === 'all' && (empty($module_names) || (int) $modules[0] === 0)); - $all_common_modules = ($modules_selection_mode === 'common' && (empty($module_names) || (int) $modules[0] === 0)); - - if ($all_common_modules === true) { - $module_names = explode(',', get_parameter('all_common_modules')); - } - - // 'Is running' check. - $is_running = (bool) db_get_value( - 'executed', - 'tplanned_downtime', - 'id', - $id_downtime - ); - if ($is_running) { - ui_print_error_message( - __('This elements cannot be modified while the downtime is being executed') - ); - } else { - // If is selected 'Any', get all the agents. - if (count($agents) === 1 && (int) $agents[0] === -2) { - $all_agents = get_parameter('all_agents'); - $agents = explode(',', $all_agents); - } - - foreach ($agents as $agent_id) { - // Check module belongs to the agent. - if ($modules_selection_mode == 'all' && $all_modules === false) { - $check = false; - foreach ($module_names as $module_name) { - $check_module = modules_get_agentmodule_id( - $module_name, - $agent_id - ); - if (!empty($check_module)) { - $check = true; - } - } - - if (!$check) { - continue; - } - } - - // Check AD permission on agent. - $agent_group = db_get_value( - 'id_grupo', - 'tagente', - 'id_agente', - $agent_id - ); - - if ($agent_group === false - || !in_array($agent_group, $user_groups_ad) - ) { - continue; - } - - $values = [ - 'id_downtime' => $id_downtime, - 'id_agent' => $agent_id, - 'all_modules' => $all_modules, - ]; - $result = db_process_sql_insert( - 'tplanned_downtime_agents', - $values - ); - - if ($result && !$all_modules) { - foreach ($module_names as $module_name) { - $module = modules_get_agentmodule_id( - $module_name, - $agent_id - ); - - if (empty($module)) { - continue; - } - - $values = [ - 'id_downtime' => $id_downtime, - 'id_agent' => $agent_id, - 'id_agent_module' => $module['id_agente_modulo'], - ]; - $result = db_process_sql_insert( - 'tplanned_downtime_modules', - $values - ); - - if ($result) { - $values = ['id_user' => $config['id_user']]; - $result = db_process_sql_update( - 'tplanned_downtime', - $values, - ['id' => $id_downtime] - ); - } - } - } - } - } + insert_downtime_agent($id_downtime, $user_groups_ad); } // DELETE A DOWNTIME_AGENT ASSOCIATION. @@ -490,10 +356,6 @@ if ($create_downtime || $update_downtime) { 'date_to' => $datetime_to, 'id_user' => $config['id_user'], ]; - } else if ($is_running) { - ui_print_error_message( - __('Cannot be modified while the downtime is being executed') - ); } else { $values = [ 'name' => $name, @@ -524,12 +386,16 @@ if ($create_downtime || $update_downtime) { } } - if (!empty($values)) { - $result = db_process_sql_update( - 'tplanned_downtime', - $values, - ['id' => $id_downtime] - ); + if ($is_running) { + $result = false; + } else { + if (!empty($values)) { + $result = db_process_sql_update( + 'tplanned_downtime', + $values, + ['id' => $id_downtime] + ); + } } } @@ -542,6 +408,9 @@ if ($create_downtime || $update_downtime) { } else { if ($create_downtime && $name && !$check) { $id_downtime = $result; + + insert_downtime_agent($id_downtime, $user_groups_ad); + ui_print_success_message(__('Successfully created')); } else if ($update_downtime && $name) { ui_print_success_message(__('Successfully updated')); @@ -584,8 +453,8 @@ if ($id_downtime > 0) { case 'postgresql': $columns_str = implode(',', $columns); $sql = "SELECT $columns_str - FROM tplanned_downtime - WHERE id = $id_downtime"; + FROM tplanned_downtime + WHERE id = $id_downtime"; break; case 'oracle': @@ -607,8 +476,8 @@ if ($id_downtime > 0) { $columns_str = implode(',', $columns); $sql = "SELECT $columns_str - FROM tplanned_downtime - WHERE id = $id_downtime"; + FROM tplanned_downtime + WHERE id = $id_downtime"; break; } @@ -672,20 +541,11 @@ $table->data[0][1] = html_print_input_text( true, $disabled_in_execution ); - -$return_all_group = false; - -if (users_can_manage_group_all('AW') === true - || users_can_manage_group_all('AD') === true -) { - $return_all_group = true; -} - $table->data[1][0] = __('Group'); $table->data[1][1] = '
'.html_print_select_groups( false, $access, - $return_all_group, + true, 'id_group', $id_group, '', @@ -750,23 +610,23 @@ $days = array_combine(range(1, 31), range(1, 31)); $table->data[5][0] = __('Configure the time').' '; ; $table->data[5][1] = " - - '; if ($id_downtime > 0) { - echo ''; - echo "
"; + echo ""; +} else { + echo ''; } // Editor form. html_print_table($table); +echo ""; + +$filter_group = (int) get_parameter('filter_group', 0); + +// User AD groups to str for the filter. +$id_groups_str = implode(',', $user_groups_ad); + +if (empty($id_groups_str)) { + // Restrictive filter on error. This will filter all the downtimes. + $id_groups_str = '-1'; +} + +$filter_cond = ''; +if ($filter_group > 0) { + if ($recursion) { + $rg = groups_get_children_ids($filter_group, true); + $filter_cond .= ' AND id_grupo IN ('; + + $i = 0; + $len = count($rg); + + foreach ($rg as $key) { + if ($i == ($len - 1)) { + $filter_cond .= $key.')'; + } else { + $i++; + $filter_cond .= $key.','; + } + } + } else { + $filter_cond = " AND id_grupo = $filter_group "; + } +} + +$agents = get_planned_downtime_agents_list($id_downtime, $filter_cond, $id_groups_str); + +$disabled_add_button = false; +if (empty($agents) || $disabled_in_execution) { + $disabled_add_button = true; +} + +// Show available agents to include into downtime +$table = new StdClass(); +$table->class = 'databox filters'; +$table->width = '100%'; +$table->data = []; + +$table->data[0][0] = __('Group filter'); +$table->data[0][1] = html_print_select_groups(false, $access, true, 'filter_group', $filter_group, '', '', '', true, false, true, '', false, 'min-width:180px;margin-right:15px;'); +$table->data[0][2] = __('Recursion').'  '.html_print_checkbox('recursion', 1, $recursion, true, false, ''); + +$table->data[1][0] = __('Available agents'); +$table->data[1][1] = html_print_select($agents, 'id_agents[]', -1, '', _('Any'), -2, true, true, true, '', false, 'width: 180px;'); + + +if ($type_downtime != 'quiet') { + echo '"; +// Start Overview of existing planned downtime. +echo '

'.__('Agents planned for this downtime').':

'; - $filter_group = (int) get_parameter('filter_group', 0); +// User the $id_groups_str built before. +$sql = sprintf( + 'SELECT ta.nombre, tpda.id, + ta.id_os, ta.id_agente, ta.id_grupo, + ta.ultimo_contacto, tpda.all_modules + FROM tagente ta + INNER JOIN tplanned_downtime_agents tpda + ON ta.id_agente = tpda.id_agent + AND tpda.id_downtime = %d + WHERE ta.id_grupo IN (%s)', + $id_downtime, + $id_groups_str +); +$downtimes_agents = db_get_all_rows_sql($sql); - // User AD groups to str for the filter. - $id_groups_str = implode(',', $user_groups_ad); +if (empty($downtimes_agents)) { + echo '
'.__('There are no agents').'
'; +} else { + $table = new stdClass(); + $table->id = 'list'; + $table->class = 'databox data'; + $table->width = '100%'; + $table->data = []; + $table->head = []; + $table->head[0] = __('Name'); + $table->head[1] = __('Group'); + $table->head[2] = __('OS'); + $table->head[3] = __('Last contact'); + $table->head['count_modules'] = __('Modules'); - if (empty($id_groups_str)) { - // Restrictive filter on error. This will filter all the downtimes. - $id_groups_str = '-1'; + if (!$running) { + $table->head[5] = __('Actions'); + $table->align[5] = 'center'; + $table->size[5] = '5%'; } - $filter_cond = ''; - if ($filter_group > 0) { - if ($recursion) { - $rg = groups_get_children_ids($filter_group, true); - $filter_cond .= ' AND id_grupo IN ('; + foreach ($downtimes_agents as $downtime_agent) { + $data = []; - $i = 0; - $len = count($rg); + $alias = db_get_value( + 'alias', + 'tagente', + 'id_agente', + $downtime_agent['id_agente'] + ); + $data[0] = $alias; - foreach ($rg as $key) { - if ($i == ($len - 1)) { - $filter_cond .= $key.')'; - } else { - $i++; - $filter_cond .= $key.','; - } - } + $data[1] = db_get_sql( + 'SELECT nombre + FROM tgrupo + WHERE id_grupo = '.$downtime_agent['id_grupo'] + ); + + $data[2] = ui_print_os_icon($downtime_agent['id_os'], true, true); + + $data[3] = $downtime_agent['ultimo_contacto']; + + if ($type_downtime == 'disable_agents_alerts') { + $data['count_modules'] = __('All alerts'); + } else if ($type_downtime == 'disable_agents') { + $data['count_modules'] = __('Entire agent'); } else { - $filter_cond = " AND id_grupo = $filter_group "; + if ($downtime_agent['all_modules']) { + $data['count_modules'] = __('All modules'); + } else { + $data['count_modules'] = __('Some modules'); + } } - } - - $agents = get_planned_downtime_agents_list($id_downtime, $filter_cond, $id_groups_str); - - $disabled_add_button = false; - if (empty($agents) || $disabled_in_execution) { - $disabled_add_button = true; - } - - echo "
"; - html_print_select_groups(false, $access, true, 'filter_group', $filter_group, '', '', '', false, false, true, '', false, 'min-width:180px;margin-right:15px;'); - - html_print_checkbox('recursion', 1, $recursion, false, false, ''); - echo __('Recursion').' '; - echo '

'; - html_print_submit_button(__('Filter by group'), '', false, 'class="sub next"', false); - echo '
'; - // Show available agents to include into downtime - echo '

'.__('Available agents').':

'; - echo "
"; - html_print_input_hidden('all_agents', implode(',', array_keys($agents))); - echo html_print_select($agents, 'id_agents[]', -1, '', _('Any'), -2, false, true, true, '', false, 'width: 180px;'); - - if ($type_downtime != 'quiet') { - echo '
'; - - // Start Overview of existing planned downtime. - echo '

'.__('Agents planned for this downtime').':

'; - - // User the $id_groups_str built before. - $sql = sprintf( - 'SELECT ta.nombre, tpda.id, - ta.id_os, ta.id_agente, ta.id_grupo, - ta.ultimo_contacto, tpda.all_modules - FROM tagente ta - INNER JOIN tplanned_downtime_agents tpda - ON ta.id_agente = tpda.id_agent - AND tpda.id_downtime = %d - WHERE ta.id_grupo IN (%s)', - $id_downtime, - $id_groups_str - ); - $downtimes_agents = db_get_all_rows_sql($sql); - - if (empty($downtimes_agents)) { - echo '
'.__('There are no agents').'
'; - } else { - $table = new stdClass(); - $table->id = 'list'; - $table->class = 'databox data'; - $table->width = '100%'; - $table->data = []; - $table->head = []; - $table->head[0] = __('Name'); - $table->head[1] = __('Group'); - $table->head[2] = __('OS'); - $table->head[3] = __('Last contact'); - $table->head['count_modules'] = __('Modules'); if (!$running) { - $table->head[5] = __('Actions'); - $table->align[5] = 'center'; - $table->size[5] = '5%'; - } - - foreach ($downtimes_agents as $downtime_agent) { - $data = []; - - $alias = db_get_value( - 'alias', - 'tagente', - 'id_agente', - $downtime_agent['id_agente'] - ); - $data[0] = $alias; - - $data[1] = db_get_sql( - 'SELECT nombre - FROM tgrupo - WHERE id_grupo = '.$downtime_agent['id_grupo'] - ); - - $data[2] = ui_print_os_icon($downtime_agent['id_os'], true, true); - - $data[3] = $downtime_agent['ultimo_contacto']; - - if ($type_downtime == 'disable_agents_alerts') { - $data['count_modules'] = __('All alerts'); - } else if ($type_downtime == 'disable_agents') { - $data['count_modules'] = __('Entire agent'); - } else { - if ($downtime_agent['all_modules']) { - $data['count_modules'] = __('All modules'); - } else { - $data['count_modules'] = __('Some modules'); - } + $data[5] = ''; + if ($type_downtime != 'disable_agents_alerts' + && $type_downtime != 'disable_agents' + ) { + $data[5] = ''.html_print_image('images/config.png', true, ['border' => '0', 'alt' => __('Delete'), 'class' => 'invert_filter']).''; } - if (!$running) { - $data[5] = ''; - if ($type_downtime != 'disable_agents_alerts' - && $type_downtime != 'disable_agents' - ) { - $data[5] = ''.html_print_image( - 'images/config.png', - true, - [ - 'border' => '0', - 'alt' => __('Delete'), - ] - ).''; - } - - $data[5] .= ''.html_print_image( - 'images/cross.png', - true, - [ - 'border' => '0', - 'alt' => __('Delete'), - 'class' => 'invert_filter', - ] - ).''; - } - - $table->data['agent_'.$downtime_agent['id_agente']] = $data; + $data[5] .= ''.html_print_image('images/cross.png', true, ['border' => '0', 'alt' => __('Delete'), 'class' => 'invert_filter']).''; } - html_print_table($table); + $table->data['agent_'.$downtime_agent['id_agente']] = $data; } + + html_print_table($table); } $table = new stdClass(); @@ -1145,20 +984,20 @@ $table->data['module'][1] = '

'.__('Modules').'

'; // List of modules, empty, it is populated by javascript. $table->data['module'][1] = " - - - - - - - - - - - + +
".__('Module')."".__('Action')."
".''.html_print_image( + + + + + + + + + + + - - - + + - - -
".__('Module')."".__('Action')."
".__('Add Module:').' '.html_print_select( +
".__('Add Module:').' '.html_print_select( [], 'modules', '', @@ -1178,7 +1017,7 @@ $table->data['module'][1] = " 0, true )."".'".''."'.'
'; +).'
'."
'; echo "'; ui_include_time_picker(); ui_require_jquery_file('ui.datepicker-'.get_user_language(), 'include/javascript/i18n/'); +// Auxiliary function for downtime agent creation. +function insert_downtime_agent($id_downtime, $user_groups_ad) +{ + // Check AD permission on downtime. + $downtime_group = db_get_value( + 'id_group', + 'tplanned_downtime', + 'id', + $id_downtime + ); + + if ($downtime_group === false + || !in_array($downtime_group, $user_groups_ad) + ) { + db_pandora_audit( + 'ACL Violation', + 'Trying to access downtime scheduler' + ); + include 'general/noaccess.php'; + return; + } + + $agents = (array) get_parameter('id_agents'); + $module_names = (array) get_parameter('module'); + $modules_selection_mode = (string) get_parameter('modules_selection_mode'); + + $all_modules = ($modules_selection_mode === 'all' && (empty($module_names) || (string) $module_names[0] === '0')); + $all_common_modules = ($modules_selection_mode === 'common' && (empty($module_names) || (string) $module_names[0] === '0')); + + if ($all_common_modules === true) { + $module_names = explode(',', get_parameter('all_common_modules')); + } + + // 'Is running' check. + $is_running = (bool) db_get_value( + 'executed', + 'tplanned_downtime', + 'id', + $id_downtime + ); + if ($is_running) { + ui_print_error_message( + __('This elements cannot be modified while the downtime is being executed') + ); + } else { + // If is selected 'Any', get all the agents. + if (count($agents) === 1 && (int) $agents[0] === -2) { + $all_agents = get_parameter('all_agents'); + $agents = explode(',', $all_agents); + } + + foreach ($agents as $agent_id) { + // Check module belongs to the agent. + if ($modules_selection_mode == 'all' && $all_modules === false) { + $check = false; + foreach ($module_names as $module_name) { + $check_module = modules_get_agentmodule_id( + $module_name, + $agent_id + ); + if (!empty($check_module)) { + $check = true; + } + } + + if (!$check) { + continue; + } + } + + // Check AD permission on agent. + $agent_group = db_get_value( + 'id_grupo', + 'tagente', + 'id_agente', + $agent_id + ); + + if ($agent_group === false + || !in_array($agent_group, $user_groups_ad) + ) { + continue; + } + + $values = [ + 'id_downtime' => $id_downtime, + 'id_agent' => $agent_id, + 'all_modules' => $all_modules, + ]; + $result = db_process_sql_insert( + 'tplanned_downtime_agents', + $values + ); + + if ($result && !$all_modules) { + foreach ($module_names as $module_name) { + $module = modules_get_agentmodule_id( + $module_name, + $agent_id + ); + + if (empty($module)) { + continue; + } + + $values = [ + 'id_downtime' => $id_downtime, + 'id_agent' => $agent_id, + 'id_agent_module' => $module['id_agente_modulo'], + ]; + $result = db_process_sql_insert( + 'tplanned_downtime_modules', + $values + ); + + if ($result) { + $values = ['id_user' => $config['id_user']]; + $result = db_process_sql_update( + 'tplanned_downtime', + $values, + ['id' => $id_downtime] + ); + } + } + } + } + } +} + + ?>