1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
6// Please see http://pandorafms.org for full contribution list
7
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU General Public License
10// as published by the Free Software Foundation for version 2.
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16global $config;
17
18// Load global vars
19require_once ("include/config.php");
20require_once ("include/functions_agents.php");
21require_once ("include/functions_reporting.php");
22require_once ('include/functions_modules.php');
23require_once ('include/functions_users.php');
24
25check_login();
26
27if (!check_acl ($config['id_user'], 0, "RR")) {
28	require ("general/noaccess.php");
29	return;
30}
31
32ui_require_javascript_file ('calendar');
33
34
35// Header
36ui_print_page_header (__("Export data"), "images/server_export_mc.png");
37
38$group = get_parameter_post ('group', 0);
39$agentName = get_parameter_post ('agent', 0);
40
41switch ($config["dbtype"]) {
42	case "mysql":
43		$agents = agents_get_agents(
44			array('nombre LIKE "' . $agentName . '"'), array ('id_agente'));
45		break;
46	case "postgresql":
47		$agents = agents_get_agents(
48			array('nombre LIKE \'' . $agentName . '\''), array ('id_agente'));
49		break;
50	case "oracle":
51		$agents = agents_get_agents(
52			array('nombre LIKE \'%' . $agentName . '%\''), array ('id_agente'));
53		break;
54}
55$agent = $agents[0]['id_agente'];
56
57$module = (array) get_parameter_post ('module_arr', array ());
58$start_date = get_parameter_post ('start_date', 0);
59$end_date = get_parameter_post ('end_date', 0);
60$start_time = get_parameter_post ('start_time', 0);
61$end_time = get_parameter_post ('end_time', 0);
62$export_type = get_parameter_post ('export_type', 'data');
63$export_btn = get_parameter ('export_btn', 0);
64
65$show_form = false;
66
67if (!empty ($export_btn) && !empty ($module)) {
68
69	// Disable SQL cache
70	global $sql_cache;
71	$sql_cache = array ('saved' => 0);
72
73
74	//Convert start time and end time to unix timestamps
75	$start = strtotime ($start_date . " " . $start_time);
76	$end = strtotime ($end_date . " " . $end_time);
77	$period = $end - $start;
78	$data = array ();
79
80	//If time is negative or zero, don't process - it's invalid
81	if ($start < 1 || $end < 1) {
82		ui_print_error_message (__('Invalid time specified'));
83		return;
84	}
85
86	//******************************************************************
87	// Starts, ends and dividers
88	//******************************************************************
89	switch ($export_type) {
90		case "data":
91		case "avg":
92		default:
93			//HTML output - don't style or use XHTML just in case somebody needs to copy/paste it. (Office doesn't handle <thead> and <tbody>)
94			$datastart = '<table style="width:100%;" class="databox data">' .
95				'<tr>' .
96					'<th>' . __('Agent') . '</th>' .
97					'<th>' . __('Module') . '</th>' .
98					'<th>' . __('Data') . '</th>' .
99					'<th>' .__('Timestamp') . '</th>' .
100				'</tr>';
101			$rowstart = '<tr><td>';
102			$divider = '</td><td>';
103			$rowend = '</td></tr>';
104			$dataend = '</table>';
105			break;
106	}
107
108	//******************************************************************
109	// Data processing
110	//******************************************************************
111	$data = array ();
112	switch ($export_type) {
113		case "data":
114		case "avg":
115			// Show header
116			echo $datastart;
117
118			foreach ($module as $selected) {
119
120				$output = "";
121				$work_period = SECONDS_1DAY;
122				if ($work_period > $period) {
123					$work_period = $period;
124				}
125
126				$work_end = $end - $period + $work_period;
127				$work_start = $end - $work_period;
128				//Buffer to get data, anyway this will report a memory exhaustin
129
130				$flag_last_time_slice = false;
131				while ($work_end <= $end) {
132
133					$data = array (); // Reinitialize array for each module chunk
134
135					if ($export_type == "avg") {
136						$arr = array ();
137						$arr["data"] =
138							reporting_get_agentmodule_data_average(
139								$selected, $work_period, $work_end);
140						if ($arr["data"] === false) {
141							$work_end = $work_end + $work_period;
142							continue;
143						}
144						$arr["module_name"] = modules_get_agentmodule_name ($selected);
145						$arr["agent_name"] = modules_get_agentmodule_agent_name ($selected);
146						$arr["agent_id"] = modules_get_agentmodule_agent ($selected);
147						$arr["utimestamp"] = $end;
148						array_push ($data, $arr);
149					}
150					else {
151						$data_single = modules_get_agentmodule_data(
152							$selected, $work_period, $work_end);
153
154						if (!empty ($data_single)) {
155							$data = array_merge ($data, $data_single);
156						}
157					}
158
159
160
161					foreach ($data as $key => $module) {
162						$output .= $rowstart;
163						$output .= io_safe_output($module['agent_name']);
164						$output .= $divider;
165						$output .= io_safe_output($module['module_name']);
166						$output .= $divider;
167						$output .= $module['data'];
168						$output .= $divider;
169						$output .= date ("Y-m-d G:i:s", $work_start ) . ' - ' . date ("Y-m-d G:i:s", $module['utimestamp']);
170						$output .= $rowend;
171					}
172
173					switch ($export_type) {
174						default:
175						case "data":
176						case "avg":
177							echo $output;
178							break;
179					}
180					unset($output);
181					$output = "";
182					unset($data);
183					unset($data_single);
184
185					// The last time slice is executed now exit of
186					// while loop
187					if ($flag_last_time_slice)
188						breaK;
189
190					if (($work_end  + $work_period) > $end || $work_period == 0) {
191						// Get the last timelapse
192						$work_period = $end - $work_end;
193						$work_end = $end;
194						$flag_last_time_slice = true;
195					}
196					else {
197						$work_end = $work_end + $work_period;
198					}
199				}
200				unset ($output);
201				$output = "";
202			} // main foreach
203
204			echo $dataend;
205			break;
206	}
207}
208elseif (!empty ($export_btn) && empty ($module)) {
209	ui_print_error_message (__('No modules specified'));
210	$show_form = true;
211}
212
213if (empty($export_btn) || $show_form) {
214	echo '<form method="post" action="index.php?sec=reporting&amp;sec2=operation/agentes/exportdata" name="export_form" id="export_form">';
215
216	$table->width = '100%';
217	$table->border = 0;
218	$table->cellspacing = 3;
219	$table->cellpadding = 5;
220	$table->class = "databox filters";
221	$table->style[0] = 'vertical-align: top;';
222
223	$table->data = array ();
224
225	//Group selector
226	$table->data[0][0] = '<b>' . __('Group') . '</b>';
227
228	$groups = users_get_groups ($config['id_user'], "RR", users_can_manage_group_all());
229
230	$table->data[0][1] = html_print_select_groups($config['id_user'],
231		"RR", users_can_manage_group_all(), "group", $group, '', '', 0, true, false, true,
232		'w130', false);
233
234	//Agent selector
235	$table->data[1][0] = '<b>'.__('Source agent').'</b>';
236
237	if ($group > 0) {
238		$filter['id_grupo'] = (array) $group;
239	}
240	else {
241		$filter['id_grupo'] = array_keys ($groups);
242	}
243
244	$agents = array ();
245	$rows = agents_get_agents ($filter, false, 'RR');
246	if ($rows == null) $rows = array();
247	foreach ($rows as $row) {
248		$agents[$row['id_agente']] = $row['nombre'];
249	}
250
251	//Src code of lightning image with skins
252	$src_code = html_print_image ('images/lightning.png', true, false, true);
253
254	$params = array();
255	$params['return'] = true;
256	$params['show_helptip'] = true;
257	$params['input_name'] = 'agent';
258	$params['selectbox_group'] = 'group';
259	$params['value'] = agents_get_name ($agent);
260	$params['javascript_is_function_select'] = true;
261	$params['add_none_module'] = false;
262	$params['selectbox_id'] = 'module_arr';
263	$table->data[1][1] = ui_print_agent_autocomplete_input($params);
264
265	//Module selector
266	$table->data[2][0] = '<b>'.__('Modules').'</b>';
267	$table->data[2][0] .= ui_print_help_tip(__("No modules of type string. You can not calculate their average"),true);
268
269	if ($agent > 0) {
270		$modules = agents_get_modules ($agent);
271	}
272	else {
273		$modules = array ();
274	}
275
276	if (!empty($modules)) { //remove modules of type string because you cant calculate their average.
277		$i = 0;
278		foreach ($modules as $key=>$module) {
279			$id_module_type = modules_get_agentmodule_type ($key);
280			switch ($id_module_type) {
281				case 3:
282				case 10:
283				case 17:
284				case 23:
285				case 33:
286					unset($modules[$i]);
287					break;
288			}
289			$i++;
290		}
291	}
292
293	$disabled_export_button = false;
294	if (empty($modules)) {
295		$disabled_export_button = true;
296	}
297
298	$table->data[2][1] = html_print_select ($modules, "module_arr[]", array_keys ($modules), '', '', 0, true, true, true, 'w155', false);
299
300	//Start date selector
301	$table->data[3][0] = '<b>'.__('Begin date').'</b>';
302
303	$table->data[3][1] = html_print_input_text ('start_date',
304		date ("Y-m-d", get_system_time () - SECONDS_1DAY), false, 10, 10, true);
305	$table->data[3][1] .= html_print_image ("images/calendar_view_day.png", true, array ("alt" => "calendar", "onclick" => "scwShow(scwID('text-start_date'),this);"));
306	$table->data[3][1] .= html_print_input_text ('start_time',
307		date ("H:i:s", get_system_time () - SECONDS_1DAY), false, 10, 9, true);
308
309	//End date selector
310	$table->data[4][0] = '<b>'.__('End date').'</b>';
311	$table->data[4][1] = html_print_input_text ('end_date',
312		date ("Y-m-d", get_system_time ()), false, 10, 10, true);
313	$table->data[4][1] .= html_print_image ("images/calendar_view_day.png", true, array ("alt" => "calendar", "onclick" => "scwShow(scwID('text-end_date'),this);"));
314	$table->data[4][1] .= html_print_input_text ('end_time',
315		date ("H:i:s", get_system_time ()), false, 10, 9, true);
316
317	//Export type
318	$table->data[5][0] = '<b>'.__('Export type').'</b>';
319
320	$export_types = array ();
321	$export_types["data"] = __('Data table');
322	$export_types["csv"] = __('CSV');
323	$export_types["excel"] = __('MS Excel');
324	$export_types["avg"] = __('Average per hour/day');
325
326	$table->data[5][1] = html_print_select ($export_types, "export_type", $export_type, '', '', 0, true, false, true, 'w130', false);
327
328	html_print_table ($table);
329
330	// Submit button
331	echo '<div class="action-buttons" style="width:100%;">';
332		html_print_button (__('Export'), 'export_btn', false, 'change_action()', 'class="sub wand"');
333	echo '</div></form>';
334}
335ui_require_jquery_file ('pandora.controls');
336ui_require_jquery_file ('ajaxqueue');
337ui_require_jquery_file ('bgiframe');
338?>
339<script type="text/javascript">
340	/* <![CDATA[ */
341	function change_action() {
342		type = $("#export_type").val();
343		var f = document.forms.export_form;
344
345		switch (type) {
346			case 'csv':
347				f.action = "operation/agentes/exportdata.csv.php";
348				break;
349			case 'excel':
350				f.action = "operation/agentes/exportdata.excel.php";
351				break;
352			case 'avg':
353			case 'data':
354				f.action = "index.php?sec=reporting&sec2=operation/agentes/exportdata&export_btn=1";
355				break;
356		}
357		$("#export_form").submit();
358	}
359	/* ]]> */
360</script>
361