From 8bb5ec8849459cde14a2e3fb13e9fa1475be7cbc Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Wed, 10 Sep 2014 17:49:17 +0200 Subject: [PATCH 01/12] Improved the autocomplete input for modules * pandora_console/include/ajax/module.php: Improved the module name search. * pandora_consle/include/functons_html.php: Improved the autocomplete input for modules. --- pandora_console/include/ajax/module.php | 2 +- pandora_console/include/functions_html.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/ajax/module.php b/pandora_console/include/ajax/module.php index 81ef02fc34..1f5235d7d8 100644 --- a/pandora_console/include/ajax/module.php +++ b/pandora_console/include/ajax/module.php @@ -48,7 +48,7 @@ if ($get_plugin_macros) { if ($search_modules) { $id_agents = json_decode(io_safe_output(get_parameter('id_agents'))); - $filter = get_parameter('q', '') . '%'; + $filter = '%' . get_parameter('q', '') . '%'; $other_filter = json_decode(io_safe_output(get_parameter('other_filter')), true); $modules = agents_get_modules($id_agents, false, diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 62a43605a1..f52f73a838 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -2007,6 +2007,12 @@ function html_print_autocomplete_modules($name = 'module', } }); }, + change: function( event, ui ) { + if (!ui.item) + $("input[name='_hidden']") + .val(0); + return false; + }, select: function( event, ui ) { $("input[name='_hidden']") .val(ui.item.value); From b06fc9d0393ebdfa747b3d1d73f8c34d2c092c80 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Wed, 10 Sep 2014 18:02:32 +0200 Subject: [PATCH 02/12] Improvements on the Planned Downtime section * pandora_console/godmode/agentes/planned_downtime.editor.php: Fixed an error which made impossible the creation of new periodic downtimes. * pandora_console/godmode/agentes/planned_downtime.export_csv.php: Added file. Exports the planned downtimes list to a CSV file. * pandora_console/godmode/agentes/planned_downtime.list.php: Added a new form to filter the list. Added pagination. Changed the items order. Error fixes. --- .../agentes/planned_downtime.editor.php | 4 +- .../agentes/planned_downtime.export_csv.php | 215 ++++++++++++++++ .../godmode/agentes/planned_downtime.list.php | 240 ++++++++++++++++-- 3 files changed, 430 insertions(+), 29 deletions(-) create mode 100644 pandora_console/godmode/agentes/planned_downtime.export_csv.php diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index 87c2f75060..f540de54b0 100755 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -152,10 +152,10 @@ if ($create_downtime || $update_downtime) { $datetime_to = strtotime ($once_date_to . ' ' . $once_time_to); $now = strtotime(date(DATE_FORMAT). ' ' . date(TIME_FORMAT)); - if ($datetime_from < $now) { + if ($type_execution == 'once' && $datetime_from < $now) { ui_print_error_message(__('Not created. Error inserting data. Start time must be higher than the current time' )); } - else if ($datetime_from >= $datetime_to) { + else if ($type_execution == 'once' && $datetime_from >= $datetime_to) { ui_print_error_message(__('Not created. Error inserting data' ). ': START >= END'); } diff --git a/pandora_console/godmode/agentes/planned_downtime.export_csv.php b/pandora_console/godmode/agentes/planned_downtime.export_csv.php new file mode 100644 index 0000000000..da4fd6564b --- /dev/null +++ b/pandora_console/godmode/agentes/planned_downtime.export_csv.php @@ -0,0 +1,215 @@ += '".strtotime("$date_from 00:00:00")."'))"; +} + +if (!empty($date_to)) { + $periodically_monthly_w = "type_periodicity = 'monthly' AND (periodically_day_from <= '".date('d', strtotime($date_from))."' AND periodically_time_to >= '".date('d', strtotime($date_to))."')"; + + $periodically_weekly_days = array(); + $date_from_aux = strtotime($date_from); + $date_end = strtotime($date_to); + $days_number = 0; + + while ($date_from_aux <= $date_end && $days_number < 7) { + $weekday_actual = strtolower(date('l', $date_from_aux)); + + $periodically_weekly_days[] = "$weekday_actual = 1"; + + $date_from_aux = $date_from_aux + SECONDS_1DAY; + $days_number++; + } + + $periodically_weekly_w = "type_periodicity = 'weekly' AND (".implode(" OR ", $periodically_weekly_days).")"; + + $periodically_w = "type_execution = 'periodically' AND (($periodically_monthly_w) OR ($periodically_weekly_w))"; + + $once_w = "type_execution = 'once' AND date_to <= '".strtotime("$date_to 23:59:59")."'"; + + $where_values .= " AND (($periodically_w) OR ($once_w))"; +} + +if (!$show_archived) { + $where_values .= " AND (type_execution = 'periodically' OR (type_execution = 'once' AND date_to >= '".time()."'))"; +} + +if (!empty($agent_id)) { + $where_values .= " AND id IN (SELECT id_downtime FROM tplanned_downtime_agents WHERE id_agent = $agent_id)"; +} + +if (!empty($module_id)) { + $where_values .= " AND (id IN (SELECT id_downtime + FROM tplanned_downtime_modules + WHERE id_agent_module = $module_id) + OR id IN (SELECT id_downtime + FROM tplanned_downtime_agents tpda, tagente_modulo tam + WHERE tpda.id_agent = tam.id_agente + AND tam.id_agente_modulo = $module_id + AND tpda.all_modules = 1))"; +} + +$sql = "SELECT * + FROM tplanned_downtime + WHERE $where_values + ORDER BY type_execution DESC, date_from DESC"; +$downtimes = @db_get_all_rows_sql($sql); +html_debug_print($sql); +html_debug_print($downtimes); +if (!empty($downtimes)) { + ob_clean(); + // Show contentype header + Header("Content-type: text/csv"); + header('Content-Disposition: attachment; filename="pandora_planned_downtime_'.date("Y/m/d H:i:s").'.csv"'); + + $titles = array(); + $titles[] = "id"; + $titles[] = "name"; + $titles[] = "description"; + $titles[] = "group"; + $titles[] = "type"; + $titles[] = "execution_type"; + $titles[] = "execution_date"; + + echo implode($separator, $titles); + echo chr(13); + + foreach ($downtimes as $downtime) { + $id = $downtime['id']; + $name = io_safe_output($downtime['name']); + $description = io_safe_output($downtime['description']); + $group = ucfirst(io_safe_output(groups_get_name($downtime['id_group']))); + $type = ucfirst(io_safe_output($downtime['type_downtime'])); + $execution_type = ucfirst(io_safe_output($downtime['type_execution'])); + + switch ($downtime['type_execution']) { + case 'once': + $execution_date = date ("Y-m-d H:i", $downtime['date_from']) . + " " . __('to') . " ". + date ("Y-m-d H:i", $downtime['date_to']); + break; + case 'periodically': + switch ($downtime['type_periodicity']) { + case 'weekly': + $execution_date = __('Weekly:'); + $execution_date .= " "; + if ($downtime['monday']) { + $execution_date .= __('Mon'); + $execution_date .= " "; + } + if ($downtime['tuesday']) { + $execution_date .= __('Tue'); + $execution_date .= " "; + } + if ($downtime['wednesday']) { + $execution_date .= __('Wed'); + $execution_date .= " "; + } + if ($downtime['thursday']) { + $execution_date .= __('Thu'); + $execution_date .= " "; + } + if ($downtime['friday']) { + $execution_date .= __('Fri'); + $execution_date .= " "; + } + if ($downtime['saturday']) { + $execution_date .= __('Sat'); + $execution_date .= " "; + } + if ($downtime['sunday']) { + $execution_date .= __('Sun'); + $execution_date .= " "; + } + $execution_date .= " (" . $downtime['periodically_time_from']; + $execution_date .= "-" . $downtime['periodically_time_to'] . ")"; + break; + case 'monthly': + $execution_date = __('Monthly:'); + $execution_date .= __('From day') . " " . $downtime['periodically_day_from']; + $execution_date .= "/" . __('To day') . " "; + $execution_date .= $downtime['periodically_day_to']; + $execution_date .= " (" . $downtime['periodically_time_from']; + $execution_date .= "-" . $downtime['periodically_time_to'] . ")"; + break; + } + break; + } + $execution_date = io_safe_output($execution_date); + + $values = array(); + $values[] = $id; + $values[] = $name; + $values[] = $description; + $values[] = $group; + $values[] = $type; + $values[] = $execution_type; + $values[] = $execution_date; + + echo implode($separator, $values); + echo chr(13); + } +} +else { + echo '
'.__('No planned downtime').'
'; +} +?> \ No newline at end of file diff --git a/pandora_console/godmode/agentes/planned_downtime.list.php b/pandora_console/godmode/agentes/planned_downtime.list.php index e14ee776b0..c9dc4eb5cc 100755 --- a/pandora_console/godmode/agentes/planned_downtime.list.php +++ b/pandora_console/godmode/agentes/planned_downtime.list.php @@ -180,9 +180,95 @@ if ($delete_downtime) { } } -$groups = users_get_groups (); +// Filter parameters +$offset = (int) get_parameter('offset'); +$search_text = (string) get_parameter('search_text'); +$date_from = (string) get_parameter('date_from'); +$date_to = (string) get_parameter('date_to'); +$execution_type = (string) get_parameter('execution_type'); +$show_archived = (bool) get_parameter('archived'); +$agent_id = (int) get_parameter('agent_id'); +$agent_name = !empty($agent_id) ? (string) get_parameter('agent_name') : ""; +$module_id = (int) get_parameter('module_name_hidden'); +$module_name = !empty($module_id) ? (string) get_parameter('module_name') : ""; + +$filter_params = array(); +$filter_params['search_text'] = $search_text; +$filter_params['date_from'] = $date_from; +$filter_params['date_to'] = $date_to; +$filter_params['execution_type'] = $execution_type; +$filter_params['archived'] = $show_archived; +$filter_params['agent_id'] = $agent_id; +$filter_params['agent_name'] = $agent_name; +$filter_params['module_id'] = $module_id; +$filter_params['module_name'] = $module_name; + +$filter_params_aux = array(); +foreach ($filter_params as $name => $value) { + $filter_params_aux[] = is_bool($value) ? $name."=".(int)$value : "$name=$value"; +} +$filter_params_str = !empty($filter_params_aux) ? implode("&", $filter_params_aux) : ""; + +// Table filter +$table = new StdClass(); +$table->class = 'databox'; +$table->width = '99%'; +$table->rowstyle = array(); +$table->rowstyle[0] = "background-color: #f9faf9;"; +$table->rowstyle[1] = "background-color: #f9faf9;"; +$table->rowstyle[2] = "background-color: #f9faf9;"; +$table->data = array(); + +$row = array(); + +// Search text +$row[] = __('Search') . ' ' . html_print_input_text("search_text", $search_text, '', 50, 250, true); +// Dates +$date_inputs = __('From') . ' ' . html_print_input_text('date_from', $date_from, '', 10, 10, true); +$date_inputs .= "  "; +$date_inputs .= __('To') . ' ' . html_print_input_text('date_to', $date_to, '', 10, 10, true); +$row[] = $date_inputs; + +$table->data[] = $row; + +$row = array(); + +// Execution type +$execution_type_fields = array('once' => __('Once'), 'periodically' => __('Periodically')); +$row[] = __('Execution type') . ' ' . html_print_select($execution_type_fields, 'execution_type', $execution_type, '', __('Any'), '', true, false, false); +// Show past downtimes +$row[] = __('Show past downtimes') . ' ' . html_print_checkbox ("archived", 1, $show_archived, true); + +$table->data[] = $row; + +$row = array(); + +// Agent +$params = array(); +$params['show_helptip'] = true; +$params['input_name'] = 'agent_name'; +$params['value'] = $agent_name; +$params['return'] = true; +$params['print_hidden_input_idagent'] = true; +$params['hidden_input_idagent_name'] = 'agent_id'; +$params['hidden_input_idagent_value'] = $agent_id; +$agent_input = __('Agent') . ' ' . ui_print_agent_autocomplete_input($params); +$row[] = $agent_input; + +// Module +$module_input = __('Module') . ' ' . html_print_autocomplete_modules('module_name', $module_name, false, true, '', array(), true); +$row[] = $module_input; + +$row[] = html_print_submit_button('Search', 'search', false, 'class="sub search"', true); + +$table->data[] = $row; + +echo "
"; +html_print_table($table); +echo "
"; // View available downtimes present in database (if any of them) +$table = new StdClass(); $table->class = 'databox'; //Start Overview of existing planned downtime $table->width = '98%'; @@ -194,10 +280,10 @@ $table->head[2] = __('Group'); $table->head[3] = __('Type'); $table->head[4] = __('Execution'); $table->head[5] = __('Configuration'); -$table->head[6] = __('Delete'); -$table->head[7] = __('Update'); -$table->head[8] = __('Running'); -$table->head[9] = __('Stop downtime'); +$table->head[6] = __('Running'); +$table->head[7] = __('Stop downtime'); +$table->head[8] = __('Edit'); +$table->head[9] = __('Delete'); $table->align[2] = "center"; //$table->align[5] = "center"; $table->align[6] = "center"; @@ -205,11 +291,82 @@ $table->align[7] = "center"; $table->align[8] = "center"; $table->align[9] = "center"; +$groups = users_get_groups (); if(!empty($groups)) { + $where_values = "1=1"; + + $groups_string = implode (",", array_keys ($groups)); + $where_values .= " AND id_group IN ($groups_string)"; + + if (!empty($search_text)) { + $where_values .= " AND (name LIKE '%$search_text%' OR description LIKE '%$search_text%')"; + } + + if (!empty($execution_type)) { + $where_values .= " AND type_execution = '$execution_type'"; + } + + if (!empty($date_from)) { + $where_values .= " AND (type_execution = 'periodically' OR (type_execution = 'once' AND date_from >= '".strtotime("$date_from 00:00:00")."'))"; + } + + if (!empty($date_to)) { + $periodically_monthly_w = "type_periodicity = 'monthly' AND (periodically_day_from <= '".date('d', strtotime($date_from))."' AND periodically_time_to >= '".date('d', strtotime($date_to))."')"; + + $periodically_weekly_days = array(); + $date_from_aux = strtotime($date_from); + $date_end = strtotime($date_to); + $days_number = 0; + + while ($date_from_aux <= $date_end && $days_number < 7) { + $weekday_actual = strtolower(date('l', $date_from_aux)); + + $periodically_weekly_days[] = "$weekday_actual = 1"; + + $date_from_aux = $date_from_aux + SECONDS_1DAY; + $days_number++; + } + + $periodically_weekly_w = "type_periodicity = 'weekly' AND (".implode(" OR ", $periodically_weekly_days).")"; + + $periodically_w = "type_execution = 'periodically' AND (($periodically_monthly_w) OR ($periodically_weekly_w))"; + + $once_w = "type_execution = 'once' AND date_to <= '".strtotime("$date_to 23:59:59")."'"; + + $where_values .= " AND (($periodically_w) OR ($once_w))"; + } + + if (!$show_archived) { + $where_values .= " AND (type_execution = 'periodically' OR (type_execution = 'once' AND date_to >= '".time()."'))"; + } + + if (!empty($agent_id)) { + $where_values .= " AND id IN (SELECT id_downtime FROM tplanned_downtime_agents WHERE id_agent = $agent_id)"; + } + + if (!empty($module_id)) { + $where_values .= " AND (id IN (SELECT id_downtime + FROM tplanned_downtime_modules + WHERE id_agent_module = $module_id) + OR id IN (SELECT id_downtime + FROM tplanned_downtime_agents tpda, tagente_modulo tam + WHERE tpda.id_agent = tam.id_agente + AND tam.id_agente_modulo = $module_id + AND tpda.all_modules = 1))"; + } + $sql = "SELECT * - FROM tplanned_downtime - WHERE id_group IN (" . implode (",", array_keys ($groups)) . ")"; + FROM tplanned_downtime + WHERE $where_values + ORDER BY type_execution DESC, date_from DESC + LIMIT ".$config["block_size"]." + OFFSET $offset"; + $sql_count = "SELECT COUNT(id) AS num + FROM tplanned_downtime + WHERE $where_values"; $downtimes = db_get_all_rows_sql ($sql); + $downtimes_number_res = db_get_all_rows_sql($sql_count); + $downtimes_number = $downtimes_number_res != false ? $downtimes_number_res[0]['num'] : 0; } else { $downtimes = array(); @@ -219,6 +376,8 @@ if (!$downtimes) { echo '
'.__('No planned downtime').'
'; } else { + ui_pagination($downtimes_number, "index.php?sec=estado&sec2=godmode/agentes/planned_downtime.list&$filter_params_str", $offset); + foreach ($downtimes as $downtime) { $data = array(); $total = db_get_sql ("SELECT COUNT(id_agent) @@ -295,44 +454,71 @@ else { } if ($downtime["executed"] == 0) { - $data[6] = '' . - html_print_image("images/cross.png", true, array("border" => '0', "alt" => __('Delete'))); - $data[7] = '' . - html_print_image("images/config.png", true, array("border" => '0', "alt" => __('Update'))) . ''; - } - else { - $data[6]= "N/A"; - $data[7]= "N/A"; - - } - if ($downtime["executed"] == 0) { - $data[8] = html_print_image ("images/pixel_red.png", true, + $data[6] = html_print_image ("images/pixel_red.png", true, array ('width' => 20, 'height' => 20, 'alt' => __('Executed'))); } else { - $data[8] = html_print_image ("images/pixel_green.png", true, + $data[6] = html_print_image ("images/pixel_green.png", true, array ('width' => 20, 'height' => 20, 'alt' => __('Not executed'))); } - - if (($downtime['type_execution'] == 'once') - && ($downtime["executed"] == 1)) { + if ($downtime['type_execution'] == 'once' && $downtime["executed"] == 1) { - $data[9] = '' . html_print_image("images/cancel.png", true, array("border" => '0', "alt" => __('Stop downtime'))); } + else { + $data[7] = ""; + } + if ($downtime["executed"] == 0) { + $data[8] = '' . + html_print_image("images/config.png", true, array("border" => '0', "alt" => __('Update'))) . ''; + $data[9] = '' . + html_print_image("images/cross.png", true, array("border" => '0', "alt" => __('Delete'))); + } + else { + $data[8]= "N/A"; + $data[9]= "N/A"; + + } array_push ($table->data, $data); } html_print_table ($table); } echo '
'; -echo '
'; +echo '
'; +echo '
'; +html_print_button(__('Export to CSV'), 'csv_export', false, "location.href='godmode/agentes/planned_downtime.export_csv.php?$filter_params_str'", 'class="sub next"'); +echo '
'; +echo ' '; +echo ''; html_print_submit_button (__('Create'), 'create', false, 'class="sub next"'); echo '
'; echo '
'; + + +ui_require_jquery_file("ui.datepicker-" . get_user_language(), "include/javascript/i18n/"); + +?> + \ No newline at end of file From 732630c37fb81755f1ff811480b41c927cf9c29d Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Wed, 10 Sep 2014 18:58:46 +0200 Subject: [PATCH 03/12] Now the planned downtimes CSV shows the affected elements --- .../agentes/planned_downtime.export_csv.php | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/pandora_console/godmode/agentes/planned_downtime.export_csv.php b/pandora_console/godmode/agentes/planned_downtime.export_csv.php index da4fd6564b..58a89ac5dd 100644 --- a/pandora_console/godmode/agentes/planned_downtime.export_csv.php +++ b/pandora_console/godmode/agentes/planned_downtime.export_csv.php @@ -20,8 +20,6 @@ require_once ("../../include/functions.php"); require_once ("../../include/functions_db.php"); require_once ("../../include/functions_users.php"); require_once ("../../include/functions_groups.php"); -// require_once ("../../include/functions_modules.php"); -// require_once ("../../include/functions_agents.php"); $config["id_user"] = $_SESSION["id_usuario"]; if (! check_acl ($config['id_user'], 0, "AW")) { @@ -43,6 +41,7 @@ $module_id = (int) get_parameter('module_name_hidden'); $module_name = !empty($module_id) ? (string) get_parameter('module_name') : ""; $separator = (string) get_parameter("separator", ";"); +$items_separator = (string) get_parameter("items_separator", ","); // SQL QUERY CREATION $where_values = "1=1"; @@ -129,6 +128,7 @@ if (!empty($downtimes)) { $titles[] = "type"; $titles[] = "execution_type"; $titles[] = "execution_date"; + $titles[] = "affected_items"; echo implode($separator, $titles); echo chr(13); @@ -196,6 +196,39 @@ if (!empty($downtimes)) { } $execution_date = io_safe_output($execution_date); + $affected_items = array(); + + $sql_agents = "SELECT tpda.id_agent AS agent_id, tpda.all_modules AS all_modules, ta.nombre AS agent_name + FROM tplanned_downtime_agents tpda, tagente ta + WHERE tpda.id_downtime = $id + AND tpda.id_agent = ta.id_agente"; + $downtime_agents = @db_get_all_rows_sql($sql_agents); + + if (!empty($downtime_agents)) { + foreach ($downtime_agents as $downtime_agent) { + $downtime_items = array(); + $downtime_items[] = $downtime_agent['agent_name']; + + if (!$downtime_agent['all_modules']) { + $agent_id = $downtime_agent['agent_id']; + $sql_modules = "SELECT tpdm.id_agent_module AS module_id, tam.nombre AS module_name + FROM tplanned_downtime_modules tpdm, tagente_modulo tam + WHERE tpdm.id_downtime = $id + AND tpdm.id_agent = $agent_id + AND tpdm.id_agent_module = tam.id_agente_modulo"; + $downtime_modules = @db_get_all_rows_sql($sql_modules); + + if (!empty($downtime_modules)) { + foreach ($downtime_modules as $downtime_module) { + $downtime_items[] = $downtime_module['module_name']; + } + } + } + $affected_items[] = "[".implode("|", $downtime_items)."]"; + } + } + $affected_items = implode(",", $affected_items); + $values = array(); $values[] = $id; $values[] = $name; @@ -204,6 +237,7 @@ if (!empty($downtimes)) { $values[] = $type; $values[] = $execution_type; $values[] = $execution_date; + $values[] = $affected_items; echo implode($separator, $values); echo chr(13); From fb02de8bea5344df437a1ee65fd7f6a00bd859fb Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 11 Sep 2014 11:42:57 +0200 Subject: [PATCH 04/12] Added support to create scheduled downtimes with past dates * pandora_console/godmode/setup/setup_general.php, pandora_console/include/functions_config.php: Added a config token to allow the creation of scheduled downtimes in the past. * pandora_console/godmode/agentes/planned_downtime.editor.php: Now is possible to create a scheduled downtime with a past date if the config token is activated. --- pandora_console/godmode/agentes/planned_downtime.editor.php | 3 ++- pandora_console/godmode/setup/setup_general.php | 6 ++++++ pandora_console/include/functions_config.php | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index f540de54b0..48a7032dbb 100755 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -25,6 +25,7 @@ if (! check_acl ($config['id_user'], 0, "AW")) { return; } +$config["past_planned_downtimes"] = isset($config["past_planned_downtimes"]) ? $config["past_planned_downtimes"] : 1; require_once ('include/functions_users.php'); @@ -152,7 +153,7 @@ if ($create_downtime || $update_downtime) { $datetime_to = strtotime ($once_date_to . ' ' . $once_time_to); $now = strtotime(date(DATE_FORMAT). ' ' . date(TIME_FORMAT)); - if ($type_execution == 'once' && $datetime_from < $now) { + if (!$config["past_planned_downtimes"] && $type_execution == 'once' && $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) { diff --git a/pandora_console/godmode/setup/setup_general.php b/pandora_console/godmode/setup/setup_general.php index e6582fd756..5cf05f2cf5 100644 --- a/pandora_console/godmode/setup/setup_general.php +++ b/pandora_console/godmode/setup/setup_general.php @@ -263,6 +263,12 @@ $table->data['tutorial_mode'][1] = html_print_select($modes_tutorial, 'tutorial_mode', $config["tutorial_mode"], '', '', 0, true); +$config["past_planned_downtimes"] = isset($config["past_planned_downtimes"]) ? $config["past_planned_downtimes"] : 1; +$table->data[33][0] = __('Allow create planned downtimes in the past') . + ui_print_help_tip(__('The planned downtimes created in the past will affect the SLA reports'), true); +$table->data[33][1] = __('Yes').'   '.html_print_radio_button ('past_planned_downtimes', 1, '', $config["past_planned_downtimes"], true).'  '; +$table->data[33][1] .= __('No').'   '.html_print_radio_button ('past_planned_downtimes', 0, '', $config["past_planned_downtimes"], true); + echo '
'; echo "
"; diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index c42801eabd..b9d564d517 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -184,6 +184,8 @@ function config_update_config () { $error_update[] = __('Server logs directory'); if (!config_update_value ('tutorial_mode', get_parameter('tutorial_mode'))) $error_update[] = __('Tutorial mode'); + if (!config_update_value ('past_planned_downtimes', get_parameter('past_planned_downtimes'))) + $error_update[] = __('Allow create planned downtimes in the past'); break; case 'enterprise': if (isset($config['enterprise_installed']) && $config['enterprise_installed'] == 1) { From 770ccc994e39beb99901874d8d500e3bb668d3c2 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Fri, 12 Sep 2014 11:48:51 +0200 Subject: [PATCH 05/12] Improved the warning messages on the planned downtimes edit section --- .../godmode/agentes/planned_downtime.editor.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index 48a7032dbb..d8e337c738 100755 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -512,7 +512,7 @@ if ($id_downtime > 0) { echo html_print_select (array(), "module[]", '', '', '', 0, false, true, true, '', false, 'width: 180px;'); echo ""; echo "


"; - html_print_submit_button (__('Add'), '', $disabled_add_button, 'class="sub next"',false); + html_print_submit_button (__('Add'), 'add_item', $disabled_add_button, 'class="sub next"',false); echo ""; echo ""; @@ -970,5 +970,18 @@ ui_require_jquery_file("ui.datepicker-" . get_user_language(), "include/javascri $("#id_agent").blur (function () { $(this).css ("width", "180px"); }); + + // Warning message about the problems caused updating a past planned downtime + var type_execution = ""; + var datetime_from = ; + var datetime_now = ; + var create = ; + if (!create && (type_execution == 'periodically' || (type_execution == 'once' && datetime_from < datetime_now))) { + $("input#submit-updbutton, input#submit-add_item, table#list a").click(function (e) { + if (!confirm("")) { + e.preventDefault(); + } + }); + } }); From 8057517acda4dd4c6090ebc52ea43a1e770eb7d2 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Mon, 15 Sep 2014 18:52:29 +0200 Subject: [PATCH 06/12] Fixed the date filter of the list --- pandora_console/godmode/agentes/planned_downtime.list.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandora_console/godmode/agentes/planned_downtime.list.php b/pandora_console/godmode/agentes/planned_downtime.list.php index c9dc4eb5cc..675efc44a6 100755 --- a/pandora_console/godmode/agentes/planned_downtime.list.php +++ b/pandora_console/godmode/agentes/planned_downtime.list.php @@ -311,7 +311,10 @@ if(!empty($groups)) { } if (!empty($date_to)) { - $periodically_monthly_w = "type_periodicity = 'monthly' AND (periodically_day_from <= '".date('d', strtotime($date_from))."' AND periodically_time_to >= '".date('d', strtotime($date_to))."')"; + $periodically_monthly_w = "type_periodicity = 'monthly' + AND ((periodically_day_from <= '".date('d', strtotime($date_from))."' AND periodically_day_to >= '".date('d', strtotime($date_to))."') + OR (periodically_day_from > periodically_day_to + AND (periodically_day_from <= '".date('d', strtotime($date_from))."' OR periodically_day_to >= '".date('d', strtotime($date_to))."')))"; $periodically_weekly_days = array(); $date_from_aux = strtotime($date_from); From 066c30315db0aad06b1fb06c5b0f83675aa8b122 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Mon, 15 Sep 2014 18:53:13 +0200 Subject: [PATCH 07/12] Improved the planned downtimes calculation for the SLA functions Now the functions "reporting_get_agentmodule_sla" and "reporting_get_agentmodule_sla_array" do the planned downtimes calculation correctly. --- .../include/functions_reporting.php | 381 ++++++++++++++++-- 1 file changed, 339 insertions(+), 42 deletions(-) diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index fec59121cb..a9e58903e5 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -478,27 +478,174 @@ function reporting_get_agentmodule_sla ($id_agent_module, $period = 0, $min_valu $interval_data = array (); } - //calculate planned downtime dates - $id_agent = db_get_value('id_agente', 'tagente_modulo', 'id_agente_modulo', $id_agent_module); - $sql_downtime = "SELECT id_downtime FROM tplanned_downtime_agents WHERE id_agent=$id_agent"; - $downtimes = db_get_all_rows_sql($sql_downtime); + // Calculate planned downtime dates + $sql_downtime = "SELECT DISTINCT(tpd.id), tpd.* + FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tplanned_downtime_modules tpdm, tagente_modulo tam + WHERE (tpd.id = tpda.id_downtime + AND tpda.all_modules = 1 + AND tpda.id_agent = tam.id_agente + AND tam.id_agente_modulo = $id_agent_module) + OR (tpd.id = tpdm.id_downtime + AND tpdm.id_agent_module = $id_agent_module)"; + $downtimes = db_get_all_rows_sql($sql_downtime); if ($downtimes == false) { $downtimes = array(); } - $i = 0; $downtime_dates = array(); foreach ($downtimes as $downtime) { - $id_downtime = $downtime['id_downtime']; - $sql_date = "SELECT date_from, date_to FROM tplanned_downtime WHERE id=$id_downtime"; - $date_downtime = db_get_row_sql($sql_date); + $downtime_id = $downtime['id']; + $downtime_type = $downtime['type_execution']; + $downtime_periodicity = $downtime['type_periodicity']; - if ($date_downtime != false) { - $downtime_dates[$i]['date_from'] = $date_downtime['date_from']; - $downtime_dates[$i]['date_to'] = $date_downtime['date_to']; - $i++; + if ($downtime_type == 'once') { + $dates = array(); + $dates['date_from'] = $downtime['date_from']; + $dates['date_to'] = $downtime['date_to']; + $downtime_dates[] = $dates; + } + else if ($downtime_type == 'periodically') { + $downtime_time_from = $downtime['periodically_time_from']; + $downtime_time_to = $downtime['periodically_time_to']; + + $downtime_hour_from = date("H", strtotime($downtime_time_from)); + $downtime_minute_from = date("i", strtotime($downtime_time_from)); + $downtime_second_from = date("s", strtotime($downtime_time_from)); + $downtime_hour_to = date("H", strtotime($downtime_time_to)); + $downtime_minute_to = date("i", strtotime($downtime_time_to)); + $downtime_second_to = date("s", strtotime($downtime_time_to)); + + if ($downtime_periodicity == "monthly") { + $downtime_day_from = $downtime['periodically_day_from']; + $downtime_day_to = $downtime['periodically_day_to']; + + $date_aux = strtotime(date("Y-m-01", $datelimit)); + $year_aux = date("Y", $date_aux); + $month_aux = date("m", $date_aux); + + $end_year = date("Y", $date); + $end_month = date("m", $date); + + while ($year_aux < $end_year || ($year_aux == $end_year && $month_aux <= $end_month)) { + + if ($downtime_day_from > $downtime_day_to) { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime(date("Y-m-t H:i:s", strtotime("$year_aux-$month_aux-28 23:59:59"))); + $downtime_dates[] = $dates; + + $dates = array(); + if ($month_aux + 1 <= 12) { + $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_aux + 1)."-01-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + if ($downtime_day_from == $downtime_day_to && strtotime($downtime_time_from) > strtotime($downtime_time_to)) { + $date_aux_from = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $max_day_num = date('t', $date_aux); + + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_from 23:59:59"); + $downtime_dates[] = $dates; + + if ($downtime_day_to + 1 > $max_day_num) { + + $dates = array(); + if ($month_aux + 1 <= 12) { + $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_aux + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + + $month_aux++; + if ($month_aux > 12) { + $month_aux = 1; + $year_aux++; + } + } + } + else if ($downtime_periodicity == "weekly") { + $date_aux = $datelimit; + $active_days = array(); + $active_days[0] = ($downtime['sunday'] == 1) ? true : false; + $active_days[1] = ($downtime['monday'] == 1) ? true : false; + $active_days[2] = ($downtime['tuesday'] == 1) ? true : false; + $active_days[3] = ($downtime['wednesday'] == 1) ? true : false; + $active_days[4] = ($downtime['thursday'] == 1) ? true : false; + $active_days[5] = ($downtime['friday'] == 1) ? true : false; + $active_days[6] = ($downtime['saturday'] == 1) ? true : false; + + while ($date_aux <= $date) { + $weekday_num = date('w', $date_aux); + + if ($active_days[$weekday_num]) { + $day_num = date('d', $date_aux); + $month_num = date('m', $date_aux); + $year_num = date('Y', $date_aux); + + $max_day_num = date('t', $date_aux); + + if (strtotime($downtime_time_from) > strtotime($downtime_time_to)) { + $dates = array(); + $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_num-$month_num-$day_num 23:59:59"); + $downtime_dates[] = $dates; + + $dates = array(); + if ($day_num + 1 > $max_day_num) { + if ($month_num + 1 > 12) { + $dates['date_from'] = strtotime(($year_num + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_num + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime("$year_num-".($month_num + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_num-".($month_num + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + } + else { + $dates['date_from'] = strtotime("$year_num-$month_num-".($day_num + 1)." 00:00:00"); + $dates['date_to'] = strtotime("$year_num-$month_num-".($day_num + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + + $date_aux += SECONDS_1DAY; + } + } } } - ///// // Get previous data $previous_data = modules_get_previous_data ($id_agent_module, $datelimit); @@ -772,43 +919,193 @@ function reporting_get_agentmodule_sla_array ($id_agent_module, $period = 0, $mi //------------------------------------------------------------------ //--------Calculate planned downtime dates-------------------------- - $id_agent = db_get_value('id_agente', 'tagente_modulo', 'id_agente_modulo', $id_agent_module); - $sql_downtime = "SELECT id_downtime FROM tplanned_downtime_agents WHERE id_agent=$id_agent"; - $downtimes = db_get_all_rows_sql($sql_downtime); + $sql_downtime = "SELECT DISTINCT(tpd.id), tpd.* + FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tplanned_downtime_modules tpdm, tagente_modulo tam + WHERE (tpd.id = tpda.id_downtime + AND tpda.all_modules = 1 + AND tpda.id_agent = tam.id_agente + AND tam.id_agente_modulo = $id_agent_module) + OR (tpd.id = tpdm.id_downtime + AND tpdm.id_agent_module = $id_agent_module)"; + $downtimes = db_get_all_rows_sql($sql_downtime); if ($downtimes == false) { $downtimes = array(); } - $i = 0; $downtime_dates = array(); - foreach ($downtimes as $downtime) { - $id_downtime = $downtime['id_downtime']; - $sql_date = "SELECT date_from, date_to FROM tplanned_downtime WHERE id=$id_downtime"; - $date_downtime = db_get_row_sql($sql_date); + $downtime_id = $downtime['id']; + $downtime_type = $downtime['type_execution']; + $downtime_periodicity = $downtime['type_periodicity']; - if ($date_downtime != false) { - // Delete data of the planned downtime and put the last data on the upper limit - $interval_data_indexed[$date_downtime['date_from']]['data'] = 0; - $interval_data_indexed[$date_downtime['date_from']]['status'] = 5; - - $last_downtime_data = false; - foreach ($interval_data_indexed as $idi_timestamp => $idi) { - if ($idi_timestamp != $date_downtime['date_from'] && $idi_timestamp != $date_downtime['date_to'] && - $idi_timestamp >= $date_downtime['date_from'] && $idi_timestamp <= $date_downtime['date_to']) { - $last_downtime_data = $idi['data']; - unset($interval_data_indexed[$idi_timestamp]); - } + if ($downtime_type == 'once') { + $dates = array(); + $dates['date_from'] = $downtime['date_from']; + $dates['date_to'] = $downtime['date_to']; + $downtime_dates[] = $dates; + } + else if ($downtime_type == 'periodically') { + $downtime_time_from = $downtime['periodically_time_from']; + $downtime_time_to = $downtime['periodically_time_to']; + + $downtime_hour_from = date("H", strtotime($downtime_time_from)); + $downtime_minute_from = date("i", strtotime($downtime_time_from)); + $downtime_second_from = date("s", strtotime($downtime_time_from)); + $downtime_hour_to = date("H", strtotime($downtime_time_to)); + $downtime_minute_to = date("i", strtotime($downtime_time_to)); + $downtime_second_to = date("s", strtotime($downtime_time_to)); + + if ($downtime_periodicity == "monthly") { + $downtime_day_from = $downtime['periodically_day_from']; + $downtime_day_to = $downtime['periodically_day_to']; + + $date_aux = strtotime(date("Y-m-01", $datelimit)); + $year_aux = date("Y", $date_aux); + $month_aux = date("m", $date_aux); + + $end_year = date("Y", $date); + $end_month = date("m", $date); + + while ($year_aux < $end_year || ($year_aux == $end_year && $month_aux <= $end_month)) { + + if ($downtime_day_from > $downtime_day_to) { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime(date("Y-m-t H:i:s", strtotime("$year_aux-$month_aux-28 23:59:59"))); + $downtime_dates[] = $dates; + + $dates = array(); + if ($month_aux + 1 <= 12) { + $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_aux + 1)."-01-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + if ($downtime_day_from == $downtime_day_to && strtotime($downtime_time_from) > strtotime($downtime_time_to)) { + $date_aux_from = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $max_day_num = date('t', $date_aux); + + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_from 23:59:59"); + $downtime_dates[] = $dates; + + if ($downtime_day_to + 1 > $max_day_num) { + + $dates = array(); + if ($month_aux + 1 <= 12) { + $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_aux + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + + $month_aux++; + if ($month_aux > 12) { + $month_aux = 1; + $year_aux++; + } + } } - - // Set the last data of the interval as limit - if ($last_downtime_data !== false) { - $interval_data_indexed[$date_downtime['date_to']]['data'] = $last_downtime_data; - }// If there arent data into the downtime, set unknown - else { - $interval_data_indexed[$date_downtime['date_to']]['data'] = 0; - $interval_data_indexed[$date_downtime['date_to']]['status'] = 4; + else if ($downtime_periodicity == "weekly") { + $date_aux = $datelimit; + $active_days = array(); + $active_days[0] = ($downtime['sunday'] == 1) ? true : false; + $active_days[1] = ($downtime['monday'] == 1) ? true : false; + $active_days[2] = ($downtime['tuesday'] == 1) ? true : false; + $active_days[3] = ($downtime['wednesday'] == 1) ? true : false; + $active_days[4] = ($downtime['thursday'] == 1) ? true : false; + $active_days[5] = ($downtime['friday'] == 1) ? true : false; + $active_days[6] = ($downtime['saturday'] == 1) ? true : false; + + while ($date_aux <= $date) { + $weekday_num = date('w', $date_aux); + + if ($active_days[$weekday_num]) { + $day_num = date('d', $date_aux); + $month_num = date('m', $date_aux); + $year_num = date('Y', $date_aux); + + $max_day_num = date('t', $date_aux); + + if (strtotime($downtime_time_from) > strtotime($downtime_time_to)) { + $dates = array(); + $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_num-$month_num-$day_num 23:59:59"); + $downtime_dates[] = $dates; + + $dates = array(); + if ($day_num + 1 > $max_day_num) { + if ($month_num + 1 > 12) { + $dates['date_from'] = strtotime(($year_num + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_num + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime("$year_num-".($month_num + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_num-".($month_num + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + } + else { + $dates['date_from'] = strtotime("$year_num-$month_num-".($day_num + 1)." 00:00:00"); + $dates['date_to'] = strtotime("$year_num-$month_num-".($day_num + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + + $date_aux += SECONDS_1DAY; + } } - $i++; + } + } + + foreach ($downtime_dates as $downtime_date) { + // Delete data of the planned downtime and put the last data on the upper limit + $interval_data_indexed[$downtime_date['date_from']]['data'] = 0; + $interval_data_indexed[$downtime_date['date_from']]['status'] = 5; + $interval_data_indexed[$downtime_date['date_to']]['data'] = 0; + $interval_data_indexed[$downtime_date['date_to']]['status'] = 4; + + $last_downtime_data = false; + foreach ($interval_data_indexed as $idi_timestamp => $idi) { + if ($idi_timestamp != $downtime_date['date_from'] && $idi_timestamp != $downtime_date['date_to'] && + $idi_timestamp >= $downtime_date['date_from'] && $idi_timestamp <= $downtime_date['date_to']) { + $last_downtime_data = $idi['data']; + unset($interval_data_indexed[$idi_timestamp]); + } + } + + // Set the last data of the interval as limit + if ($last_downtime_data !== false) { + $interval_data_indexed[$downtime_date['date_to']]['data'] = $last_downtime_data; } } //------------------------------------------------------------------ From 02765539624a508b7ddfd27ed5c6ad4b1a1f2a3c Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Tue, 16 Sep 2014 15:33:10 +0200 Subject: [PATCH 08/12] Moved some code to the new function "reporting_get_planned_downtimes_intervals" --- .../include/functions_reporting.php | 525 +++++++----------- 1 file changed, 191 insertions(+), 334 deletions(-) diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index a9e58903e5..23d51636e1 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -479,173 +479,7 @@ function reporting_get_agentmodule_sla ($id_agent_module, $period = 0, $min_valu } // Calculate planned downtime dates - $sql_downtime = "SELECT DISTINCT(tpd.id), tpd.* - FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tplanned_downtime_modules tpdm, tagente_modulo tam - WHERE (tpd.id = tpda.id_downtime - AND tpda.all_modules = 1 - AND tpda.id_agent = tam.id_agente - AND tam.id_agente_modulo = $id_agent_module) - OR (tpd.id = tpdm.id_downtime - AND tpdm.id_agent_module = $id_agent_module)"; - $downtimes = db_get_all_rows_sql($sql_downtime); - if ($downtimes == false) { - $downtimes = array(); - } - $downtime_dates = array(); - foreach ($downtimes as $downtime) { - $downtime_id = $downtime['id']; - $downtime_type = $downtime['type_execution']; - $downtime_periodicity = $downtime['type_periodicity']; - - if ($downtime_type == 'once') { - $dates = array(); - $dates['date_from'] = $downtime['date_from']; - $dates['date_to'] = $downtime['date_to']; - $downtime_dates[] = $dates; - } - else if ($downtime_type == 'periodically') { - $downtime_time_from = $downtime['periodically_time_from']; - $downtime_time_to = $downtime['periodically_time_to']; - - $downtime_hour_from = date("H", strtotime($downtime_time_from)); - $downtime_minute_from = date("i", strtotime($downtime_time_from)); - $downtime_second_from = date("s", strtotime($downtime_time_from)); - $downtime_hour_to = date("H", strtotime($downtime_time_to)); - $downtime_minute_to = date("i", strtotime($downtime_time_to)); - $downtime_second_to = date("s", strtotime($downtime_time_to)); - - if ($downtime_periodicity == "monthly") { - $downtime_day_from = $downtime['periodically_day_from']; - $downtime_day_to = $downtime['periodically_day_to']; - - $date_aux = strtotime(date("Y-m-01", $datelimit)); - $year_aux = date("Y", $date_aux); - $month_aux = date("m", $date_aux); - - $end_year = date("Y", $date); - $end_month = date("m", $date); - - while ($year_aux < $end_year || ($year_aux == $end_year && $month_aux <= $end_month)) { - - if ($downtime_day_from > $downtime_day_to) { - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime(date("Y-m-t H:i:s", strtotime("$year_aux-$month_aux-28 23:59:59"))); - $downtime_dates[] = $dates; - - $dates = array(); - if ($month_aux + 1 <= 12) { - $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); - $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - else { - $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); - $dates['date_to'] = strtotime(($year_aux + 1)."-01-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - $downtime_dates[] = $dates; - } - else { - if ($downtime_day_from == $downtime_day_to && strtotime($downtime_time_from) > strtotime($downtime_time_to)) { - $date_aux_from = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $max_day_num = date('t', $date_aux); - - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_from 23:59:59"); - $downtime_dates[] = $dates; - - if ($downtime_day_to + 1 > $max_day_num) { - - $dates = array(); - if ($month_aux + 1 <= 12) { - $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); - $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - else { - $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); - $dates['date_to'] = strtotime(($year_aux + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - $downtime_dates[] = $dates; - } - else { - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." 00:00:00"); - $dates['date_to'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - $downtime_dates[] = $dates; - } - } - else { - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - $downtime_dates[] = $dates; - } - } - - $month_aux++; - if ($month_aux > 12) { - $month_aux = 1; - $year_aux++; - } - } - } - else if ($downtime_periodicity == "weekly") { - $date_aux = $datelimit; - $active_days = array(); - $active_days[0] = ($downtime['sunday'] == 1) ? true : false; - $active_days[1] = ($downtime['monday'] == 1) ? true : false; - $active_days[2] = ($downtime['tuesday'] == 1) ? true : false; - $active_days[3] = ($downtime['wednesday'] == 1) ? true : false; - $active_days[4] = ($downtime['thursday'] == 1) ? true : false; - $active_days[5] = ($downtime['friday'] == 1) ? true : false; - $active_days[6] = ($downtime['saturday'] == 1) ? true : false; - - while ($date_aux <= $date) { - $weekday_num = date('w', $date_aux); - - if ($active_days[$weekday_num]) { - $day_num = date('d', $date_aux); - $month_num = date('m', $date_aux); - $year_num = date('Y', $date_aux); - - $max_day_num = date('t', $date_aux); - - if (strtotime($downtime_time_from) > strtotime($downtime_time_to)) { - $dates = array(); - $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_num-$month_num-$day_num 23:59:59"); - $downtime_dates[] = $dates; - - $dates = array(); - if ($day_num + 1 > $max_day_num) { - if ($month_num + 1 > 12) { - $dates['date_from'] = strtotime(($year_num + 1)."-01-01 00:00:00"); - $dates['date_to'] = strtotime(($year_num + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - else { - $dates['date_from'] = strtotime("$year_num-".($month_num + 1)."-01 00:00:00"); - $dates['date_to'] = strtotime("$year_num-".($month_num + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - } - else { - $dates['date_from'] = strtotime("$year_num-$month_num-".($day_num + 1)." 00:00:00"); - $dates['date_to'] = strtotime("$year_num-$month_num-".($day_num + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - $downtime_dates[] = $dates; - } - else { - $dates = array(); - $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - $downtime_dates[] = $dates; - } - } - - $date_aux += SECONDS_1DAY; - } - } - } - } + $downtime_dates = reporting_get_planned_downtimes_intervals($id_agent_module, $datelimit, $date, true); // Get previous data $previous_data = modules_get_previous_data ($id_agent_module, $datelimit); @@ -919,173 +753,7 @@ function reporting_get_agentmodule_sla_array ($id_agent_module, $period = 0, $mi //------------------------------------------------------------------ //--------Calculate planned downtime dates-------------------------- - $sql_downtime = "SELECT DISTINCT(tpd.id), tpd.* - FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tplanned_downtime_modules tpdm, tagente_modulo tam - WHERE (tpd.id = tpda.id_downtime - AND tpda.all_modules = 1 - AND tpda.id_agent = tam.id_agente - AND tam.id_agente_modulo = $id_agent_module) - OR (tpd.id = tpdm.id_downtime - AND tpdm.id_agent_module = $id_agent_module)"; - $downtimes = db_get_all_rows_sql($sql_downtime); - if ($downtimes == false) { - $downtimes = array(); - } - $downtime_dates = array(); - foreach ($downtimes as $downtime) { - $downtime_id = $downtime['id']; - $downtime_type = $downtime['type_execution']; - $downtime_periodicity = $downtime['type_periodicity']; - - if ($downtime_type == 'once') { - $dates = array(); - $dates['date_from'] = $downtime['date_from']; - $dates['date_to'] = $downtime['date_to']; - $downtime_dates[] = $dates; - } - else if ($downtime_type == 'periodically') { - $downtime_time_from = $downtime['periodically_time_from']; - $downtime_time_to = $downtime['periodically_time_to']; - - $downtime_hour_from = date("H", strtotime($downtime_time_from)); - $downtime_minute_from = date("i", strtotime($downtime_time_from)); - $downtime_second_from = date("s", strtotime($downtime_time_from)); - $downtime_hour_to = date("H", strtotime($downtime_time_to)); - $downtime_minute_to = date("i", strtotime($downtime_time_to)); - $downtime_second_to = date("s", strtotime($downtime_time_to)); - - if ($downtime_periodicity == "monthly") { - $downtime_day_from = $downtime['periodically_day_from']; - $downtime_day_to = $downtime['periodically_day_to']; - - $date_aux = strtotime(date("Y-m-01", $datelimit)); - $year_aux = date("Y", $date_aux); - $month_aux = date("m", $date_aux); - - $end_year = date("Y", $date); - $end_month = date("m", $date); - - while ($year_aux < $end_year || ($year_aux == $end_year && $month_aux <= $end_month)) { - - if ($downtime_day_from > $downtime_day_to) { - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime(date("Y-m-t H:i:s", strtotime("$year_aux-$month_aux-28 23:59:59"))); - $downtime_dates[] = $dates; - - $dates = array(); - if ($month_aux + 1 <= 12) { - $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); - $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - else { - $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); - $dates['date_to'] = strtotime(($year_aux + 1)."-01-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - $downtime_dates[] = $dates; - } - else { - if ($downtime_day_from == $downtime_day_to && strtotime($downtime_time_from) > strtotime($downtime_time_to)) { - $date_aux_from = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $max_day_num = date('t', $date_aux); - - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_from 23:59:59"); - $downtime_dates[] = $dates; - - if ($downtime_day_to + 1 > $max_day_num) { - - $dates = array(); - if ($month_aux + 1 <= 12) { - $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); - $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - else { - $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); - $dates['date_to'] = strtotime(($year_aux + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - $downtime_dates[] = $dates; - } - else { - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." 00:00:00"); - $dates['date_to'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - $downtime_dates[] = $dates; - } - } - else { - $dates = array(); - $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - $downtime_dates[] = $dates; - } - } - - $month_aux++; - if ($month_aux > 12) { - $month_aux = 1; - $year_aux++; - } - } - } - else if ($downtime_periodicity == "weekly") { - $date_aux = $datelimit; - $active_days = array(); - $active_days[0] = ($downtime['sunday'] == 1) ? true : false; - $active_days[1] = ($downtime['monday'] == 1) ? true : false; - $active_days[2] = ($downtime['tuesday'] == 1) ? true : false; - $active_days[3] = ($downtime['wednesday'] == 1) ? true : false; - $active_days[4] = ($downtime['thursday'] == 1) ? true : false; - $active_days[5] = ($downtime['friday'] == 1) ? true : false; - $active_days[6] = ($downtime['saturday'] == 1) ? true : false; - - while ($date_aux <= $date) { - $weekday_num = date('w', $date_aux); - - if ($active_days[$weekday_num]) { - $day_num = date('d', $date_aux); - $month_num = date('m', $date_aux); - $year_num = date('Y', $date_aux); - - $max_day_num = date('t', $date_aux); - - if (strtotime($downtime_time_from) > strtotime($downtime_time_to)) { - $dates = array(); - $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_num-$month_num-$day_num 23:59:59"); - $downtime_dates[] = $dates; - - $dates = array(); - if ($day_num + 1 > $max_day_num) { - if ($month_num + 1 > 12) { - $dates['date_from'] = strtotime(($year_num + 1)."-01-01 00:00:00"); - $dates['date_to'] = strtotime(($year_num + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - else { - $dates['date_from'] = strtotime("$year_num-".($month_num + 1)."-01 00:00:00"); - $dates['date_to'] = strtotime("$year_num-".($month_num + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - } - else { - $dates['date_from'] = strtotime("$year_num-$month_num-".($day_num + 1)." 00:00:00"); - $dates['date_to'] = strtotime("$year_num-$month_num-".($day_num + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - } - $downtime_dates[] = $dates; - } - else { - $dates = array(); - $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); - $dates['date_to'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); - $downtime_dates[] = $dates; - } - } - - $date_aux += SECONDS_1DAY; - } - } - } - } + $downtime_dates = reporting_get_planned_downtimes_intervals($id_agent_module, $datelimit, $date, true); foreach ($downtime_dates as $downtime_date) { // Delete data of the planned downtime and put the last data on the upper limit @@ -1203,6 +871,195 @@ function reporting_get_agentmodule_sla_array ($id_agent_module, $period = 0, $mi return $data_colors; } +/** + * Get the time intervals where an agentmodule is affected by the planned downtimes. + * + * @param int Agent module to calculate planned downtimes intervals. + * @param int Start date in utimestamp. + * @param int End date in utimestamp. + * @param bool Whether ot not to get the planned downtimes that affect the service associated with the agentmodule. + * + * @return Array with time intervals. + */ +function reporting_get_planned_downtimes_intervals ($id_agent_module, $start_date, $end_date, $check_services = false) { + $sql_downtime = "SELECT DISTINCT(tpd.id), tpd.* + FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tplanned_downtime_modules tpdm, tagente_modulo tam + WHERE (tpd.id = tpda.id_downtime + AND tpda.all_modules = 1 + AND tpda.id_agent = tam.id_agente + AND tam.id_agente_modulo = $id_agent_module) + OR (tpd.id = tpdm.id_downtime + AND tpdm.id_agent_module = $id_agent_module)"; + $downtimes = db_get_all_rows_sql($sql_downtime); + if ($downtimes == false) { + $downtimes = array(); + } + $downtime_dates = array(); + foreach ($downtimes as $downtime) { + $downtime_id = $downtime['id']; + $downtime_type = $downtime['type_execution']; + $downtime_periodicity = $downtime['type_periodicity']; + + if ($downtime_type == 'once') { + $dates = array(); + $dates['date_from'] = $downtime['date_from']; + $dates['date_to'] = $downtime['date_to']; + $downtime_dates[] = $dates; + } + else if ($downtime_type == 'periodically') { + $downtime_time_from = $downtime['periodically_time_from']; + $downtime_time_to = $downtime['periodically_time_to']; + + $downtime_hour_from = date("H", strtotime($downtime_time_from)); + $downtime_minute_from = date("i", strtotime($downtime_time_from)); + $downtime_second_from = date("s", strtotime($downtime_time_from)); + $downtime_hour_to = date("H", strtotime($downtime_time_to)); + $downtime_minute_to = date("i", strtotime($downtime_time_to)); + $downtime_second_to = date("s", strtotime($downtime_time_to)); + + if ($downtime_periodicity == "monthly") { + $downtime_day_from = $downtime['periodically_day_from']; + $downtime_day_to = $downtime['periodically_day_to']; + + $date_aux = strtotime(date("Y-m-01", $start_date)); + $year_aux = date("Y", $date_aux); + $month_aux = date("m", $date_aux); + + $end_year = date("Y", $end_date); + $end_month = date("m", $end_date); + + while ($year_aux < $end_year || ($year_aux == $end_year && $month_aux <= $end_month)) { + + if ($downtime_day_from > $downtime_day_to) { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime(date("Y-m-t H:i:s", strtotime("$year_aux-$month_aux-28 23:59:59"))); + $downtime_dates[] = $dates; + + $dates = array(); + if ($month_aux + 1 <= 12) { + $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_aux + 1)."-01-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + if ($downtime_day_from == $downtime_day_to && strtotime($downtime_time_from) > strtotime($downtime_time_to)) { + $date_aux_from = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $max_day_num = date('t', $date_aux); + + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_from 23:59:59"); + $downtime_dates[] = $dates; + + if ($downtime_day_to + 1 > $max_day_num) { + + $dates = array(); + if ($month_aux + 1 <= 12) { + $dates['date_from'] = strtotime("$year_aux-".($month_aux + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime(($year_aux + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_aux + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." 00:00:00"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + + $month_aux++; + if ($month_aux > 12) { + $month_aux = 1; + $year_aux++; + } + } + } + else if ($downtime_periodicity == "weekly") { + $date_aux = $start_date; + $active_days = array(); + $active_days[0] = ($downtime['sunday'] == 1) ? true : false; + $active_days[1] = ($downtime['monday'] == 1) ? true : false; + $active_days[2] = ($downtime['tuesday'] == 1) ? true : false; + $active_days[3] = ($downtime['wednesday'] == 1) ? true : false; + $active_days[4] = ($downtime['thursday'] == 1) ? true : false; + $active_days[5] = ($downtime['friday'] == 1) ? true : false; + $active_days[6] = ($downtime['saturday'] == 1) ? true : false; + + while ($date_aux <= $end_date) { + $weekday_num = date('w', $date_aux); + + if ($active_days[$weekday_num]) { + $day_num = date('d', $date_aux); + $month_num = date('m', $date_aux); + $year_num = date('Y', $date_aux); + + $max_day_num = date('t', $date_aux); + + if (strtotime($downtime_time_from) > strtotime($downtime_time_to)) { + $dates = array(); + $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_num-$month_num-$day_num 23:59:59"); + $downtime_dates[] = $dates; + + $dates = array(); + if ($day_num + 1 > $max_day_num) { + if ($month_num + 1 > 12) { + $dates['date_from'] = strtotime(($year_num + 1)."-01-01 00:00:00"); + $dates['date_to'] = strtotime(($year_num + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + else { + $dates['date_from'] = strtotime("$year_num-".($month_num + 1)."-01 00:00:00"); + $dates['date_to'] = strtotime("$year_num-".($month_num + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + } + else { + $dates['date_from'] = strtotime("$year_num-$month_num-".($day_num + 1)." 00:00:00"); + $dates['date_to'] = strtotime("$year_num-$month_num-".($day_num + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + } + $downtime_dates[] = $dates; + } + else { + $dates = array(); + $dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from"); + $dates['date_to'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_to:$downtime_minute_to:$downtime_second_to"); + $downtime_dates[] = $dates; + } + } + + $date_aux += SECONDS_1DAY; + } + } + } + } + + if ($check_services) { + enterprise_include_once("include/functions_services.php"); + if (function_exists("services_get_planned_downtimes_intervals")) { + services_get_planned_downtimes_intervals($downtime_dates, $start_date, $end_date, false, $id_agent_module); + } + } + + return $downtime_dates; +} + function reporting_get_stats_servers($tiny = true) { global $config; From 4db6a4ee9cc8f9502536dcd064152a0a3cd96e49 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 18 Sep 2014 15:31:42 +0200 Subject: [PATCH 09/12] Added the function "reporting_get_planned_downtimes". * pandora_console/include/functions_reporting.php: The new function "reporting_get_planned_downtimes" return a planned downtimes array with the planned downtimes that affect the passed modules on an specific datetime range. Improved two function calls on the function "reporting_render_report_html_item". --- .../include/functions_reporting.php | 148 +++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 23d51636e1..850424e65f 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -1060,6 +1060,148 @@ function reporting_get_planned_downtimes_intervals ($id_agent_module, $start_dat return $downtime_dates; } +/** + * Get the planned downtimes that affect the passed modules on an specific datetime range. + * + * @param int Start date in utimestamp. + * @param int End date in utimestamp. + * @param array The agent modules ids. + * + * @return Array with the planned downtimes that are executed in any moment of the range selected and affect the + * agent modules selected. + */ +function reporting_get_planned_downtimes ($start_date, $end_date, $id_agent_modules = false) { + $start_time = date("H:i:s", $start_date); + $end_time = date("H:i:s", $end_date); + + $start_day = date("d", $start_date); + $end_day = date("d", $end_date); + + $start_month = date("m", $start_date); + $end_month = date("m", $end_date); + + if ($start_date > $end_date) { + return false; + } + + if ($end_date - $start_date >= SECONDS_1MONTH) { + // If the date range is larger than 1 month, every monthly planned downtime will be inside + $periodically_monthly_w = "type_periodicity = 'monthly'"; + } + else { + // Check if the range is larger than the planned downtime execution, or if its start or end + // is inside the planned downtime execution. + // The start and end time is very important. + $periodically_monthly_w = "type_periodicity = 'monthly' + AND (((periodically_day_from > '$start_day' + OR (periodically_day_from = '$start_day' + AND periodically_time_from > '$start_time')) + AND (periodically_day_to < '$end_day' + OR (periodically_day_to = '$end_day' + AND periodically_time_to < '$end_time'))) + OR (periodically_day_from < '$start_day' + OR (periodically_day_from = '$start_day' + AND periodically_time_from < '$start_time') + AND (periodically_day_to > '$start_day' + OR (periodically_day_to = '$start_day' + AND periodically_time_to > '$start_time'))) + OR (periodically_day_from < '$end_day' + OR (periodically_day_from = '$end_day' + AND periodically_time_from < '$end_time') + AND (periodically_day_to > '$end_day' + OR (periodically_day_to = '$end_day' + AND periodically_time_to > '$end_time'))))"; + } + + $periodically_weekly_days = array(); + $date_aux = $start_date; + $i = 0; + + if (($end_date - $start_date) >= SECONDS_1WEEK) { + // If the date range is larger than 7 days, every weekly planned downtime will be inside. + for ($i = 0; $i < 7; $i++) { + $weekday_actual = strtolower(date('l', $date_aux)); + $periodically_weekly_days[] = "($weekday_actual = 1)"; + $date_aux += SECONDS_1DAY; + } + } + else if (($end_date - $start_date) <= SECONDS_1DAY && $start_day == $end_day) { + // If the date range is smaller than 1 day, the start and end days can be equal or consecutive. + // If they are equal, the execution times have to be contained in the date range times or contain + // the start or end time of the date range. + $weekday_actual = strtolower(date('l', $start_date)); + $periodically_weekly_days[] = "($weekday_actual = 1 + AND ((periodically_time_from > '$start_time' AND periodically_time_to < '$end_time') + OR (periodically_time_from = '$start_time' + OR (periodically_time_from < '$start_time' + AND periodically_time_to >= '$start_time')) + OR (periodically_time_from = '$end_time' + OR (periodically_time_from < '$end_time' + AND periodically_time_to >= '$end_time'))))"; + } + else { + while ($date_aux <= $end_date && $i < 7) { + + $weekday_actual = strtolower(date('l', $date_aux)); + $day_num_actual = date('d', $date_aux); + + if ($date_aux == $start_date) { + $periodically_weekly_days[] = "($weekday_actual = 1 AND periodically_time_to >= '$start_time')"; + } + else if ($day_num_actual == $end_day) { + $periodically_weekly_days[] = "($weekday_actual = 1 AND periodically_time_from <= '$end_time')"; + } + else { + $periodically_weekly_days[] = "($weekday_actual = 1)"; + } + + $date_aux += SECONDS_1DAY; + $i++; + } + } + + if (!empty($periodically_weekly_days)) { + $periodically_weekly_w = "type_periodicity = 'weekly' AND (".implode(" OR ", $periodically_weekly_days).")"; + $periodically_condition = "(($periodically_monthly_w) OR ($periodically_weekly_w))"; + } + else { + $periodically_condition = "($periodically_monthly_w)"; + } + + if (!empty($id_agent_modules)) { + $id_agent_modules_str = implode(",", $id_agent_modules); + $agent_modules_condition_tpda = "AND tam.id_agente_modulo IN ($id_agent_modules_str)"; + $agent_modules_condition_tpdm = "AND tpdm.id_agent_module IN ($id_agent_modules_str)"; + } + else { + $agent_modules_condition_tpda = ""; + $agent_modules_condition_tpdm = ""; + } + + $sql_downtime = "SELECT DISTINCT(tpd.id), tpd.* + FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tplanned_downtime_modules tpdm, tagente_modulo tam + WHERE ((tpd.id = tpda.id_downtime + AND tpda.all_modules = 1 + AND tpda.id_agent = tam.id_agente + $agent_modules_condition_tpda) + OR (tpd.id = tpdm.id_downtime + $agent_modules_condition_tpdm) + AND ((type_execution = 'periodically' + AND $periodically_condition) + OR (type_execution = 'once' + AND ((date_from >= '$start_date' AND date_to <= '$end_date') + OR (date_from <= '$start_date' AND date_to >= '$end_date') + OR (date_from <= '$start_date' AND date_to >= '$start_date') + OR (date_from <= '$end_date' AND date_to >= '$end_date'))))"; + + $downtimes = db_get_all_rows_sql($sql_downtime); + if ($downtimes == false) { + $downtimes = array(); + } + + return $downtimes; +} + function reporting_get_stats_servers($tiny = true) { global $config; @@ -3521,10 +3663,12 @@ function reporting_render_report_html_item ($content, $table, $report, $mini = f break; case 'SLA_monthly': - reporting_enterprise_sla_monthly($mini, $content, $report, $table, $item_title); + if (function_exists("reporting_enterprise_sla_monthly")) + reporting_enterprise_sla_monthly($mini, $content, $report, $table, $item_title); break; case 'SLA_services': - reporting_enterprise_sla_services($mini, $content, $report, $table, $item_title); + if (function_exists("reporting_enterprise_sla_services")) + reporting_enterprise_sla_services($mini, $content, $report, $table, $item_title); break; case 3: case 'SLA': From 2e1f0f964dd51cf942f9483b96fdb934305b52be Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 18 Sep 2014 19:36:39 +0200 Subject: [PATCH 10/12] Improved some functions * pandora_console/include/functions_reporting.php: Fixed the function "reporting_get_planned_downtimes". Added a table to the SLA reports which shows the planned downtimes that affect the SLA report elements. --- .../include/functions_reporting.php | 117 +++++++++++++++++- 1 file changed, 111 insertions(+), 6 deletions(-) diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 850424e65f..afeefd2eda 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -1099,15 +1099,15 @@ function reporting_get_planned_downtimes ($start_date, $end_date, $id_agent_modu AND (periodically_day_to < '$end_day' OR (periodically_day_to = '$end_day' AND periodically_time_to < '$end_time'))) - OR (periodically_day_from < '$start_day' + OR ((periodically_day_from < '$start_day' OR (periodically_day_from = '$start_day' - AND periodically_time_from < '$start_time') + AND periodically_time_from < '$start_time')) AND (periodically_day_to > '$start_day' OR (periodically_day_to = '$start_day' AND periodically_time_to > '$start_time'))) - OR (periodically_day_from < '$end_day' + OR ((periodically_day_from < '$end_day' OR (periodically_day_from = '$end_day' - AND periodically_time_from < '$end_time') + AND periodically_time_from < '$end_time')) AND (periodically_day_to > '$end_day' OR (periodically_day_to = '$end_day' AND periodically_time_to > '$end_time'))))"; @@ -1185,7 +1185,7 @@ function reporting_get_planned_downtimes ($start_date, $end_date, $id_agent_modu AND tpda.id_agent = tam.id_agente $agent_modules_condition_tpda) OR (tpd.id = tpdm.id_downtime - $agent_modules_condition_tpdm) + $agent_modules_condition_tpdm)) AND ((type_execution = 'periodically' AND $periodically_condition) OR (type_execution = 'once' @@ -1193,7 +1193,7 @@ function reporting_get_planned_downtimes ($start_date, $end_date, $id_agent_modu OR (date_from <= '$start_date' AND date_to >= '$end_date') OR (date_from <= '$start_date' AND date_to >= '$start_date') OR (date_from <= '$end_date' AND date_to >= '$end_date'))))"; - + $downtimes = db_get_all_rows_sql($sql_downtime); if ($downtimes == false) { $downtimes = array(); @@ -3956,10 +3956,115 @@ function reporting_render_report_html_item ($content, $table, $report, $mini = f array_push ($table->data, $data); $table->colspan[$next_row][0] = 3; + $next_row++; $data = array(); $data[0] = html_print_table($tableslice, true); array_push ($table->data, $data); } + + // Table Planned Downtimes + $id_agent_modules = array(); + foreach ($slas as $sla) { + if (!empty($sla['id_agent_module'])) + $id_agent_modules[] = $sla['id_agent_module']; + } + $planned_downtimes = reporting_get_planned_downtimes(($report['datetime']-$content['period']), $report['datetime'], $id_agent_modules); + + if (!empty($planned_downtimes)) { + + $table_planned_downtimes = new StdClass(); + $table_planned_downtimes->width = '100%'; + $table_planned_downtimes->title = __('This SLA has been affected by the following planned downtimes'); + $table_planned_downtimes->head = array(); + $table_planned_downtimes->head[0] = __('Name'); + $table_planned_downtimes->head[1] = __('Description'); + $table_planned_downtimes->head[2] = __('Execution'); + $table_planned_downtimes->head[3] = __('Dates'); + $table_planned_downtimes->headstyle = array(); + $table_planned_downtimes->style = array(); + $table_planned_downtimes->data = array(); + + if ($for_pdf) { + $table_planned_downtimes->titlestyle = 'background: #373737; color: #FFF; display: table-cell; font-size: 12px; border: 1px solid grey'; + $table_planned_downtimes->class = 'table_sla table_beauty'; + + for ($i = 0; $i < count($table_planned_downtimes->head); $i++) { + $table_planned_downtimes->headstyle[$i] = 'background: #666; color: #FFF; display: table-cell; font-size: 11px; border: 1px solid grey'; + } + for ($i = 0; $i < count($table_planned_downtimes->head); $i++) { + $table_planned_downtimes->style[$i] = 'display: table-cell; font-size: 10px;'; + } + } + + foreach ($planned_downtimes as $planned_downtime) { + $data = array(); + $data[0] = $planned_downtime['name']; + $data[1] = $planned_downtime['description']; + $data[2] = ucfirst($planned_downtime['type_execution']); + + switch ($planned_downtime['type_execution']) { + case 'once': + $data[3] = date ("Y-m-d H:i", $planned_downtime['date_from']) . + " " . __('to') . " ". + date ("Y-m-d H:i", $planned_downtime['date_to']); + break; + case 'periodically': + switch ($planned_downtime['type_periodicity']) { + case 'weekly': + $data[3] = __('Weekly:'); + $data[3] .= " "; + if ($planned_downtime['monday']) { + $data[3] .= __('Mon'); + $data[3] .= " "; + } + if ($planned_downtime['tuesday']) { + $data[3] .= __('Tue'); + $data[3] .= " "; + } + if ($planned_downtime['wednesday']) { + $data[3] .= __('Wed'); + $data[3] .= " "; + } + if ($planned_downtime['thursday']) { + $data[3] .= __('Thu'); + $data[3] .= " "; + } + if ($planned_downtime['friday']) { + $data[3] .= __('Fri'); + $data[3] .= " "; + } + if ($planned_downtime['saturday']) { + $data[3] .= __('Sat'); + $data[3] .= " "; + } + if ($planned_downtime['sunday']) { + $data[3] .= __('Sun'); + $data[3] .= " "; + } + $data[3] .= " (" . $planned_downtime['periodically_time_from']; + $data[3] .= "-" . $planned_downtime['periodically_time_to'] . ")"; + break; + case 'monthly': + $data[3] = __('Monthly:') . " "; + $data[3] .= __('From day') . " " . $planned_downtime['periodically_day_from']; + $data[3] .= " " . strtolower(__('To day')) . " "; + $data[3] .= $planned_downtime['periodically_day_to']; + $data[3] .= " (" . $planned_downtime['periodically_time_from']; + $data[3] .= "-" . $planned_downtime['periodically_time_to'] . ")"; + break; + } + break; + } + + $table_planned_downtimes->data[] = $data; + } + + $data = array(); + $data[0] = html_print_table($table_planned_downtimes, true); + $table->colspan[$next_row][0] = 3; + $next_row++; + array_push ($table->data, $data); + } break; case 6: case 'monitor_report': From 43a8dc0219b500a900f5ae1e3a364c5590fe564c Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Fri, 19 Sep 2014 17:27:29 +0200 Subject: [PATCH 11/12] Added new help files to explain the datetime configuration of a planned downtime --- .../help/en/help_planned_downtime_time.php | 31 +++++++++++++++++++ .../help/es/help_planned_downtime_time.php | 31 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 pandora_console/include/help/en/help_planned_downtime_time.php create mode 100644 pandora_console/include/help/es/help_planned_downtime_time.php diff --git a/pandora_console/include/help/en/help_planned_downtime_time.php b/pandora_console/include/help/en/help_planned_downtime_time.php new file mode 100644 index 0000000000..7c151cd8fb --- /dev/null +++ b/pandora_console/include/help/en/help_planned_downtime_time.php @@ -0,0 +1,31 @@ + +

Scheduled downtimes. Time and date configuration

+ +

Once execution

+ +

+ The date format must be year/month/day and the time format must be hour:minute:second. + It's possible to create a scheduled downtime with a past date, if that option aren't disabled by the admin of Pandora FMS. +

+ +

Periodically execution

+ +

Monthly

+ +

+ The downtime will be executed every month, from the start day at the start time, to the end date at the end time selected. + The time format must be hour:minute:second and the start day can't be lower than the end day. + To reflect a downtime which ends away than the last day of the month, it's necessary to create two scheduled downtimes, the first should ends the day 31 at 23:59:59 and the other should start the day 1 at 00:00:00. +

+ +

Weekly

+ +

+ The downtime will be executed every selected day, from the start time, to the end time selected. + The start time can't be lower than the end time. + To reflect a downtime which ends away than the last day time, it's necessary to create two scheduled downtimes, the first should end at 23:59:59 and the other should start at 00:00:00 on the next day. +

\ No newline at end of file diff --git a/pandora_console/include/help/es/help_planned_downtime_time.php b/pandora_console/include/help/es/help_planned_downtime_time.php new file mode 100644 index 0000000000..cb4af40bfc --- /dev/null +++ b/pandora_console/include/help/es/help_planned_downtime_time.php @@ -0,0 +1,31 @@ + +

Desconexiones programadas. Configuración de tiempo y fecha

+ +

Ejecución única

+ +

+ El formato de la fecha debe ser año/mes/día y el del tiempo hora:minuto:segundo. + Se pueden crear paradas planificadas en fechas pasadas, siempre que el administrador de Pandora FMS no haya deshabilitado esa opción. +

+ +

Ejecución periódica

+ +

Mensual

+ +

+ La parada se ejecutará cada mes, desde el día de inicio a la hora de inicio, hasta el día final a la hora final indicados. + El formato del tiempo debe ser hora:minuto:segundo y el día de inicio no puede ser menor al día final. + Para reflejar una parada que va más allá del último día del mes, habría que crear dos paradas, una que terminase el día 31 a las 23:59:59 y otra que empezase el día 1 a las 00:00:00. +

+ +

Semanal

+ +

+ La parada se ejecutará cada día seleccionado, desde la hora de inicio a la hora final indicadas. + La hora de inicio no puede ser superior a la hora final. + Para reflejar una parada que va más allá de la última hora del día, habría que crear dos paradas, una que terminase a las 23:59:59 y otra que empezase a las 00:00:00 del día siguiente. +

\ No newline at end of file From 6f6debb3242a993f1f0180d9eeebf1860b234421 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Fri, 19 Sep 2014 17:30:17 +0200 Subject: [PATCH 12/12] Error fixes and improvements. Improved the help tips of the planned downtimes editor --- .../agentes/planned_downtime.editor.php | 41 ++++++++++++------- .../include/functions_reporting.php | 12 +++--- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index d8e337c738..d5455f22f4 100755 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -52,7 +52,7 @@ $once_time_to = (string) get_parameter ('once_time_to', date(TIME_FORMAT)); $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)); -$periodically_time_to = (string) get_parameter ('periodically_time_to', date(TIME_FORMAT)); +$periodically_time_to = (string) get_parameter ('periodically_time_to', date(TIME_FORMAT, time() + SECONDS_1HOUR)); $first_create = (int) get_parameter ('first_create', 0); @@ -153,12 +153,19 @@ if ($create_downtime || $update_downtime) { $datetime_to = strtotime ($once_date_to . ' ' . $once_time_to); $now = strtotime(date(DATE_FORMAT). ' ' . date(TIME_FORMAT)); - if (!$config["past_planned_downtimes"] && $type_execution == 'once' && $datetime_from < $now) { + 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' ). - ': START >= END'); + ui_print_error_message(__('Not created. Error inserting data') . ". " .__('The end date must be higher than the start date')); + } + else if ($type_execution == 'periodically' + && (($type_periodicity == 'weekly' && $periodically_time_from >= $periodically_time_to) + || ($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 = ''; @@ -324,35 +331,35 @@ $table->data[3][1] = html_print_select(array('quiet' => __('Quiet'), 'type_downtime', $type_downtime, 'change_type_downtime()', '', 0, true, false, true, ''); $table->data[4][0] = __('Execution'); -$table->data[4][1] = html_print_select(array('once' => __('once'), +$table->data[4][1] = html_print_select(array('once' => __('Once'), 'periodically' => __('Periodically')), 'type_execution', $type_execution, 'change_type_execution();', '', 0, true); $days = array_combine(range(1, 31), range(1, 31)); -$table->data[5][0] = __('Configure the time'); +$table->data[5][0] = __('Configure the time') . " " . ui_print_help_icon ('planned_downtime_time', true);; $table->data[5][1] = "