"".html_print_image( 'images/list.png', true, [ 'title' => __('List'), 'class' => 'invert_filter', ] ).'', ]; // Header. ui_print_page_header( __('Scheduled Downtime'), 'images/gm_monitoring.png', false, '', true, $buttons ); // Recursion group filter. $recursion = get_parameter('recursion', $_POST['recursion']); // Initialize data. $id_group = (int) get_parameter('id_group'); $name = (string) get_parameter('name'); $description = (string) get_parameter('description'); $type_downtime = (string) get_parameter('type_downtime', 'quiet'); $type_execution = (string) get_parameter('type_execution', 'once'); $type_periodicity = (string) get_parameter('type_periodicity', 'weekly'); $utimestamp = get_system_time(); // Fake utimestamp to retrieve the string date of the system. $system_time = ($utimestamp - get_fixed_offset()); $once_date_from = (string) get_parameter( 'once_date_from', date(DATE_FORMAT, $utimestamp) ); $once_time_from = (string) get_parameter( 'once_time_from', date(TIME_FORMAT, $utimestamp) ); $once_date_to = (string) get_parameter( 'once_date_to', date(DATE_FORMAT, $utimestamp) ); $once_time_to = (string) get_parameter( 'once_time_to', date(TIME_FORMAT, ($utimestamp + SECONDS_1HOUR)) ); $periodically_day_from = (int) get_parameter( 'periodically_day_from', 1 ); $periodically_day_to = (int) get_parameter( 'periodically_day_to', 31 ); $periodically_time_from = (string) get_parameter( 'periodically_time_from', date(TIME_FORMAT, $system_time) ); $periodically_time_to = (string) get_parameter( 'periodically_time_to', date(TIME_FORMAT, ($system_time + SECONDS_1HOUR)) ); $hour_from = get_parameter('cron_hour_from', '*'); $minute_from = get_parameter('cron_minute_from', '*'); $mday_from = get_parameter('cron_mday_from', '*'); $month_from = get_parameter('cron_month_from', '*'); $wday_from = get_parameter('cron_wday_from', '*'); $hour_to = get_parameter('cron_hour_to', '*'); $minute_to = get_parameter('cron_minute_to', '*'); $mday_to = get_parameter('cron_mday_to', '*'); $month_to = get_parameter('cron_month_to', '*'); $wday_to = get_parameter('cron_wday_to', '*'); $monday = (bool) get_parameter('monday'); $tuesday = (bool) get_parameter('tuesday'); $wednesday = (bool) get_parameter('wednesday'); $thursday = (bool) get_parameter('thursday'); $friday = (bool) get_parameter('friday'); $saturday = (bool) get_parameter('saturday'); $sunday = (bool) get_parameter('sunday'); $first_create = (int) get_parameter('first_create'); $create_downtime = (int) get_parameter('create_downtime'); $update_downtime = (int) get_parameter('update_downtime'); $edit_downtime = (int) get_parameter('edit_downtime'); $downtime_copy = (int) get_parameter('downtime_copy'); $id_downtime = (int) get_parameter('id_downtime'); $id_agent = (int) get_parameter('id_agent'); $insert_downtime_agent = (int) get_parameter('insert_downtime_agent'); $delete_downtime_agent = (int) get_parameter('delete_downtime_agent'); $modules_selection_mode = (string) get_parameter('modules_selection_mode'); // User groups with AD or AW permission for ACL checks. $user_groups_ad = array_keys( users_get_groups($config['id_user'], $access) ); // INSERT A NEW DOWNTIME_AGENT ASSOCIATION. if ($insert_downtime_agent === 1) { insert_downtime_agent($id_downtime, $user_groups_ad); } // DELETE A DOWNTIME_AGENT ASSOCIATION. if ($delete_downtime_agent === 1) { $id_da = (int) get_parameter('id_downtime_agent'); // 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( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } // Check AD permission on agent. $agent_group = db_get_value( 'id_grupo', 'tagente', 'id_agente', $id_agent ); if ($agent_group === false || !in_array($agent_group, $user_groups_ad) ) { db_pandora_audit( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } // '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 { $row_to_delete = db_get_row('tplanned_downtime_agents', 'id', $id_da); $result = db_process_sql_delete( 'tplanned_downtime_agents', ['id' => $id_da] ); if ($result) { // Delete modules in downtime. db_process_sql_delete( 'tplanned_downtime_modules', [ 'id_downtime' => $row_to_delete['id_downtime'], 'id_agent' => $id_agent, ] ); } } } // UPDATE OR CREATE A DOWNTIME (MAIN DATA, NOT AGENT ASSOCIATION). if ($create_downtime || $update_downtime) { $check = (bool) db_get_value('name', 'tplanned_downtime', 'name', $name); $datetime_from = strtotime($once_date_from.' '.$once_time_from); $datetime_to = strtotime($once_date_to.' '.$once_time_to); $now = time(); if ($type_execution == 'once' && !$config['past_planned_downtimes'] && $datetime_from < $now) { ui_print_error_message( __('Not created. Error inserting data. Start time must be higher than the current time') ); } else if ($type_execution == 'once' && $datetime_from >= $datetime_to) { ui_print_error_message( __('Not created. Error inserting data').'. '.__('The end date must be higher than the start date') ); } else if ($type_execution == 'once' && $datetime_to <= $now && !$config['past_planned_downtimes']) { ui_print_error_message( __('Not created. Error inserting data').'. '.__('The end date must be higher than the current time') ); } else if ($type_execution == 'periodically' && $type_periodicity == 'monthly' && $periodically_day_from == $periodically_day_to && $periodically_time_from >= $periodically_time_to ) { ui_print_error_message( __('Not created. Error inserting data').'. '.__('The end time must be higher than the start time') ); } else if ($type_execution == 'periodically' && $type_periodicity == 'monthly' && $periodically_day_from > $periodically_day_to) { ui_print_error_message( __('Not created. Error inserting data').'. '.__('The end day must be higher than the start day') ); } else { $sql = ''; $error_cron_from = false; $error_cron_to = false; $error_field = ''; if ($type_execution === 'cron') { // Validate 'from' cron values. $hour_from = io_safe_output(trim($hour_from)); if (preg_match('/^((?:([0-1]?[0-9]|2[0-3])|\*)\s*(?:(?:[\/-]([0-1]?[0-9]|2[0-3])))?\s*)$/', $hour_from, $matches) !== 1) { $error_cron_from = true; $error_field = __('hour (from)'); } else { $interval_values = explode('-', $hour_from); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_from = true; } } } $minute_from = io_safe_output(trim($minute_from)); if (preg_match('/^((?:(5[0-9]|[0-5]?[0-9])|\*)\s*(?:(?:[\/-](5[0-9]|[0-5]?[0-9])))?\s*)$/', $minute_from, $matches) !== 1) { $error_cron_from = true; $error_field = __('minute (from)'); } else { $interval_values = explode('-', $minute_from); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_from = true; } } } $mday_from = io_safe_output(trim($mday_from)); if (preg_match('/^((?:(0?[1-9]|[12][0-9]|3[01])|\*)\s*(?:(?:[\/-](0?[1-9]|[12][0-9]|3[01])))?\s*)$/', $mday_from, $matches) !== 1) { $error_cron_from = true; $error_field = __('month day (from)'); } else { $interval_values = explode('-', $mday_from); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_from = true; } } } $month_from = io_safe_output(trim($month_from)); if (preg_match('/^((?:([1-9]|1[012])|\*)\s*(?:(?:[\/-]([1-9]|1[012])))?\s*)$/', $month_from, $matches) !== 1) { $error_cron_from = true; $error_field = __('month (from)'); } else { $interval_values = explode('-', $month_from); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_from = true; } } } $wday_from = io_safe_output(trim($wday_from)); if (preg_match('/^((?:[0-6]|\*)\s*(?:(?:[\/-][0-6]))?\s*)$/', $wday_from, $matches) !== 1) { $error_cron_from = true; $error_field = __('week day (from)'); } else { $interval_values = explode('-', $wday_from); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_from = true; } } } // Validate 'to' cron values. $hour_to = io_safe_output(trim($hour_to)); if (preg_match('/^((?:([0-1]?[0-9]|2[0-3])|\*)\s*(?:(?:[\/-]([0-1]?[0-9]|2[0-3])))?\s*)$/', $hour_to, $matches) !== 1) { $error_cron_to = true; $error_field = __('hour (to)'); } else { $interval_values = explode('-', $hour_to); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_to = true; } } } $minute_to = io_safe_output(trim($minute_to)); if (preg_match('/^((?:(5[0-9]|[0-5]?[0-9])|\*)\s*(?:(?:[\/-](5[0-9]|[0-5]?[0-9])))?\s*)$/', $minute_to, $matches) !== 1) { $error_cron_to = true; $error_field = __('minute (to)'); } else { $interval_values = explode('-', $minute_to); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_to = true; } } } $mday_to = io_safe_output(trim($mday_to)); if (preg_match('/^((?:(0?[1-9]|[12][0-9]|3[01])|\*)\s*(?:(?:[\/-](0?[1-9]|[12][0-9]|3[01])))?\s*)$/', $mday_to, $matches) !== 1) { $error_cron_to = true; $error_field = __('month day (to)'); } else { $interval_values = explode('-', $mday_to); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_to = true; } } } $month_to = io_safe_output(trim($month_to)); if (preg_match('/^((?:([1-9]|1[012])|\*)\s*(?:(?:[\/-]([1-9]|1[012])))?\s*)$/', $month_to, $matches) !== 1) { $error_cron_to = true; $error_field = __('month (to)'); } else { $interval_values = explode('-', $month_to); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_to = true; } } } $wday_to = io_safe_output(trim($wday_to)); if (preg_match('/^((?:[0-6]|\*)\s*(?:(?:[\/-][0-6]))?\s*)$/', $wday_to, $matches) !== 1) { $error_cron_to = true; $error_field = __('week day (to)'); } else { $interval_values = explode('-', $wday_to); if (count($interval_values) > 1) { $interval_from = $interval_values[0]; $interval_to = $interval_values[1]; if ((int) $interval_to < (int) $interval_from) { $error_cron_to = true; } } } $cron_interval_from = io_safe_output($minute_from.' '.$hour_from.' '.$mday_from.' '.$month_from.' '.$wday_from); $cron_interval_to = io_safe_output($minute_to.' '.$hour_to.' '.$mday_to.' '.$month_to.' '.$wday_to); } if (cron_check_syntax($cron_interval_from) !== 1) { $cron_interval_from = ''; } if (cron_check_syntax($cron_interval_to) !== 1) { $cron_interval_to = ''; } if ($create_downtime) { // Check AD permission on new downtime. if (!in_array($id_group, $user_groups_ad)) { db_pandora_audit( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } if ($error_cron_to === true || $error_cron_from === true) { if ($error_cron_from === true) { ui_print_error_message( __('Downtime start cron expression is not correct').': '.$error_field ); } if ($error_cron_to === true) { ui_print_error_message( __('Downtime stop cron expression is not correct').': '.$error_field ); } $result = false; } else { if (trim(io_safe_output($name)) != '') { if (!$check) { $values = [ 'name' => $name, 'description' => $description, 'date_from' => $datetime_from, 'date_to' => $datetime_to, 'executed' => 0, 'id_group' => $id_group, 'only_alerts' => 0, 'monday' => $monday, 'tuesday' => $tuesday, 'wednesday' => $wednesday, 'thursday' => $thursday, 'friday' => $friday, 'saturday' => $saturday, 'sunday' => $sunday, 'periodically_time_from' => $periodically_time_from, 'periodically_time_to' => $periodically_time_to, 'periodically_day_from' => $periodically_day_from, 'periodically_day_to' => $periodically_day_to, 'type_downtime' => $type_downtime, 'type_execution' => $type_execution, 'type_periodicity' => $type_periodicity, 'id_user' => $config['id_user'], 'cron_interval_from' => $cron_interval_from, 'cron_interval_to' => $cron_interval_to, ]; if ($config['dbtype'] == 'oracle') { $values['periodically_time_from'] = '1970/01/01 '.$values['periodically_time_from']; $values['periodically_time_to'] = '1970/01/01 '.$values['periodically_time_to']; } $result = db_process_sql_insert( 'tplanned_downtime', $values ); } else { ui_print_error_message( __('Each scheduled downtime must have a different name') ); } } else { ui_print_error_message( __('Scheduled downtime must have a name') ); } } } else if ($update_downtime) { $old_downtime = db_get_row('tplanned_downtime', 'id', $id_downtime); // Check AD permission on OLD downtime. if (empty($old_downtime) || !in_array($old_downtime['id_group'], $user_groups_ad)) { db_pandora_audit( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } // Check AD permission on NEW downtime group. if (!in_array($id_group, $user_groups_ad)) { db_pandora_audit( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } // 'Is running' check. $is_running = (bool) $old_downtime['executed']; $values = []; if (trim(io_safe_output($name)) == '') { ui_print_error_message( __('Scheduled downtime must have a name') ); } // When running only certain items can be modified for the 'once' type. else if ($is_running && $type_execution == 'once') { $values = [ 'description' => $description, 'date_to' => $datetime_to, 'id_user' => $config['id_user'], ]; } else { $values = [ 'name' => $name, 'description' => $description, 'date_from' => $datetime_from, 'date_to' => $datetime_to, 'id_group' => $id_group, 'only_alerts' => 0, 'monday' => $monday, 'tuesday' => $tuesday, 'wednesday' => $wednesday, 'thursday' => $thursday, 'friday' => $friday, 'saturday' => $saturday, 'sunday' => $sunday, 'periodically_time_from' => $periodically_time_from, 'periodically_time_to' => $periodically_time_to, 'periodically_day_from' => $periodically_day_from, 'periodically_day_to' => $periodically_day_to, 'type_downtime' => $type_downtime, 'type_execution' => $type_execution, 'type_periodicity' => $type_periodicity, 'id_user' => $config['id_user'], 'cron_interval_from' => $cron_interval_from, 'cron_interval_to' => $cron_interval_to, ]; if ($config['dbtype'] == 'oracle') { $values['periodically_time_from'] = '1970/01/01 '.$values['periodically_time_from']; $values['periodically_time_to'] = '1970/01/01 '.$values['periodically_time_to']; } } if ($error_cron_to === true || $error_cron_from === true) { if ($error_cron_from === true) { ui_print_error_message( __('Downtime start cron expression is not correct').': '.$error_field ); } if ($error_cron_to === true) { ui_print_error_message( __('Downtime stop cron expression is not correct').': '.$error_field ); } $result = false; } else { if ($is_running) { $result = false; } else { if (!empty($values)) { $result = db_process_sql_update( 'tplanned_downtime', $values, ['id' => $id_downtime] ); } } } } if ($result === false) { if ($create_downtime) { ui_print_error_message(__('Could not be created')); } else { ui_print_error_message(__('Could not be updated')); } } 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')); } } } } if ($downtime_copy) { $result = planned_downtimes_copy($id_downtime); if ($result['id_downtime'] !== false) { $id_downtime = $result['id_downtime']; ui_print_success_message($result['success']); } else { ui_print_error_message(__($result['error'])); } } // Have any data to show ? if ($id_downtime > 0) { // Columns of the table tplanned_downtime. $columns = [ 'id', 'name', 'description', 'date_from', 'date_to', 'executed', 'id_group', 'only_alerts', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'periodically_time_from', 'periodically_time_to', 'periodically_day_from', 'periodically_day_to', 'type_downtime', 'type_execution', 'type_periodicity', 'id_user', 'cron_interval_from', 'cron_interval_to', ]; switch ($config['dbtype']) { case 'mysql': case 'postgresql': $columns_str = implode(',', $columns); $sql = "SELECT $columns_str FROM tplanned_downtime WHERE id = $id_downtime"; break; case 'oracle': // Oracle doesn't have TIME type, // so we should transform the DATE value. $new_time_from = "TO_CHAR(periodically_time_from, 'HH24:MI:SS') AS periodically_time_from"; $new_time_to = "TO_CHAR(periodically_time_to, 'HH24:MI:SS') AS periodically_time_to"; $time_from_key = array_search('periodically_time_from', $columns); $time_to_key = array_search('periodically_time_to', $columns); if ($time_from_key !== false) { $columns[$time_from_key] = $new_time_from; } if ($time_to_key !== false) { $columns[$time_to_key] = $new_time_to; } $columns_str = implode(',', $columns); $sql = "SELECT $columns_str FROM tplanned_downtime WHERE id = $id_downtime"; break; } $result = db_get_row_sql($sql); // Permission check for the downtime with the AD user groups if (empty($result) || !in_array($result['id_group'], $user_groups_ad)) { db_pandora_audit( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } $name = (string) $result['name']; $id_group = (int) $result['id_group']; $description = (string) $result['description']; $type_downtime = (string) $result['type_downtime']; $type_execution = (string) $result['type_execution']; $type_periodicity = (string) $result['type_periodicity']; $once_date_from = date(DATE_FORMAT, $result['date_from']); $once_date_to = date(DATE_FORMAT, $result['date_to']); $once_time_from = date(TIME_FORMAT, $result['date_from']); $once_time_to = date(TIME_FORMAT, $result['date_to']); $periodically_time_from = (string) $result['periodically_time_from']; $periodically_time_to = (string) $result['periodically_time_to']; $periodically_day_from = (int) $result['periodically_day_from']; $periodically_day_to = (int) $result['periodically_day_to']; $monday = (bool) $result['monday']; $tuesday = (bool) $result['tuesday']; $wednesday = (bool) $result['wednesday']; $thursday = (bool) $result['thursday']; $friday = (bool) $result['friday']; $saturday = (bool) $result['saturday']; $sunday = (bool) $result['sunday']; $cron_interval_from = explode(' ', $result['cron_interval_from']); if (isset($cron_interval_from[4]) === true) { $minute_from = $cron_interval_from[0]; $hour_from = $cron_interval_from[1]; $mday_from = $cron_interval_from[2]; $month_from = $cron_interval_from[3]; $wday_from = $cron_interval_from[4]; } else { $minute_from = '*'; $hour_from = '*'; $mday_from = '*'; $month_from = '*'; $wday_from = '*'; } $cron_interval_to = explode(' ', $result['cron_interval_to']); if (isset($cron_interval_to[4]) === true) { $minute_to = $cron_interval_to[0]; $hour_to = $cron_interval_to[1]; $mday_to = $cron_interval_to[2]; $month_to = $cron_interval_to[3]; $wday_to = $cron_interval_to[4]; } else { $minute_to = '*'; $hour_to = '*'; $mday_to = '*'; $month_to = '*'; $wday_to = '*'; } $running = (bool) $result['executed']; } // When the planned downtime is in execution, // only action to postpone on once type is enabled and the other are disabled. $disabled_in_execution = (int) $running; $return_all_group = false; if (users_can_manage_group_all('AW') === true || $disabled) { $return_all_group = true; } $table = new StdClass(); $table->class = 'databox filters'; $table->width = '100%'; $table->data = []; $table->data[0][0] = __('Name'); $table->data[0][1] = html_print_input_text( 'name', $name, '', 25, 40, true, $disabled_in_execution ); $table->data[1][0] = __('Group'); $table->data[1][1] = '
'.html_print_select_groups( false, $access, $return_all_group, 'id_group', $id_group, '', '', 0, true, false, true, '', $disabled_in_execution ).'
'; $table->data[2][0] = __('Description'); $table->data[2][1] = html_print_textarea( 'description', 3, 35, $description, '', true ); $table->data[3][0] = __('Type').ui_print_help_tip( __('Quiet: Modules will not generate events or fire alerts.').'
'.__('Disable Agents: Disables the selected agents.').'
'.__('Disable Alerts: Disable alerts for the selected agents.'), true ); $table->data[3][1] = html_print_select( [ 'quiet' => __('Quiet'), 'disable_agents' => __('Disabled Agents'), 'disable_agent_modules' => __('Disable Modules'), 'disable_agents_alerts' => __('Disabled only Alerts'), ], 'type_downtime', $type_downtime, 'change_type_downtime()', '', 0, true, false, true, '', $disabled_in_execution ); $table->data[4][0] = __('Execution'); $table->data[4][1] = html_print_select( [ 'once' => __('Once'), 'periodically' => __('Periodically'), 'cron' => __('Cron from/to'), ], 'type_execution', $type_execution, 'change_type_execution();', '', 0, true, false, true, '', $disabled_in_execution ); $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 "
"; } 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->size[0] = '25%'; $table->data[0][0] = __('Group filter'); $table->data[0][1] = html_print_select_groups( false, $access, $return_all_group, '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, 'min-width: 250px;width: 70%;'); $table->rowid[2] = 'available_modules_selection_mode'; $table->data[2][1] = html_print_select( [ 'common' => __('Show common modules'), 'all' => __('Show all modules'), ], 'modules_selection_mode', 'common', false, '', '', true, false, true, '', false, 'min-width:180px;' ); $table->rowid[3] = 'available_modules'; $table->data[3][0] = __('Available modules:').ui_print_help_tip( __('Only for type Quiet for downtimes.'), true ); $table->data[3][1] = html_print_select( [], 'module[]', '', '', '', 0, true, true, true, '', false, 'min-width: 250px;width: 70%;' ); // Print agent table. html_print_table($table); echo '


'; html_print_input_hidden('id_agent', $id_agent); echo '
'; if ($id_downtime > 0) { html_print_input_hidden('update_downtime', 1); html_print_input_hidden('id_downtime', $id_downtime); html_print_submit_button( __('Update'), 'updbutton', false, 'class="sub upd"' ); } else { html_print_input_hidden('create_downtime', 1); html_print_submit_button( __('Add'), 'crtbutton', false, 'class="sub wand"' ); } echo '
'; html_print_input_hidden('all_common_modules', ''); 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'); } } if (!$running) { $data[5] = ''; if ($type_downtime !== 'disable_agents') { $data[5] = ''.html_print_image('images/config.png', true, ['border' => '0', 'alt' => __('Delete'), 'class' => 'invert_filter']).''; } $data[5] .= ''.html_print_image('images/cross.png', true, ['border' => '0', 'alt' => __('Delete'), 'class' => 'invert_filter']).''; } $table->data['agent_'.$downtime_agent['id_agente']] = $data; } html_print_table($table); } $table = new stdClass(); $table->id = 'loading'; $table->width = '100%'; $table->colspan['loading'][0] = '6'; $table->style[0] = 'text-align: center;'; $table->data = []; $table->data['loading'] = []; $table->data['loading'][0] = html_print_image('images/spinner.gif', true); echo "'; $table = new stdClass(); $table->id = 'editor'; $table->width = '100%'; $table->colspan['module'][1] = '5'; $table->data = []; $table->data['module'] = []; $table->data['module'][0] = ''; $table->data['module'][1] = '

'.__('Modules').'

'; // List of modules, empty, it is populated by javascript. $table->data['module'][1] = "
".__('Module')." ".__('Action')."
".__('Add Module:').' '.html_print_select( [], 'modules', '', '', '', 0, true, false, true, '', false, false, false, false, false, '', false, false, false, false, false )." ".''."
'; echo "'; echo "'; echo "'; echo "'; 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( AUDIT_LOG_ACL_VIOLATION, 'Trying to access downtime scheduler' ); include 'general/noaccess.php'; return; } $agents = (array) get_parameter('id_agents'); $filter_group = (int) get_parameter('filter_group', 0); $module_names = (array) get_parameter('module'); $modules_selection_mode = (string) get_parameter('modules_selection_mode'); $type_downtime = (string) get_parameter('type_downtime', 'quiet'); $recursion = (bool) get_parameter_checkbox('recursion', false); $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 ($type_downtime === 'disable_agents') { $all_modules = true; } 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) { if ($recursion === true) { $filter_group = groups_get_children_ids( $filter_group, false, true, 'AW' ); }; $agents = db_get_all_rows_filter( 'tagente', ['id_grupo' => $filter_group], 'id_agente' ); $agents = array_reduce( $agents, function ($carry, $item) { $carry[] = $item['id_agente']; return $carry; } ); } foreach ($agents as $agent_id) { $agent_id = (int) $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; } // Check if agent is already in downtime. $agent_in_downtime = db_get_value_filter( 'id_downtime', 'tplanned_downtime_agents', [ 'id_agent' => $agent_id, 'id_downtime' => $id_downtime, ] ); if ($agent_in_downtime !== false) { $values = ['all_modules' => $all_modules]; $result = db_process_sql_update( 'tplanned_downtime_agents', $values, [ 'id_downtime' => $id_downtime, 'id_agent' => $agent_id, ] ); } else { $values = [ 'id_downtime' => $id_downtime, 'id_agent' => $agent_id, 'all_modules' => $all_modules, ]; $result = db_process_sql_insert( 'tplanned_downtime_agents', $values ); } if ($result !== false && (bool) $all_modules === false) { foreach ($module_names as $module_name) { $module = modules_get_agentmodule_id( $module_name, $agent_id ); if (empty($module)) { continue; } // Check if modules are already in downtime. $module_in_downtime = db_get_value_filter( 'id_downtime', 'tplanned_downtime_modules', [ 'id_downtime' => $id_downtime, 'id_agent' => $agent_id, 'id_agent_module' => $module['id_agente_modulo'], ] ); if ($module_in_downtime !== false) { continue; } else { $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 !== false) { $values = ['id_user' => $config['id_user']]; $result = db_process_sql_update( 'tplanned_downtime', $values, ['id' => $id_downtime] ); } } } } } } ?>