Wizard (interfaces) backend performance improved

This commit is contained in:
fbsanchez 2021-02-04 13:11:47 +01:00
parent 778b3762a2
commit 5a38d31a6d
5 changed files with 190 additions and 134 deletions

View File

@ -653,6 +653,30 @@ $table_other->data[14][1] = html_print_input_text(
true
);
$table_other->data[15][0] = __('SNMP walk binary');
$table_other->data[15][1] = html_print_input_text(
'snmpwalk',
$config['snmpwalk'],
'',
50,
10,
true
);
$tip = ui_print_help_tip(
__('SNMP bulk walk is not able to request V1 SNMP, this option will be used instead (by default snmpwalk, slower).'),
true
);
$table_other->data[16][0] = __('SNMP walk binary (fallback)').$tip;
$table_other->data[16][1] = html_print_input_text(
'snmpwalk_fallback',
$config['snmpwalk_fallback'],
'',
50,
10,
true
);
echo '<form id="form_setup" method="post">';
echo '<fieldset>';

View File

@ -913,16 +913,34 @@ class AgentWizard extends HTML
*/
public function performSNMPInterfaces($receivedOid)
{
// Path for get the IPs (ipv4).
$snmpIpDiscover = '.1.3.6.1.2.1.4.34.1.4.1.4';
$snmpIpIndexes = '.1.3.6.1.2.1.4.34.1.3.1.4';
$ipsResult = [];
// In this case we need the full information provided by snmpwalk.
$ipsResult = $this->snmpwalkValues($snmpIpDiscover, false, true);
$indexes = $this->snmpwalkValues($snmpIpIndexes, false, true);
$unicastIpReferences = [];
foreach ($indexes as $k => $v) {
$key = str_replace($snmpIpIndexes.'.', '', $k);
// Only catch the unicast records.
if ((preg_match('/unicast/', $ipsResult[$snmpIpDiscover.'.'.$key]) === 1)) {
$value = explode(': ', $v)[1];
$unicastIpReferences[$value] = $key;
}
}
// Create a list with the interfaces.
$interfaces = [];
foreach ($receivedOid as $keyOid => $nameOid) {
list($nameKey, $indexKey) = explode(
'.',
str_replace('IF-MIB::', '', $keyOid)
);
list($typeValue, $value) = explode(': ', $nameOid);
foreach ($receivedOid as $indexKey => $name) {
if ($indexKey[0] === '.') {
$indexKey = substr($indexKey, 1, strlen($indexKey));
}
// Set the name of interface.
$interfaces[$indexKey]['name'] = $value;
$interfaces[$indexKey]['name'] = $name;
// Get the description.
$interfaces[$indexKey]['descr'] = $this->snmpgetValue(
'.1.3.6.1.2.1.2.2.1.2.'.$indexKey
@ -933,40 +951,8 @@ class AgentWizard extends HTML
);
// Get unicast IP address.
$interfaces[$indexKey]['ip'] = '';
// Path for get the IPs (ipv4).
$snmpIpDiscover = '.1.3.6.1.2.1.4.34.1.4.1.4';
$ipsResult = [];
// In this case we need the full information provided by snmpwalk.
$snmpwalkIps = sprintf(
'snmpwalk -On -v%s -c %s %s %s',
$this->version,
$this->community,
$this->targetIp,
$snmpIpDiscover
);
exec($snmpwalkIps, $ipsResult);
foreach ($ipsResult as $ipResult) {
list($ipOidDirection, $ipOidValue) = explode(' = ', $ipResult);
// Only catch the unicast records.
if ((preg_match('/unicast/', $ipOidValue) === 1)) {
$tmpIpOidDirection = str_replace(
$snmpIpDiscover,
'',
$ipOidDirection
);
$snmpIpIndexDiscover = '.1.3.6.1.2.1.4.34.1.3.1.4';
$snmpIpIndexDiscover .= $tmpIpOidDirection;
$snmpgetIpIndex = $this->snmpgetValue($snmpIpIndexDiscover);
// If this Ip index number match with the current index key.
if ($snmpgetIpIndex === $indexKey) {
$interfaces[$indexKey]['ip'] .= substr(
$tmpIpOidDirection,
1
);
}
} else {
continue;
}
if (isset($unicastIpReferences[$indexKey]) === true) {
$interfaces[$indexKey]['ip'] = '';
}
}
@ -1029,21 +1015,10 @@ class AgentWizard extends HTML
if ($this->wizardSection === 'snmp_interfaces_explorer') {
// Check if thereis x64 counters.
$snmp_tmp = '.1.3.6.1.2.1.31.1.1.1.6';
$check_x64 = get_snmpwalk(
$this->targetIp,
$this->version,
$this->community,
$this->authUserV3,
$this->securityLevelV3,
$this->authMethodV3,
$this->authPassV3,
$this->privacyMethodV3,
$this->privacyPassV3,
0,
$check_x64 = $this->snmpwalkValues(
$snmp_tmp,
$this->targetPort,
$this->server,
$this->extraArguments
false,
true
);
if ($check_x64) {
@ -1056,21 +1031,10 @@ class AgentWizard extends HTML
// Explore interface names.
$oidExplore = '.1.3.6.1.2.1.31.1.1.1.1';
$receivedOid = get_snmpwalk(
$this->targetIp,
$this->version,
$this->community,
$this->authUserV3,
$this->securityLevelV3,
$this->authMethodV3,
$this->authPassV3,
$this->privacyMethodV3,
$this->privacyPassV3,
0,
$receivedOid = $this->snmpwalkValues(
$oidExplore,
$this->targetPort,
$this->server,
$this->extraArguments
false,
true
);
} else {
// Get the device PEN.
@ -1078,21 +1042,10 @@ class AgentWizard extends HTML
}
// Doc Interfaces de red.
$receivedOid = get_snmpwalk(
$this->targetIp,
$this->version,
$this->community,
$this->authUserV3,
$this->securityLevelV3,
$this->authMethodV3,
$this->authPassV3,
$this->privacyMethodV3,
$this->privacyPassV3,
0,
$receivedOid = $this->snmpwalkValues(
$oidExplore,
$this->targetPort,
$this->server,
$this->extraArguments
false,
false
);
if (empty($receivedOid) || preg_grep('/no.*object/i', $receivedOid)) {
@ -1100,21 +1053,10 @@ class AgentWizard extends HTML
$oidExplore = '1.3.6.1.2.1.2.2.1.2';
// Doc Interfaces de red.
$receivedOid = get_snmpwalk(
$this->targetIp,
$this->version,
$this->community,
$this->authUserV3,
$this->securityLevelV3,
$this->authMethodV3,
$this->authPassV3,
$this->privacyMethodV3,
$this->privacyPassV3,
0,
$receivedOid = $this->snmpwalkValues(
$oidExplore,
$this->targetPort,
$this->server,
$this->extraArguments
false,
false
);
}
@ -2442,6 +2384,7 @@ class AgentWizard extends HTML
// Get current value.
$currentValue = $this->snmpgetValue($moduleData['value']);
// It unit of measure have data, attach to current value.
if (empty($moduleData['module_unit']) === false) {
$currentValue .= ' '.$moduleData['module_unit'];
@ -2577,6 +2520,7 @@ class AgentWizard extends HTML
// Get current value.
$currentValue = $this->snmpgetValue($moduleData['value']);
// Format current value with thousands and decimals.
if (is_numeric($currentValue) === true) {
$decimals = (is_float($currentValue) === true) ? 2 : 0;
@ -3263,32 +3207,26 @@ class AgentWizard extends HTML
*/
private function snmpgetValue(string $oid, ?bool $full_output=false)
{
$output = get_snmpwalk(
$this->targetIp,
$this->version,
$this->community,
$this->authUserV3,
$this->securityLevelV3,
$this->authMethodV3,
$this->authPassV3,
$this->privacyMethodV3,
$this->privacyPassV3,
0,
$oid,
$this->targetPort,
$this->server,
$this->extraArguments,
(($full_output === false) ? '-Oa -On' : '-Oa')
);
if ($oid[0] !== '.') {
$oid = '.'.$oid;
}
$output = $this->snmpwalkValues($oid, false, true, true);
if (is_array($output) === true) {
foreach ($output as $k => $v) {
if ($full_output === true) {
return $k.' = '.$v;
if ($k[0] !== '.') {
$k = '.'.$k;
}
$value = explode(': ', $v, 2);
return $value[1];
if ($k == $oid) {
if ($full_output === true) {
return $k.' = '.$v;
}
$value = explode(': ', $v, 2);
return $value[1];
}
}
}
@ -3301,11 +3239,56 @@ class AgentWizard extends HTML
*
* @param string $oid Oid for get the values.
* @param boolean $full_output Array with full output.
* @param boolean $pure Return results as received by get_snmwalk.
* @param boolean $get If get operation, adjust key.
*
* @return array
*/
private function snmpwalkValues(string $oid, bool $full_output=false)
{
private function snmpwalkValues(
string $oid,
bool $full_output=false,
bool $pure=false,
bool $get=false
) {
static $__cached_walks;
if ($__cached_walks === null) {
$__cached_walks = [];
}
if ($oid[0] !== '.') {
$oid = '.'.$oid;
}
if ($get === true) {
// Request from snmpget. Cache is in tree.
$tree_oid = strrev($oid);
$tree_oid = strrev(
substr(
$tree_oid,
(strpos($tree_oid, '.') + 1),
strlen($tree_oid)
)
);
$key = $tree_oid.'-'.((int) $full_output).'-'.((int) $pure);
// Request entire sub-tree.
$oid = $tree_oid;
} else {
$key = $oid.'-'.((int) $full_output).'-'.((int) $pure);
}
if (isset($__cached_walks[$key]) === true) {
return $__cached_walks[$key];
}
if (defined(caca)) {
hd($oid);
hd($tree_oid);
hd($__cached_walks);
die();
}
$output = [];
$temporal = get_snmpwalk(
$this->targetIp,
@ -3325,6 +3308,11 @@ class AgentWizard extends HTML
(($full_output === false) ? '-Oa -On' : '-Oa')
);
if ($pure === true) {
$__cached_walks[$key] = $temporal;
return $temporal;
}
if (empty($temporal) === false) {
foreach ($temporal as $key => $oid_unit) {
if ($full_output === true) {
@ -3337,6 +3325,7 @@ class AgentWizard extends HTML
}
}
$__cached_walks[$key] = $output;
return $output;
}

View File

@ -2012,22 +2012,22 @@ function get_snmpwalk(
$base_oid = escapeshellarg($base_oid);
}
if (empty($config['snmpwalk'])) {
switch (PHP_OS) {
case 'FreeBSD':
$snmpwalk_bin = '/usr/local/bin/snmpwalk';
break;
switch (PHP_OS) {
case 'FreeBSD':
$snmpwalk_bin = '/usr/local/bin/snmpwalk';
break;
case 'NetBSD':
$snmpwalk_bin = '/usr/pkg/bin/snmpwalk';
break;
case 'NetBSD':
$snmpwalk_bin = '/usr/pkg/bin/snmpwalk';
break;
default:
default:
if ($snmp_version == '1') {
$snmpwalk_bin = 'snmpwalk';
break;
}
} else {
$snmpwalk_bin = $config['snmpwalk'];
} else {
$snmpwalk_bin = 'snmpbulkwalk';
}
break;
}
switch (PHP_OS) {
@ -2035,6 +2035,7 @@ function get_snmpwalk(
case 'WINNT':
case 'Windows':
$error_redir_dir = 'NUL';
$snmpwalk_bin = 'snmpwalk';
break;
default:
@ -2042,6 +2043,14 @@ function get_snmpwalk(
break;
}
if (empty($config['snmpwalk']) === false) {
if ($snmp_version == '1') {
$snmpwalk_bin = $config['snmpwalk_fallback'];
} else {
$snmpwalk_bin = $config['snmpwalk'];
}
}
$output = [];
$rc = 0;
switch ($snmp_version) {

View File

@ -864,6 +864,14 @@ function config_update_config()
if (!config_update_value('row_limit_csv', get_parameter('row_limit_csv'))) {
$error_update[] = __('Row limit in csv log');
}
if (!config_update_value('snmpwalk', get_parameter('snmpwalk'))) {
$error_update[] = __('SNMP walk binary path');
}
if (!config_update_value('snmpwalk_fallback', get_parameter('snmpwalk_fallback'))) {
$error_update[] = __('SNMP walk binary path (fallback for v1)');
}
break;
case 'vis':
@ -1872,6 +1880,32 @@ function config_process_config()
config_update_value('row_limit_csv', 10000);
}
if (!isset($config['snmpwalk'])) {
switch (PHP_OS) {
case 'FreeBSD':
config_update_value('snmpwalk', '/usr/local/bin/snmpwalk');
break;
case 'NetBSD':
config_update_value('snmpwalk', '/usr/pkg/bin/snmpwalk');
break;
case 'WIN32':
case 'WINNT':
case 'Windows':
config_update_value('snmpwalk', 'snmpwalk');
break;
default:
config_update_value('snmpwalk', 'snmpbulkwalk');
break;
}
}
if (!isset($config['snmpwalk_fallback'])) {
config_update_value('snmpwalk_fallback', 'snmpwalk');
}
if (!isset($config['event_purge'])) {
config_update_value('event_purge', 15);
}

View File

@ -227,7 +227,7 @@ function io_safe_output_array(&$item, $key=false, $utf8=true)
* @param string|array $value String or array of strings to be cleaned.
* @param boolean $utf8 Flag, set the output encoding in utf8, by default true.
*
* @return unknown_type
* @return string
*/
function io_safe_output($value, $utf8=true)
{