refactor: Use one snmpget during os discovery (#7566)

Use only one snmpget to fetch sysObjectId and sysDescr
fix multiline data in snmp_get_multi_oid()
add snmp_get_multi_oid() to mock.snmp.inc.php
This commit is contained in:
Tony Murray
2017-10-28 05:53:05 -05:00
committed by Neil Lathwood
parent 090157779d
commit a93bb6635d
3 changed files with 52 additions and 11 deletions

View File

@@ -95,17 +95,18 @@ function getHostOS($device)
{ {
global $config; global $config;
$res = snmp_get_multi_oid($device, array('SNMPv2-MIB::sysDescr.0', 'SNMPv2-MIB::sysObjectID.0'));
$sysDescr = isset($res['.1.3.6.1.2.1.1.1.0']) ? $res['.1.3.6.1.2.1.1.1.0'] : '';
$sysObjectId = isset($res['.1.3.6.1.2.1.1.2.0']) ? $res['.1.3.6.1.2.1.1.2.0'] : '';
d_echo("| $sysDescr | $sysObjectId | \n");
$deferred_os = array( $deferred_os = array(
'freebsd', 'freebsd',
'linux', 'linux',
'ibmtl' //only has snmpget check 'ibmtl' //only has snmpget check
); );
$sysDescr = snmp_get($device, "SNMPv2-MIB::sysDescr.0", "-Ovq");
$sysObjectId = snmp_get($device, "SNMPv2-MIB::sysObjectID.0", "-Ovqn");
d_echo("| $sysDescr | $sysObjectId | \n");
// check yaml files // check yaml files
$os_defs = Config::get('os'); $os_defs = Config::get('os');
foreach ($os_defs as $os => $def) { foreach ($os_defs as $os => $def) {

View File

@@ -231,12 +231,22 @@ function snmp_get_multi_oid($device, $oids, $options = '-OUQn', $mib = null, $mi
$data = trim(external_exec($cmd)); $data = trim(external_exec($cmd));
$array = array(); $array = array();
$oid = '';
foreach (explode("\n", $data) as $entry) { foreach (explode("\n", $data) as $entry) {
list($oid,$value) = explode('=', $entry, 2); if (str_contains($entry, '=')) {
$oid = trim($oid); list($oid,$value) = explode('=', $entry, 2);
$value = trim($value, "\" \n\r"); $oid = trim($oid);
if (!strstr($value, 'at this OID') && isset($oid)) { $value = trim($value, "\" \n\r");
$array[$oid] = $value; if (!strstr($value, 'at this OID') && isset($oid)) {
$array[$oid] = $value;
}
} else {
if (isset($array[$oid])) {
// if appending, add a line return
$array[$oid] .= PHP_EOL . $entry;
} else {
$array[$oid] = $entry;
}
} }
} }

View File

@@ -227,6 +227,36 @@ function snmp_get($device, $oid, $options = null, $mib = null, $mibdir = null)
} }
} }
function snmp_get_multi_oid($device, $oids, $options = '-OUQn', $mib = null, $mibdir = null)
{
if (!is_array($oids)) {
$oids = explode(' ', $oids);
}
$data = array();
foreach ($oids as $index => $oid) {
if (str_contains($options, 'n')) {
$oid_name = '.' . snmp_translate_number($oid, $mib, $mibdir);
$val = snmp_get($device, $oid_name, $options, $mib, $mibdir);
} elseif (str_contains($options, 's')
&& str_contains($oid, '::')) {
$tmp = explode('::', $oid);
$oid_name = $tmp[1];
$val = snmp_get($device, $oid, $options, $mib, $mibdir);
} else {
$oid_name = $oid;
$val = snmp_get($device, $oid, $options, $mib, $mibdir);
}
if ($val !== false) {
$data[$oid_name] = $val;
}
}
return $data;
}
function snmp_walk($device, $oid, $options = null, $mib = null, $mibdir = null) function snmp_walk($device, $oid, $options = null, $mib = null, $mibdir = null)
{ {
$community = $device['community']; $community = $device['community'];