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
16// Load global vars
17check_login ();
18
19if (! check_acl ($config['id_user'], 0, "AW")) {
20	db_pandora_audit("ACL Violation",
21		"Trying to access massive agent deletion section");
22	require ("general/noaccess.php");
23	return;
24}
25
26require_once ('include/functions_agents.php');
27require_once ('include/functions_ui.php');
28require_once ('include/functions_alerts.php');
29require_once ('include/functions_modules.php');
30require_once ('include/functions_servers.php');
31require_once ('include/functions_gis.php');
32require_once ('include/functions_users.php');
33
34if (is_ajax ()) {
35	$get_n_conf_files = (bool) get_parameter ('get_n_conf_files');
36	if ($get_n_conf_files) {
37		$id_agents = get_parameter('id_agents');
38		$cont = 0;
39		foreach ($id_agents as $id_agent) {
40			$name = agents_get_name($id_agent);
41			$agent_md5 = md5($name);
42			if (file_exists ($config["remote_config"]."/md5/".$agent_md5.".md5"))
43				$cont ++;
44		}
45		echo $cont;
46		return;
47	}
48}
49
50$update_agents = get_parameter('update_agents', 0);
51$recursion = get_parameter('recursion');
52
53if ($update_agents) {
54	$values = array();
55	if (get_parameter ('group', '') != -1)
56		$values['id_grupo'] = get_parameter ('group');
57	if (get_parameter ('interval', 0) != 0)
58		$values['intervalo'] = get_parameter ('interval');
59	if (get_parameter ('id_os', '') != -1)
60		$values['id_os'] = get_parameter ('id_os');
61	if (get_parameter ('id_parent', '') != '')
62		$values['id_parent'] = agents_get_agent_id(get_parameter('id_parent'));
63	if (get_parameter ('server_name', '') != -1)
64		$values['server_name'] = get_parameter ('server_name');
65	if (get_parameter ('description', '') != '')
66		$values['comentarios'] = get_parameter ('description');
67	if (get_parameter ('mode', '') != -1)
68		$values['modo'] = get_parameter ('mode');
69	if (get_parameter ('disabled', '') != -1)
70		$values['disabled'] = get_parameter ('disabled');
71	if (get_parameter ('icon_path', '') != '')
72		$values['icon_path'] = get_parameter('icon_path');
73	if (get_parameter ('update_gis_data', -1) != -1)
74		$values['update_gis_data'] = get_parameter('update_gis_data');
75	if (get_parameter ('custom_id', '') != '')
76		$values['custom_id'] = get_parameter('custom_id');
77	if (get_parameter ('cascade_protection', -1) != -1)
78		$values['cascade_protection'] = get_parameter('cascade_protection');
79	if (get_parameter ('delete_conf', 0) != 0)
80		$values['delete_conf'] = get_parameter('delete_conf');
81	if (get_parameter('quiet_select', -1) != -1)
82		$values['quiet'] = get_parameter('quiet_select');
83
84	$fields = db_get_all_fields_in_table('tagent_custom_fields');
85
86	if ($fields === false) $fields = array();
87
88	$id_agents = get_parameter('id_agents', false);
89	if (!$id_agents) {
90		ui_print_error_message(__('No agents selected'));
91		$id_agents = array();
92	}
93	else {
94		if (empty($values) && empty($fields)) {
95			ui_print_error_message(__('No values changed'));
96			$id_agents = array();
97		}
98	}
99
100	// CONF FILE DELETION
101	if (isset($values['delete_conf'])) {
102		unset($values['delete_conf']);
103		$n_deleted = 0;
104		foreach ($id_agents as $id_agent) {
105			$agent_md5 = md5(agents_get_name($id_agent));
106			@unlink($config["remote_config"] .
107				"/md5/" . $agent_md5 . ".md5");
108			$result = @unlink($config["remote_config"] .
109				"/conf/" . $agent_md5 . ".conf");
110
111			$n_deleted += (int)$result;
112		}
113
114
115		if ($n_deleted > 0) {
116			db_pandora_audit("Massive management", "Delete conf file " . $id_agent);
117		}
118		else {
119			db_pandora_audit("Massive management", "Try to delete conf file " . $id_agent);
120		}
121
122
123		ui_print_result_message ($n_deleted > 0,
124			__('Configuration files deleted successfully') . '(' . $n_deleted . ')',
125			__('Configuration files cannot be deleted'));
126	}
127
128	if (empty($values) && empty($fields)) {
129		$id_agents = array();
130	}
131
132	$n_edited = 0;
133	$result = false;
134	foreach ($id_agents as $id_agent) {
135		if (!empty($values)) {
136			if ($config['metaconsole_agent_cache'] == 1 &&
137			    isset($values['id_grupo'])) {
138				$values['update_module_count'] = 1; // Force an update of the agent cache.
139			}
140			$result = db_process_sql_update ('tagente',
141				$values,
142				array ('id_agente' => $id_agent));
143		}
144
145		// Update Custom Fields
146		foreach ($fields as $field) {
147			if (get_parameter_post ('customvalue_' . $field['id_field'], '') != '') {
148				$key = $field['id_field'];
149				$value = get_parameter_post ('customvalue_' . $field['id_field'], '');
150
151				$old_value = db_get_all_rows_filter('tagent_custom_data', array('id_agent' => $id_agent, 'id_field' => $key));
152
153				if ($old_value === false) {
154					// Create custom field if not exist
155					$result = db_process_sql_insert ('tagent_custom_data',
156						array('id_field' => $key,
157							'id_agent' => $id_agent,
158							'description' => $value));
159				}
160				else {
161					$result = db_process_sql_update ('tagent_custom_data',
162						array('description' => $value),
163						array('id_field' => $key,'id_agent' => $id_agent));
164				}
165			}
166		}
167
168		$n_edited += (int)$result;
169	}
170
171
172	if ($result !== false) {
173		db_pandora_audit("Massive management", "Update agent " . $id_agent, false, false, json_encode($fields));
174	}
175	else {
176		if (isset ($id_agent)) {
177			db_pandora_audit("Massive management", "Try to update agent " . $id_agent, false, false, json_encode($fields));
178		}
179	}
180
181
182	ui_print_result_message ($result !== false,
183		__('Agents updated successfully') . '(' . $n_edited . ')',
184		__('Agents cannot be updated'));
185}
186$id_group = 0;
187
188$groups = users_get_groups();
189
190$table = new StdClass();
191$table->id = 'delete_table';
192$table->class = 'databox filters';
193$table->width = '100%';
194$table->data = array ();
195$table->style = array ();
196$table->style[0] = 'font-weight: bold;';
197$table->style[2] = 'font-weight: bold';
198$table->size = array ();
199$table->size[0] = '15%';
200$table->size[1] = '35%';
201$table->size[2] = '15%';
202$table->size[3] = '35%';
203
204$table->data = array ();
205$table->data[0][0] = __('Group');
206$table->data[0][1] = html_print_select_groups(false, "AW", true,
207	'id_group', $id_group, false, '', '', true);
208$table->data[0][2] = __('Group recursion');
209$table->data[0][3] = html_print_checkbox ("recursion", 1, $recursion,
210	true, false);
211
212
213$status_list = array ();
214$status_list[AGENT_STATUS_NORMAL] = __('Normal');
215$status_list[AGENT_STATUS_WARNING] = __('Warning');
216$status_list[AGENT_STATUS_CRITICAL] = __('Critical');
217$status_list[AGENT_STATUS_UNKNOWN] = __('Unknown');
218$status_list[AGENT_STATUS_NOT_NORMAL] = __('Not normal');
219$status_list[AGENT_STATUS_NOT_INIT] = __('Not init');
220$table->data[1][0] = __('Status');
221$table->data[1][1] = html_print_select($status_list, 'status_agents', 'selected',
222	'', __('All'), AGENT_STATUS_ALL, true);
223
224$table->data[2][0] = __('Agents');
225$table->data[2][0] .= '<span id="agent_loading" class="invisible">';
226$table->data[2][0] .= html_print_image('images/spinner.png', true);
227$table->data[2][0] .= '</span>';
228$enabled_agents = agents_get_group_agents (array_keys (users_get_groups ($config["id_user"], "AW", false)), array('disabled' => 0), "none");
229$all_agents = agents_get_group_agents (array_keys (users_get_groups ($config["id_user"], "AW", false)), array('disabled' => 1), "none") + $enabled_agents;
230
231$table->data[2][1] = html_print_select ($all_agents,
232	'id_agents[]', 0, false, '', '', true, true);
233
234echo '<form method="post" id="form_agent" action="index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=edit_agents">';
235html_print_table ($table);
236
237$nombre_agente = "";
238$direccion_agente = "";
239$id_agente = 0;
240$id_parent = 0;
241$cascade_protection = 0;
242$group = 0;
243$interval = '';
244$id_os = 0;
245$server_name = 0;
246$description = "";
247
248echo '<div id="form_agents" style="display: none;">';
249
250$table = new StdClass();
251$table->width = '100%';
252$table->class = "databox filters";
253
254$table->head = array ();
255$table->style = array ();
256$table->style[0] = 'font-weight: bold; width: 150px;';
257$table->size[0] = '15%';
258$table->size[1] = '35%';
259$table->size[2] = '15%';
260$table->size[3] = '35%';
261
262$table->data = array ();
263
264$groups = users_get_groups ($config["id_user"], "AW",false);
265$agents = agents_get_group_agents (array_keys ($groups));
266
267$table->data[0][0] = __('Parent');
268$params = array();
269$params['return'] = true;
270$params['show_helptip'] = true;
271$params['input_name'] = 'id_parent';
272$params['value'] = agents_get_name ($id_parent);
273$table->data[0][1] = ui_print_agent_autocomplete_input($params);
274
275$table->data[0][1] .= "<b>" . __('Cascade protection'). "</b>&nbsp;" .
276			ui_print_help_icon("cascade_protection", true) .
277					html_print_select(array(1 => __('Yes'), 0 => __('No')),
278	"cascade_protection", -1, "", __('No change'), -1, true);
279
280$table->data[1][0] = __('Group');
281$table->data[1][1] = html_print_select_groups(false, "AR", false, 'group', $group, '', __('No change'), -1, true, false, true, '', false, 'width: 150px;');
282
283$table->data[2][0] = __('Interval');
284
285$table->data[2][1] = html_print_extended_select_for_time ('interval', 0, '', __('No change'), '0', 10, true, 'width: 150px');
286
287$table->data[3][0] = __('OS');
288$table->data[3][1] = html_print_select_from_sql ('SELECT id_os, name FROM tconfig_os',
289	'id_os', $id_os, '', __('No change'), -1, true, false, true, false, 'width: 105px;');
290$table->data[3][1] .= ' <span id="os_preview">';
291$table->data[3][1] .= ui_print_os_icon ($id_os, false, true);
292$table->data[3][1] .= '</span>';
293
294// Network server
295$none = '';
296if ($server_name == '' && $id_agente)
297	$none = __('None');
298$table->data[4][0] = __('Server');
299$table->data[4][1] = html_print_select (servers_get_names (),
300	'server_name', $server_name, '', __('No change'), -1, true, false, true, '', false, 'width: 150px;');
301
302// Description
303$table->data[5][0] = __('Description');
304$table->data[5][1] = html_print_input_text ('description', $description, '', 45, 255, true);
305
306html_print_table ($table);
307unset($table);
308
309$custom_id = '';
310$mode = -1;
311$disabled = -1;
312$new_agent = true;
313$icon_path = '';
314$update_gis_data = -1;
315$cascade_protection = -1;
316$quiet_select = -1;
317
318$table = new StdClass();
319$table->width = '100%';
320$table->class = "databox filters";
321
322$table->size[0] = '15%';
323$table->size[1] = '35%';
324$table->size[2] = '15%';
325$table->size[3] = '35%';
326
327$table->head = array ();
328$table->style = array ();
329$table->style[0] = 'font-weight: bold; width: 150px;';
330$table->data = array ();
331
332// Custom ID
333$table->data[0][0] = __('Custom ID');
334$table->data[0][1] = html_print_input_text ('custom_id', $custom_id, '', 16, 255, true);
335
336// Learn mode / Normal mode
337$table->data[1][0] = __('Module definition') . ui_print_help_icon("module_definition", true);
338$table->data[1][1] = __('No change').' '.html_print_radio_button_extended ("mode", -1, '', $mode, false, '', 'style="margin-right: 40px;"', true);
339$table->data[1][1] .= __('Learning mode').' '.html_print_radio_button_extended ("mode", 1, '', $mode, false, '', 'style="margin-right: 40px;"', true);
340$table->data[1][1] .= __('Normal mode').' '.html_print_radio_button_extended ("mode", 0, '', $mode, false, '', 'style="margin-right: 40px;"', true);
341
342// Status (Disabled / Enabled)
343$table->data[2][0] = __('Status');
344$table->data[2][1] = __('No change').' '.html_print_radio_button_extended ("disabled", -1, '', $disabled, false, '', 'style="margin-right: 40px;"', true);
345$table->data[2][1] .= __('Disabled').' '.html_print_radio_button_extended ("disabled", 1, '', $disabled, false, '', 'style="margin-right: 40px;"', true);
346$table->data[2][1] .= __('Active').' '.html_print_radio_button_extended ("disabled", 0, '', $disabled, false, '', 'style="margin-right: 40px;"', true);
347
348// Remote configuration
349$table->data[3][0] = __('Remote configuration');
350
351// Delete remote configuration
352$table->data[3][1] = '<div id="delete_configurations" style="display: none">'. __('Delete available remote configurations').' (';
353$table->data[3][1] .= '<span id="n_configurations"></span>';
354$table->data[3][1] .= ') '.html_print_checkbox_extended ("delete_conf", 1, 0, false, '', 'style="margin-right: 40px;"', true).'</div>';
355
356$table->data[3][1] .= '<div id="not_available_configurations" style="display: none"><em>'.__('Not available').'</em></div>';
357
358$listIcons = gis_get_array_list_icons();
359
360$arraySelectIcon = array();
361foreach ($listIcons as $index => $value) $arraySelectIcon[$index] = $index;
362
363$path = 'images/gis_map/icons/'; //TODO set better method the path
364if ($icon_path == '') {
365	$display_icons = 'none';
366	// Hack to show no icon. Use any given image to fix not found image errors
367	$path_without = "images/spinner.png";
368	$path_default = "images/spinner.png";
369	$path_ok = "images/spinner.png";
370	$path_bad = "images/spinner.png";
371	$path_warning = "images/spinner.png";
372}
373else {
374	$display_icons = '';
375	$path_without = $path . $icon_path . ".default.png";
376	$path_default = $path . $icon_path . ".default.png";
377	$path_ok = $path . $icon_path . ".ok.png";
378	$path_bad = $path . $icon_path . ".bad.png";
379	$path_warning = $path . $icon_path . ".warning.png";
380}
381
382$table->data[4][0] = __('Agent icon');
383$table->data[4][1] = html_print_select($arraySelectIcon, "icon_path", $icon_path, "changeIcons();", __('No change'), '', true) .
384	'&nbsp;' . __('Without status') . ': ' . html_print_image($path_without, true, array("id" => 'icon_without_status',"style" => 'display:'.$display_icons.';')) .
385	'&nbsp;' . __('Default') . ': ' . html_print_image($path_default, true, array("id" => 'icon_default',"style" => 'display:'.$display_icons.';')) .
386	'&nbsp;' . __('Ok') . ': ' .  html_print_image($path_ok, true, array("id" => 'icon_ok',"style" => 'display:'.$display_icons.';')) .
387	'&nbsp;' . __('Bad') . ': ' . html_print_image($path_bad, true, array("id" => 'icon_bad',"style" => 'display:'.$display_icons.';')) .
388	'&nbsp;' . __('Warning') . ': ' .  html_print_image($path_warning, true, array("id" => 'icon_warning',"style" => 'display:'.$display_icons.';'));
389
390if ($config['activate_gis']) {
391	$table->data[5][0] = __('Ignore new GIS data:');
392	$table->data[5][1] = __('No change').' '.html_print_radio_button_extended ("update_gis_data", -1, '', $update_gis_data, false, '', 'style="margin-right: 40px;"', true);
393	$table->data[5][1] .= __('Yes').' '.html_print_radio_button_extended ("update_gis_data", 0, '', $update_gis_data, false, '', 'style="margin-right: 40px;"', true);
394	$table->data[5][1] .= __('No').' '.html_print_radio_button_extended ("update_gis_data", 1, '', $update_gis_data, false, '', 'style="margin-right: 40px;"', true);
395}
396
397$table->data[6][0] = __('Quiet');
398$table->data[6][0] .= ui_print_help_tip(__('The agent still runs but the alerts and events will be stop'), true);
399$table->data[6][1] = html_print_select(array(-1 => __('No change'),
400	1 => __('Yes'), 0 => __('No')),
401	"quiet_select", $quiet_select, "", '', 0, true);
402
403ui_toggle(html_print_table ($table, true), __('Advanced options'));
404unset($table);
405
406$table = new StdClass();
407$table->width = '100%';
408$table->class = "databox filters";
409
410$table->head = array ();
411$table->style = array ();
412$table->style[0] = 'font-weight: bold; width: 150px;';
413$table->data = array ();
414$table->size[0] = '15%';
415$table->size[1] = '35%';
416$table->size[2] = '15%';
417$table->size[3] = '35%';
418
419$fields = db_get_all_fields_in_table('tagent_custom_fields');
420
421if ($fields === false) $fields = array();
422
423foreach ($fields as $field) {
424
425	$data[0] = '<b>'.$field['name'].'</b>';
426
427	$custom_value = db_get_value_filter('description', 'tagent_custom_data', array('id_field' => $field['id_field'], 'id_agent' => $id_agente));
428
429	if ($custom_value === false) {
430		$custom_value = '';
431	}
432
433	$data[1] = html_print_textarea ('customvalue_'.$field['id_field'], 2, 65, $custom_value, 'style="min-height: 30px;"', true);
434
435	array_push ($table->data, $data);
436}
437
438if (!empty($fields)) {
439	ui_toggle(html_print_table ($table, true), __('Custom fields'));
440}
441
442
443echo '<h3 class="error invisible" id="message"> </h3>';
444
445echo '<div class="action-buttons" style="width: '.$table->width.'">';
446
447html_print_submit_button (__('Update'), 'updbutton', false, 'class="sub upd"');
448html_print_input_hidden ('update_agents', 1);
449html_print_input_hidden ('id_agente', $id_agente);
450
451echo "</div>"; // Shown and hide div
452
453echo '</div></form>';
454
455ui_require_jquery_file ('form');
456ui_require_jquery_file ('pandora.controls');
457
458
459ui_require_jquery_file ('pandora.controls');
460ui_require_jquery_file ('ajaxqueue');
461ui_require_jquery_file ('bgiframe');
462?>
463<script type="text/javascript">
464/* <![CDATA[ */
465
466var limit_parameters_massive = <?php echo $config['limit_parameters_massive']; ?>;
467
468//Use this function for change 3 icons when change the selectbox
469$(document).ready (function () {
470
471	$("#form_agent").submit(function() {
472		var get_parameters_count = window.location.href.slice(
473			window.location.href.indexOf('?') + 1).split('&').length;
474		var post_parameters_count = $("#form_agent").serializeArray().length;
475
476		var count_parameters =
477			get_parameters_count + post_parameters_count;
478
479		if (count_parameters > limit_parameters_massive) {
480			alert("<?php echo __('Unsucessful sending the data, please contact with your administrator or make with less elements.'); ?>");
481			return false;
482		}
483	});
484
485
486	$("#id_agents").change (function () {
487		var idAgents = Array();
488		jQuery.each ($("#id_agents option:selected"), function (i, val) {
489			idAgents.push($(val).val());
490		});
491		jQuery.post ("ajax.php",
492				{"page" : "godmode/massive/massive_edit_agents",
493				"get_n_conf_files" : 1,
494				"id_agents[]" : idAgents
495				},
496				function (data, status) {
497					if (data == 0) {
498						$("#delete_configurations").attr("style", "display: none");
499						$("#not_available_configurations").attr("style", "");
500					}
501					else {
502						$("#n_configurations").text(data);
503						$("#not_available_configurations").attr("style", "display: none");
504						$("#delete_configurations").attr("style", "");
505					}
506				},
507				"json"
508			);
509
510		$("#form_agents").attr("style", "");
511	});
512
513	$("#id_group").change (function () {
514		$("#form_agents").attr("style", "display: none");
515	});
516
517	$("select#id_os").pandoraSelectOS ();
518
519	var recursion;
520	$("#checkbox-recursion").click(function () {
521		recursion = this.checked ? 1 : 0;
522		$("#id_group").trigger("change");
523	});
524
525	$("#id_group").pandoraSelectGroupAgent ({
526		agentSelect: "select#id_agents",
527		privilege: "AW",
528		status_agents: function () {
529				return $("#status_agents").val();
530			},
531		recursion: function() {return recursion}
532	});
533
534	$("#status_agents").change(function() {
535		$("#id_group").trigger("change");
536	});
537
538	$("#id_group").pandoraSelectGroupAgentDisabled ({
539		agentSelect: "select#id_agents",
540		recursion: function() {return recursion}
541	});
542});
543
544function changeIcons() {
545	icon = $("#icon_path :selected").val();
546
547	$("#icon_without_status").attr("src", "images/spinner.png");
548	$("#icon_default").attr("src", "images/spinner.png");
549	$("#icon_ok").attr("src", "images/spinner.png");
550	$("#icon_bad").attr("src", "images/spinner.png");
551	$("#icon_warning").attr("src", "images/spinner.png");
552
553	if (icon.length == 0) {
554		$("#icon_without_status").attr("style", "display:none;");
555		$("#icon_default").attr("style", "display:none;");
556		$("#icon_ok").attr("style", "display:none;");
557		$("#icon_bad").attr("style", "display:none;");
558		$("#icon_warning").attr("style", "display:none;");
559	}
560	else {
561		$("#icon_without_status").attr("src",
562			"<?php echo $path; ?>" + icon + ".default.png");
563		$("#icon_default").attr("src",
564			"<?php echo $path; ?>" + icon + ".default.png");
565		$("#icon_ok").attr("src",
566			"<?php echo $path; ?>" + icon + ".ok.png");
567		$("#icon_bad").attr("src",
568			"<?php echo $path; ?>" + icon + ".bad.png");
569		$("#icon_warning").attr("src",
570			"<?php echo $path; ?>" + icon + ".warning.png");
571		$("#icon_without_status").attr("style", "");
572		$("#icon_default").attr("style", "");
573		$("#icon_ok").attr("style", "");
574		$("#icon_bad").attr("style", "");
575		$("#icon_warning").attr("style", "");
576	}
577
578	//$("#icon_default").attr("src", "<?php echo $path; ?>" + icon +
579}
580</script>
581