1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2011 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; version 2
11
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17global $config;
18include_once($config['homedir'] . "/include/functions_agents.php");
19require_once ('include/functions_modules.php');
20require_once ('include/functions_alerts.php');
21require_once ('include/functions_reporting.php');
22require_once ('include/graphs/functions_utils.php');
23
24
25$idAgent = (int) get_parameter('id_agente', 0);
26$ipAgent = db_get_value('direccion', 'tagente', 'id_agente', $idAgent);
27
28check_login ();
29
30$ip_target = (string) get_parameter ('ip_target', $ipAgent);
31$snmp_community = (string) get_parameter ('snmp_community', 'public');
32$snmp_version = get_parameter('snmp_version', '1');
33$snmp3_auth_user = get_parameter('snmp3_auth_user');
34$snmp3_security_level = get_parameter('snmp3_security_level');
35$snmp3_auth_method = get_parameter('snmp3_auth_method');
36$snmp3_auth_pass = get_parameter('snmp3_auth_pass');
37$snmp3_privacy_method = get_parameter('snmp3_privacy_method');
38$snmp3_privacy_pass = get_parameter('snmp3_privacy_pass');
39$tcp_port = (string) get_parameter ('tcp_port');
40
41//See if id_agente is set (either POST or GET, otherwise -1
42$id_agent = $idAgent;
43
44// Get passed variables
45$snmpwalk = (int) get_parameter("snmpwalk", 0);
46$create_modules = (int) get_parameter("create_modules", 0);
47
48$interfaces = array();
49$interfaces_ip = array();
50
51if ($snmpwalk) {
52	// OID Used is for SNMP MIB-2 Interfaces
53	$snmpis = get_snmpwalk($ip_target, $snmp_version, $snmp_community, $snmp3_auth_user,
54		$snmp3_security_level, $snmp3_auth_method, $snmp3_auth_pass,
55		$snmp3_privacy_method, $snmp3_privacy_pass, 0, ".1.3.6.1.2.1.2", $tcp_port);
56	// ifXTable is also used
57	$ifxitems = get_snmpwalk($ip_target, $snmp_version, $snmp_community, $snmp3_auth_user,
58		$snmp3_security_level, $snmp3_auth_method, $snmp3_auth_pass,
59		$snmp3_privacy_method, $snmp3_privacy_pass, 0, ".1.3.6.1.2.1.31.1.1", $tcp_port);
60
61	// Get the interfaces IPV4/IPV6
62	$snmp_int_ip = get_snmpwalk($ip_target, $snmp_version, $snmp_community, $snmp3_auth_user,
63		$snmp3_security_level, $snmp3_auth_method, $snmp3_auth_pass,
64		$snmp3_privacy_method, $snmp3_privacy_pass, 0, ".1.3.6.1.2.1.4.34.1.3", $tcp_port);
65
66	// Build a [<interface id>] => [<interface ip>] array
67	if (!empty($snmp_int_ip)) {
68		foreach ($snmp_int_ip as $key => $value) {
69			// The key is something like IP-MIB::ipAddressIfIndex.ipv4."<ip>"
70			// or IP-MIB::ipAddressIfIndex.ipv6."<ip>"
71			// The value is something like INTEGER: <interface id>
72
73			$data = explode(': ',$value);
74			$interface_id = !empty($data) && isset($data[1]) ? $data[1] : false;
75
76			if (preg_match("/^.+\"(.+)\"$/", $key, $matches) && isset($matches[1])) {
77				$interface_ip = $matches[1];
78			}
79
80			// Get the first ip
81			if ($interface_id !== false && !empty($interface_ip) && !isset($interfaces_ip[$interface_id]))
82				$interfaces_ip[$interface_id] = $interface_ip;
83		}
84		unset($snmp_int_ip);
85	}
86
87	$snmpis = array_merge(($snmpis === false ? array() : $snmpis), ($ifxitems === false ? array() : $ifxitems));
88
89	$interfaces = array();
90
91	// We get here only the interface part of the MIB, not full mib
92	foreach($snmpis as $key => $snmp) {
93
94		$data = explode(': ',$snmp);
95		$keydata = explode('::',$key);
96		$keydata2 = explode('.',$keydata[1]);
97
98		// Avoid results without index and interfaces without name
99		if (!isset($keydata2[1]) || !isset($data[1])) {
100			continue;
101		}
102
103		if (array_key_exists(1,$data)) {
104			$interfaces[$keydata2[1]][$keydata2[0]]['type'] = $data[0];
105			$interfaces[$keydata2[1]][$keydata2[0]]['value'] = $data[1];
106		}
107		else {
108			$interfaces[$keydata2[1]][$keydata2[0]]['type'] = '';
109			$interfaces[$keydata2[1]][$keydata2[0]]['value'] = $data[0];
110		}
111
112		$interfaces[$keydata2[1]][$keydata2[0]]['oid'] = $key;
113		$interfaces[$keydata2[1]][$keydata2[0]]['checked'] = 0;
114	}
115
116	unset($interfaces[0]);
117}
118
119if ($create_modules) {
120	$id_snmp_serialize = get_parameter_post('id_snmp_serialize');
121	$interfaces = unserialize_in_temp($id_snmp_serialize);
122
123	$id_snmp_int_ip_serialize = get_parameter_post('id_snmp_int_ip_serialize');
124	$interfaces_ip = unserialize_in_temp($id_snmp_int_ip_serialize);
125
126	if (!$interfaces) {
127		$interfaces = array();
128	}
129	if (!$interfaces_ip) {
130		$interfaces_ip = array();
131	}
132
133	$values = array();
134
135	if ($tcp_port != '') {
136		$values['tcp_port'] = $tcp_port;
137	}
138	$values['snmp_community'] = $snmp_community;
139	$values['ip_target'] = $ip_target;
140	$values['tcp_send'] = $snmp_version;
141
142	if ($snmp_version == '3') {
143		$values['plugin_user'] = $snmp3_auth_user;
144		$values['plugin_pass'] = $snmp3_auth_pass;
145		$values['plugin_parameter'] = $snmp3_auth_method;
146		$values['custom_string_1'] = $snmp3_privacy_method;
147		$values['custom_string_2'] = $snmp3_privacy_pass;
148		$values['custom_string_3'] = $snmp3_security_level;
149	}
150
151	$oids = array();
152	foreach ($interfaces as $key => $interface) {
153		foreach ($interface as $key2 => $module) {
154			$oid = get_parameter($key."-".$key2, '');
155			if ($oid != '') {
156				$interfaces[$key][$key2]['checked'] = 1;
157				$oids[$key][] = $interfaces[$key][$key2]['oid'];
158			}
159			else {
160				$interfaces[$key][$key2]['checked'] = 0;
161			}
162		}
163	}
164	$modules = get_parameter('module', array());
165	$id_snmp = get_parameter('id_snmp');
166
167	if ($id_snmp == false) {
168		ui_print_error_message (__('No modules selected'));
169		$id_snmp = array();
170	}
171
172	if (agents_get_name($id_agent) == false) {
173		ui_print_error_message (__('No agent selected or the agent does not exist'));
174		$id_snmp = array();
175	}
176
177	$result = false;
178
179	$errors = array();
180	$done = 0;
181
182	foreach ($id_snmp as $id) {
183		$ifname = '';
184		$ifPhysAddress = '';
185
186		if (isset($interfaces[$id]['ifName']) && $interfaces[$id]['ifName']['value'] != "") {
187			$ifname = $interfaces[$id]['ifName']['value'];
188		}
189		else if (isset($interfaces[$id]['ifDescr']) && $interfaces[$id]['ifDescr']['value'] != "") {
190			$ifname = $interfaces[$id]['ifDescr']['value'];
191		}
192		if (isset($interfaces[$id]['ifPhysAddress']) && $interfaces[$id]['ifPhysAddress']['value'] != "") {
193			$ifPhysAddress = $interfaces[$id]['ifPhysAddress']['value'];
194			$ifPhysAddress = strtoupper($ifPhysAddress);
195		}
196		foreach ($modules as $module) {
197			$oid_array = explode('.', $module);
198			$oid_array[count($oid_array) - 1] = $id;
199			$oid = implode('.', $oid_array);
200
201			// Get the name
202			$name_array = explode('::', $oid_array[0]);
203			$name = $name_array[1] . "_" . $ifname;
204
205			// Clean the name
206			$name = str_replace  ( "\""  , "" , $name);
207
208			// Proc moduletypes
209			if (preg_match ("/Status/", $name_array[1]))
210				$module_type = 18;
211
212			elseif (preg_match ("/Present/", $name_array[1]))
213				$module_type = 18;
214
215			elseif (preg_match("/PromiscuousMode/", $name_array[1]))
216				$module_type = 18;
217
218			// String moduletypes
219			elseif (preg_match("/Alias/", $name_array[1]))
220				$module_type = 17;
221
222			elseif (preg_match("/Address/", $name_array[1]))
223				$module_type = 17;
224
225			elseif (preg_match("/Name/", $name_array[1]))
226				$module_type = 17;
227
228			elseif (preg_match("/Specific/", $name_array[1]))
229				$module_type = 17;
230
231			elseif (preg_match("/Descr/", $name_array[1]))
232				$module_type = 17;
233
234			// Specific counters (ends in s)
235			elseif (preg_match("/s$/", $name_array[1]))
236				$module_type = 16;
237
238			// Otherwise, numeric
239			else
240				$module_type = 15;
241
242			$values['unit'] = "";
243			if (preg_match("/Octets/", $name_array[1])) {
244				$values['unit'] = "Bytes";
245			}
246
247			$values['id_tipo_modulo'] = $module_type;
248
249			if (!empty($ifPhysAddress) && isset($interfaces_ip[$id])) {
250				$values['descripcion'] = io_safe_input("(IP: ".$interfaces_ip[$id]." - MAC: ".$ifPhysAddress." - ".$name.") " . $interfaces[$id]['ifDescr']['value']);
251			}
252			else if (!empty($ifPhysAddress)) {
253				$values['descripcion'] = io_safe_input("(MAC: ".$ifPhysAddress." - ".$name.") " . $interfaces[$id]['ifDescr']['value']);
254			}
255			else if (isset($interfaces_ip[$id])) {
256				$values['descripcion'] = io_safe_input("(IP: ".$interfaces_ip[$id]." - ".$name.") " . $interfaces[$id]['ifDescr']['value']);
257			}
258			else {
259				$values['descripcion'] = io_safe_input("(".$name.") " . $interfaces[$id]['ifDescr']['value']);
260			}
261
262			$values['snmp_oid'] = $oid;
263			$values['id_modulo'] = 2;
264
265			$result = modules_create_agent_module ($id_agent, io_safe_input($name), $values);
266
267			if (is_error($result)) {
268				if (!isset($errors[$result])) {
269					$errors[$result] = 0;
270				}
271				$errors[$result]++;
272			}
273			else {
274				$done++;
275			}
276		}
277	}
278
279	if ($done > 0) {
280		ui_print_success_message(__('Successfully modules created')." ($done)");
281	}
282
283	if (!empty($errors)) {
284		$msg = __('Could not be created').':';
285
286
287		foreach ($errors as $code => $number) {
288			switch ($code) {
289				case ERR_EXIST:
290					$msg .= '<br>'.__('Another module already exists with the same name')." ($number)";
291					break;
292				case ERR_INCOMPLETE:
293					$msg .= '<br>'.__('Some required fields are missed').': ('.__('name').') '." ($number)";
294					break;
295				case ERR_DB:
296				case ERR_GENERIC:
297				default:
298					$msg .= '<br>'.__('Processing error')." ($number)";
299					break;
300			}
301		}
302
303		ui_print_error_message($msg);
304
305	}
306}
307
308// Create the interface list for the interface
309$interfaces_list = array();
310foreach ($interfaces as $interface) {
311	// Get the interface name, removing " " characters and avoid "blank" interfaces
312	if (isset($interface['ifDescr']) && $interface['ifDescr']['value'] != "") {
313		$ifname = $interface['ifDescr']['value'];
314	}
315	else if (isset($interface['ifName']) && $interface['ifName']['value'] != "") {
316		$ifname = $interface['ifName']['value'];
317	}
318	else {
319		continue;
320	}
321
322	$interfaces_list[$interface['ifIndex']['value']] = str_replace  ( "\""  , "" , $ifname);
323}
324
325echo '<span id ="none_text" style="display: none;">' . __('None') . '</span>';
326echo "<form method='post' id='walk_form' action='index.php?sec=gagente&sec2=godmode/agentes/configurar_agente&tab=agent_wizard&wizard_section=snmp_interfaces_explorer&id_agente=$id_agent'>";
327
328$table->width = '100%';
329$table->cellpadding = 0;
330$table->cellspacing = 0;
331$table->class = 'databox filters';
332
333$table->data[0][0] = '<b>' . __('Target IP') . '</b>';
334$table->data[0][1] = html_print_input_text ('ip_target', $ip_target, '', 15, 60, true);
335
336$table->data[0][2] = '<b>' . __('Port') . '</b>';
337$table->data[0][3] = html_print_input_text ('tcp_port', $tcp_port, '', 5, 20, true);
338
339$snmp_versions['1'] = 'v. 1';
340$snmp_versions['2'] = 'v. 2';
341$snmp_versions['2c'] = 'v. 2c';
342$snmp_versions['3'] = 'v. 3';
343
344$table->data[1][0] = '<b>' . __('SNMP community') . '</b>';
345$table->data[1][1] = html_print_input_text ('snmp_community', $snmp_community, '', 15, 60, true);
346
347$table->data[1][2] = '<b>' . __('SNMP version') . '</b>';
348$table->data[1][3] = html_print_select ($snmp_versions, 'snmp_version', $snmp_version, '', '', '', true, false, false, '');
349
350$table->data[1][3] .= '<div id="spinner_modules" style="float: left; display: none;">' . html_print_image("images/spinner.gif", true) . '</div>';
351html_print_input_hidden('snmpwalk', 1);
352
353html_print_table($table);
354
355unset($table);
356
357//SNMP3 OPTIONS
358$table->width = '100%';
359
360$table->data[2][1] = '<b>'.__('Auth user').'</b>';
361$table->data[2][2] = html_print_input_text ('snmp3_auth_user', $snmp3_auth_user, '', 15, 60, true);
362$table->data[2][3] = '<b>'.__('Auth password').'</b>';
363$table->data[2][4] = html_print_input_password ('snmp3_auth_pass', $snmp3_auth_pass, '', 15, 60, true);
364$table->data[2][4] .= html_print_input_hidden('active_snmp_v3', 0, true);
365
366$table->data[5][0] = '<b>'.__('Privacy method').'</b>';
367$table->data[5][1] = html_print_select(array('DES' => __('DES'), 'AES' => __('AES')), 'snmp3_privacy_method', $snmp3_privacy_method, '', '', '', true);
368$table->data[5][2] = '<b>'.__('privacy pass').'</b>';
369$table->data[5][3] = html_print_input_password ('snmp3_privacy_pass', $snmp3_privacy_pass, '', 15, 60, true);
370
371$table->data[6][0] = '<b>'.__('Auth method').'</b>';
372$table->data[6][1] = html_print_select(array('MD5' => __('MD5'), 'SHA' => __('SHA')), 'snmp3_auth_method', $snmp3_auth_method, '', '', '', true);
373$table->data[6][2] = '<b>'.__('Security level').'</b>';
374$table->data[6][3] = html_print_select(array('noAuthNoPriv' => __('Not auth and not privacy method'),
375	'authNoPriv' => __('Auth and not privacy method'), 'authPriv' => __('Auth and privacy method')), 'snmp3_security_level', $snmp3_security_level, '', '', '', true);
376
377if ($snmp_version == 3) {
378	echo '<div id="snmp3_options">';
379}
380else {
381	echo '<div id="snmp3_options" style="display: none;">';
382}
383html_print_table($table);
384echo '</div>';
385
386echo "<div style='text-align:right; width:".$table->width."'>";
387echo '<span id="oid_loading" class="invisible">' . html_print_image("images/spinner.gif", true) . '</span>';
388html_print_submit_button(__('SNMP Walk'), 'snmp_walk', false, array('class' => 'sub next'));
389echo "</div>";
390
391if ($snmpwalk && !$snmpis) {
392	ui_print_error_message(__('Unable to do SNMP walk'));
393}
394
395unset($table);
396
397echo "</form>";
398
399if (!empty($interfaces_list)) {
400	echo '<span id ="none_text" style="display: none;">' . __('None') . '</span>';
401	echo "<form method='post' action='index.php?sec=gagente&sec2=godmode/agentes/configurar_agente&tab=agent_wizard&wizard_section=snmp_interfaces_explorer&id_agente=$id_agent'>";
402	echo '<span id="form_interfaces">';
403
404	$id_snmp_serialize = serialize_in_temp($interfaces, $config['id_user']."_snmp");
405	html_print_input_hidden('id_snmp_serialize', $id_snmp_serialize);
406
407	$id_snmp_int_ip_serialize = serialize_in_temp($interfaces_ip, $config['id_user']."_snmp_int_ip");
408	html_print_input_hidden('id_snmp_int_ip_serialize', $id_snmp_int_ip_serialize);
409
410	html_print_input_hidden('create_modules', 1);
411	html_print_input_hidden('ip_target', $ip_target);
412	html_print_input_hidden('tcp_port', $tcp_port);
413	html_print_input_hidden('snmp_community', $snmp_community);
414	html_print_input_hidden('snmp_version', $snmp_version);
415	html_print_input_hidden('snmp3_auth_user', $snmp3_auth_user);
416	html_print_input_hidden('snmp3_auth_pass', $snmp3_auth_pass);
417	html_print_input_hidden('snmp3_auth_method', $snmp3_auth_method);
418	html_print_input_hidden('snmp3_privacy_method', $snmp3_privacy_method);
419	html_print_input_hidden('snmp3_privacy_pass', $snmp3_privacy_pass);
420	html_print_input_hidden('snmp3_security_level', $snmp3_security_level);
421
422	$table->width = '100%';
423
424	//Agent selector
425	$table->data[0][0] = '<b>'.__('Interfaces').'</b>';
426	$table->data[0][1] = '';
427	$table->data[0][2] = '<b>'.__('Modules').'</b>';
428
429	$table->data[1][0] = html_print_select ($interfaces_list, 'id_snmp[]', 0, false, '', '', true, true, true, '', false, 'width:200px;');
430	$table->data[1][1] = html_print_image('images/darrowright.png', true);
431	$table->data[1][2] = html_print_select (array (), 'module[]', 0, false, '', 0, true, true, true, '', false, 'width:200px;');
432	$table->data[1][2] .= html_print_input_hidden('agent', $id_agent, true);
433
434	html_print_table($table);
435
436	echo "<div style='text-align:right; width:".$table->width."'>";
437	html_print_submit_button(__('Create modules'), '', false, array('class' => 'sub add'));
438	echo "</div>";
439	unset($table);
440
441	echo "</span>";
442	echo "</form>";
443	echo '</div>';
444}
445
446ui_require_jquery_file ('pandora.controls');
447ui_require_jquery_file ('ajaxqueue');
448ui_require_jquery_file ('bgiframe');
449?>
450<script language="javascript" type="text/javascript">
451/* <![CDATA[ */
452
453$(document).ready (function () {
454	var inputActive = true;
455
456	$(document).data('text_for_module', $("#none_text").html());
457
458	$("#id_snmp").change(snmp_changed_by_multiple_snmp);
459
460	$("#snmp_version").change(function () {
461		if (this.value == "3") {
462			$("#snmp3_options").css("display", "");
463		}
464		else {
465			$("#snmp3_options").css("display", "none");
466		}
467	});
468
469	$("#walk_form").submit(function() {
470		$("#submit-snmp_walk").disable ();
471		$("#oid_loading").show ();
472		$("#no_snmp").hide ();
473		$("#form_interfaces").hide ();
474	});
475});
476
477function snmp_changed_by_multiple_snmp (event, id_snmp, selected) {
478	var idSNMP = Array();
479
480	jQuery.each ($("#id_snmp option:selected"), function (i, val) {
481		idSNMP.push($(val).val());
482	});
483	$('#module').attr ('disabled', 1);
484	$('#module').empty ();
485	$('#module').append ($('<option></option>').html ("Loading...").attr ("value", 0));
486
487	jQuery.post ('ajax.php',
488		{"page" : "godmode/agentes/agent_manager",
489			"get_modules_json_for_multiple_snmp": 1,
490			"id_snmp[]": idSNMP,
491			"id_snmp_serialize": $("#hidden-id_snmp_serialize").val()
492		},
493		function (data) {
494			$('#module').empty ();
495			c = 0;
496			jQuery.each (data, function (i, val) {
497				s = js_html_entity_decode(val);
498				$('#module').append ($('<option></option>').html (s).attr ("value", i));
499				$('#module').fadeIn ('normal');
500				c++;
501				});
502
503			if (c == 0) {
504				if (typeof($(document).data('text_for_module')) != 'undefined') {
505					$('#module').append ($('<option></option>').html ($(document).data('text_for_module')).attr("value", 0).prop('selected', true));
506				}
507				else {
508					if (typeof(data['any_text']) != 'undefined') {
509						$('#module').append ($('<option></option>').html (data['any_text']).attr ("value", 0).prop('selected', true));
510					}
511					else {
512						var anyText = $("#any_text").html(); //Trick for catch the translate text.
513
514						if (anyText == null) {
515							anyText = 'Any';
516						}
517
518						$('#module').append ($('<option></option>').html (anyText).attr ("value", 0).prop('selected', true));
519					}
520				}
521			}
522			if (selected != undefined)
523				$('#module').attr ('value', selected);
524			$('#module').removeAttr('disabled');
525		},
526		"json");
527}
528
529/* ]]> */
530</script>
531
532