Merge branch 'ent-6407-server-to-execute-snmp-browser' into 'develop'

Fixed snmp server selection

See merge request artica/pandorafms!3658
This commit is contained in:
Daniel Rodriguez 2021-01-11 14:57:12 +01:00
commit 14985c35ec
3 changed files with 145 additions and 116 deletions

View File

@ -1,22 +1,22 @@
<?php <?php
/**
// Pandora FMS- http://pandorafms.com * Pandora FMS- http://pandorafms.com.
// ================================================== * ==================================================
// Copyright (c) 2005-2021 Artica Soluciones Tecnologicas * Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
// Please see http://pandorafms.org for full contribution list * Please see http://pandorafms.org for full contribution list
// This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License * modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; version 2 * as published by the Free Software Foundation; version 2
// This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. * GNU General Public License for more details.
*/
require_once $config['homedir'].'/include/functions_config.php'; require_once $config['homedir'].'/include/functions_config.php';
require_once $config['homedir'].'/include/functions_snmp_browser.php'; require_once $config['homedir'].'/include/functions_snmp_browser.php';
require_once $config['homedir'].'/include/functions_snmp.php'; require_once $config['homedir'].'/include/functions_snmp.php';
require_once $config['homedir'].'/include/functions_network_components.php'; require_once $config['homedir'].'/include/functions_network_components.php';
global $config; global $config;
if (is_ajax()) { if (is_ajax()) {
@ -37,6 +37,7 @@ if (is_ajax()) {
$targets_oids = get_parameter('oids', ''); $targets_oids = get_parameter('oids', '');
$return_id = get_parameter('return_id', false); $return_id = get_parameter('return_id', false);
$custom_action = get_parameter('custom_action', ''); $custom_action = get_parameter('custom_action', '');
$server_to_exec = get_parameter('server_to_exec');
if (!is_array($targets_oids)) { if (!is_array($targets_oids)) {
$targets_oids = explode(',', $targets_oids); $targets_oids = explode(',', $targets_oids);
@ -50,20 +51,20 @@ if (is_ajax()) {
if ($action == 'snmptree') { if ($action == 'snmptree') {
$starting_oid = (string) get_parameter('starting_oid', '.'); $starting_oid = (string) get_parameter('starting_oid', '.');
$snmp_tree = snmp_browser_get_tree(
$snmp_tree = snmp_browser_get_tree( $target_ip,
$target_ip, $community,
$community, $starting_oid,
$starting_oid, $snmp_version,
$snmp_version, $snmp3_auth_user,
$snmp3_auth_user, $snmp3_security_level,
$snmp3_security_level, $snmp3_auth_method,
$snmp3_auth_method, $snmp3_auth_pass,
$snmp3_auth_pass, $snmp3_privacy_method,
$snmp3_privacy_method, $snmp3_privacy_pass,
$snmp3_privacy_pass, 'null',
$server_to_exec $server_to_exec
); );
if (! is_array($snmp_tree)) { if (! is_array($snmp_tree)) {
echo $snmp_tree; echo $snmp_tree;
} else { } else {
@ -166,7 +167,7 @@ if (is_ajax()) {
if ($method == 'snmp_browser_create_modules') { if ($method == 'snmp_browser_create_modules') {
// Get target ids from form. // Get target ids from form.
$id_items = get_parameter('id_item2', null); $id_items = get_parameter('id_item2', null);
if (!is_null($id_items)) { if (empty($id_items) === false) {
$id_target = explode(',', $id_items[0]); $id_target = explode(',', $id_items[0]);
} }

View File

@ -264,122 +264,148 @@ function snmp_browser_get_tree(
$snmp3_auth_pass='', $snmp3_auth_pass='',
$snmp3_privacy_method='', $snmp3_privacy_method='',
$snmp3_privacy_pass='', $snmp3_privacy_pass='',
$snmp3_context_engine_id=null $snmp3_context_engine_id=null,
$server_to_exec=0
) { ) {
global $config; global $config;
switch ($version) { if ($server_to_exec != 0) {
case '1': $output = get_snmpwalk(
$snmp_version = SNMP::VERSION_1; $target_ip,
break; $version,
$community,
case '2': $snmp3_auth_user,
$snmp_version = SNMP::VERSION_2C;
break;
case '2c':
$snmp_version = SNMP::VERSION_2C;
break;
case '3':
$snmp_version = SNMP::VERSION_3;
$community = $snmp3_auth_user;
break;
default:
$snmp_version = SNMP::VERSION_2C;
break;
}
$snmp_session = new SNMP($snmp_version, $target_ip, $community);
$snmp_session->oid_output_format = SNMP_OID_OUTPUT_MODULE;
// Set security if SNMP Version is 3.
if ($snmp_version == SNMP::VERSION_3) {
$snmp_session->setSecurity(
$snmp3_security_level, $snmp3_security_level,
$snmp3_auth_method, $snmp3_auth_method,
$snmp3_auth_pass, $snmp3_auth_pass,
$snmp3_privacy_method, $snmp3_privacy_method,
$snmp3_privacy_pass, $snmp3_privacy_pass,
0,
$starting_oid,
'', '',
$snmp3_context_engine_id $server_to_exec,
'',
''
); );
} } else {
switch ($version) {
case '1':
$snmp_version = SNMP::VERSION_1;
break;
$mibs_dir = $config['homedir'].'/attachment/mibs'; case '2':
$_dir = opendir($mibs_dir); $snmp_version = SNMP::VERSION_2C;
break;
// Future. Recomemended: Use a global config limit of MIBs loaded. case '2c':
while (($mib_file = readdir($_dir)) !== false) { $snmp_version = SNMP::VERSION_2C;
if ($mib_file == '..' || $mib_file == '.') { break;
continue;
case '3':
$snmp_version = SNMP::VERSION_3;
$community = $snmp3_auth_user;
break;
default:
$snmp_version = SNMP::VERSION_2C;
break;
} }
$rs = snmp_read_mib($mibs_dir.'/'.$mib_file); $snmp_session = new SNMP($snmp_version, $target_ip, $community);
if ($rs !== true) { $snmp_session->oid_output_format = SNMP_OID_OUTPUT_MODULE;
error_log('Failed while reading MIB file: '.$mib_file);
// Set security if SNMP Version is 3.
if ($snmp_version == SNMP::VERSION_3) {
$snmp_session->setSecurity(
$snmp3_security_level,
$snmp3_auth_method,
$snmp3_auth_pass,
$snmp3_privacy_method,
$snmp3_privacy_pass,
'',
$snmp3_context_engine_id
);
} }
}
closedir($_dir); $mibs_dir = $config['homedir'].'/attachment/mibs';
$_dir = opendir($mibs_dir);
// Future. Recomemended: Use a global config limit of MIBs loaded.
while (($mib_file = readdir($_dir)) !== false) {
if ($mib_file == '..' || $mib_file == '.') {
continue;
}
$rs = snmp_read_mib($mibs_dir.'/'.$mib_file);
if ($rs !== true) {
error_log('Failed while reading MIB file: '.$mib_file);
}
}
closedir($_dir);
$output = $snmp_session->walk($starting_oid);
if ($output == false) {
$output = $snmp_session->getError();
$snmp_session->close();
return $output;
}
$output = $snmp_session->walk($starting_oid);
if ($output == false) {
$output = $snmp_session->getError();
$snmp_session->close(); $snmp_session->close();
return $output;
} }
$snmp_session->close(); // Build the tree if output comes filled.
if (empty($output) === false) {
$oid_tree = ['__LEAVES__' => []];
foreach ($output as $oid => $value) {
// Parse the OID.
$oid_len = strlen($oid);
$group = 0;
$sub_oid = '';
$ptr = &$oid_tree['__LEAVES__'];
// Build the tree. for ($i = 0; $i < $oid_len; $i++) {
$oid_tree = ['__LEAVES__' => []]; // "X.Y.Z"
foreach ($output as $oid => $value) { if ($oid[$i] == '"') {
// Parse the OID. $group = ($group ^ 1);
$oid_len = strlen($oid);
$group = 0;
$sub_oid = '';
$ptr = &$oid_tree['__LEAVES__'];
for ($i = 0; $i < $oid_len; $i++) {
// "X.Y.Z"
if ($oid[$i] == '"') {
$group = ($group ^ 1);
}
// Move to the next element of the OID.
if ($group == 0 && ($oid[$i] == '.' || ($oid[$i] == ':' && $oid[($i + 1)] == ':'))) {
// Skip the next ":".
if ($oid[$i] == ':') {
$i++;
} }
// Starting dot. // Move to the next element of the OID.
if ($sub_oid == '') { if ($group == 0 && ($oid[$i] == '.' || ($oid[$i] == ':' && $oid[($i + 1)] == ':'))) {
continue; // Skip the next ":".
} if ($oid[$i] == ':') {
$i++;
}
if (! isset($ptr[$sub_oid]) || ! isset($ptr[$sub_oid]['__LEAVES__'])) { // Starting dot.
$ptr[$sub_oid]['__LEAVES__'] = []; if ($sub_oid == '') {
} continue;
}
$ptr = &$ptr[$sub_oid]['__LEAVES__']; if (! isset($ptr[$sub_oid]) || ! isset($ptr[$sub_oid]['__LEAVES__'])) {
$sub_oid = ''; $ptr[$sub_oid]['__LEAVES__'] = [];
} else { }
if ($oid[$i] != '"') {
$sub_oid .= $oid[$i]; $ptr = &$ptr[$sub_oid]['__LEAVES__'];
$sub_oid = '';
} else {
if ($oid[$i] != '"') {
$sub_oid .= $oid[$i];
}
} }
} }
// The last element will contain the full OID.
$ptr[$sub_oid] = [
'__OID__' => $oid,
'__VALUE__' => $value,
];
$ptr = &$ptr[$sub_oid];
$sub_oid = '';
} }
} else {
// The last element will contain the full OID. $oid_tree = __('The server did not return any response.');
$ptr[$sub_oid] = [ error_log($oid_tree);
'__OID__' => $oid,
'__VALUE__' => $value,
];
$ptr = &$ptr[$sub_oid];
$sub_oid = '';
} }
return $oid_tree; return $oid_tree;

View File

@ -25,6 +25,7 @@ function snmpBrowse() {
var snmp3_auth_pass = $("#password-snmp3_browser_auth_pass").val(); var snmp3_auth_pass = $("#password-snmp3_browser_auth_pass").val();
var snmp3_privacy_method = $("#snmp3_browser_privacy_method").val(); var snmp3_privacy_method = $("#snmp3_browser_privacy_method").val();
var snmp3_privacy_pass = $("#password-snmp3_browser_privacy_pass").val(); var snmp3_privacy_pass = $("#password-snmp3_browser_privacy_pass").val();
var server_to_exec = $("#server_to_exec").val();
var ajax_url = $("#hidden-ajax_url").val(); var ajax_url = $("#hidden-ajax_url").val();
// Prepare the AJAX call // Prepare the AJAX call
@ -41,6 +42,7 @@ function snmpBrowse() {
params["snmp3_browser_auth_pass"] = snmp3_auth_pass; params["snmp3_browser_auth_pass"] = snmp3_auth_pass;
params["snmp3_browser_privacy_method"] = snmp3_privacy_method; params["snmp3_browser_privacy_method"] = snmp3_privacy_method;
params["snmp3_browser_privacy_pass"] = snmp3_privacy_pass; params["snmp3_browser_privacy_pass"] = snmp3_privacy_pass;
params["server_to_exec"] = server_to_exec;
params["action"] = "snmptree"; params["action"] = "snmptree";
params["page"] = "include/ajax/snmp_browser.ajax"; params["page"] = "include/ajax/snmp_browser.ajax";