Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use one snmpget during os discovery #7566

Merged
merged 3 commits into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 15 additions & 5 deletions includes/snmp.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/mocks/mock.snmp.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
$community = $device['community'];
Expand Down