Fixed snmp server selection

This commit is contained in:
Jose Gonzalez 2020-11-26 10:35:48 +01:00
parent 1ce0a8cba8
commit 9bf70add51
3 changed files with 146 additions and 115 deletions

View File

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

View File

@ -263,122 +263,149 @@ function snmp_browser_get_tree(
$snmp3_auth_pass='',
$snmp3_privacy_method='',
$snmp3_privacy_pass='',
$snmp3_context_engine_id=null
$snmp3_context_engine_id=null,
$server_to_exec=0
) {
global $config;
switch ($version) {
case '1':
$snmp_version = SNMP::VERSION_1;
break;
case '2':
$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(
if ($server_to_exec != 0) {
$output = get_snmpwalk(
$target_ip,
$version,
$community,
$snmp3_auth_user,
$snmp3_security_level,
$snmp3_auth_method,
$snmp3_auth_pass,
$snmp3_privacy_method,
$snmp3_privacy_pass,
0,
$starting_oid,
'',
$snmp3_context_engine_id
$server_to_exec,
'',
''
);
}
} else {
hd('entra por igual a 0', true);
switch ($version) {
case '1':
$snmp_version = SNMP::VERSION_1;
break;
$mibs_dir = $config['homedir'].'/attachment/mibs';
$_dir = opendir($mibs_dir);
case '2':
$snmp_version = SNMP::VERSION_2C;
break;
// Future. Recomemended: Use a global config limit of MIBs loaded.
while (($mib_file = readdir($_dir)) !== false) {
if ($mib_file == '..' || $mib_file == '.') {
continue;
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;
}
$rs = snmp_read_mib($mibs_dir.'/'.$mib_file);
if ($rs !== true) {
error_log('Failed while reading MIB file: '.$mib_file);
$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_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();
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.
$oid_tree = ['__LEAVES__' => []];
foreach ($output as $oid => $value) {
// Parse the OID.
$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++;
for ($i = 0; $i < $oid_len; $i++) {
// "X.Y.Z"
if ($oid[$i] == '"') {
$group = ($group ^ 1);
}
// Starting dot.
if ($sub_oid == '') {
continue;
}
// 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++;
}
if (! isset($ptr[$sub_oid]) || ! isset($ptr[$sub_oid]['__LEAVES__'])) {
$ptr[$sub_oid]['__LEAVES__'] = [];
}
// Starting dot.
if ($sub_oid == '') {
continue;
}
$ptr = &$ptr[$sub_oid]['__LEAVES__'];
$sub_oid = '';
} else {
if ($oid[$i] != '"') {
$sub_oid .= $oid[$i];
if (! isset($ptr[$sub_oid]) || ! isset($ptr[$sub_oid]['__LEAVES__'])) {
$ptr[$sub_oid]['__LEAVES__'] = [];
}
$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 = '';
}
// The last element will contain the full OID.
$ptr[$sub_oid] = [
'__OID__' => $oid,
'__VALUE__' => $value,
];
$ptr = &$ptr[$sub_oid];
$sub_oid = '';
} else {
$oid_tree = __('The server did not return any response.');
error_log($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_privacy_method = $("#snmp3_browser_privacy_method").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();
// Prepare the AJAX call
@ -41,6 +42,7 @@ function snmpBrowse() {
params["snmp3_browser_auth_pass"] = snmp3_auth_pass;
params["snmp3_browser_privacy_method"] = snmp3_privacy_method;
params["snmp3_browser_privacy_pass"] = snmp3_privacy_pass;
params["server_to_exec"] = server_to_exec;
params["action"] = "snmptree";
params["page"] = "include/ajax/snmp_browser.ajax";