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;
$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(
'freebsd',
'linux',
'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
$os_defs = Config::get('os');
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));
$array = array();
$oid = '';
foreach (explode("\n", $data) as $entry) {
list($oid,$value) = explode('=', $entry, 2);
$oid = trim($oid);
$value = trim($value, "\" \n\r");
if (!strstr($value, 'at this OID') && isset($oid)) {
$array[$oid] = $value;
if (str_contains($entry, '=')) {
list($oid,$value) = explode('=', $entry, 2);
$oid = trim($oid);
$value = trim($value, "\" \n\r");
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;
}
}
}