コード例 #1
0
ファイル: snmp.inc.php プロジェクト: Natolumin/observium
function snmp_command($command, $device, $oids, $options, $mib = NULL, $mibdir = NULL, $flags = OBS_SNMP_ALL)
{
    global $config;
    // This is compatibility code after refactor in r6306, for keep devices up before DB updated
    if (isset($device['snmpver']) && !isset($device['snmp_version'])) {
        // FIXME. Remove this in r7000
        $device['snmp_version'] = $device['snmpver'];
        foreach (array('transport', 'port', 'timeout', 'retries', 'community', 'authlevel', 'authname', 'authpass', 'authalgo', 'cryptopass', 'cryptoalgo') as $old_key) {
            // Convert to new device snmp keys
            $device['snmp_' . $old_key] = $device[$old_key];
        }
    }
    $nobulk = $device['snmp_version'] == 'v1' || isset($device['snmp_nobulk']) && $device['snmp_nobulk'] || isset($config['os'][$device['os']]['snmp']['nobulk']) && $config['os'][$device['os']]['snmp']['nobulk'];
    // Get the full command path from the config. Chose between bulkwalk and walk. Add max-reps if needed.
    switch ($command) {
        case 'snmpwalk':
            if ($nobulk) {
                $cmd = $config['snmpwalk'];
            } else {
                $cmd = $config['snmpbulkwalk'];
                if ($config['snmp']['max-rep'] && is_numeric($config['os'][$device['os']]['snmp']['max-rep'])) {
                    $cmd .= ' -Cr' . $config['os'][$device['os']]['snmp']['max-rep'];
                }
            }
            if (isset($config['os'][$device['os']]['snmp']['noincrease']) && $config['os'][$device['os']]['snmp']['noincrease']) {
                // do not check returned OIDs are increasing
                $cmd .= ' -Cc';
            }
            break;
        case 'snmpget':
            $cmd = $config[$command];
            break;
        case 'snmpbulkget':
            if ($nobulk) {
                $cmd = $config['snmpget'];
            } else {
                $cmd = $config['snmpbulkget'];
                if ($config['snmp']['max-rep'] && is_numeric($config['os'][$device['os']]['snmp']['max-rep'])) {
                    $cmd .= ' -Cr' . $config['os'][$device['os']]['snmp']['max-rep'];
                }
            }
            break;
        default:
            print_error("Unknown command {$command} passed to snmp_command(). THIS SHOULD NOT HAPPEN. PLEASE REPORT TO DEVELOPERS.");
            return FALSE;
    }
    // Set timeout values if set in the database, otherwise set to configured defaults
    if (is_numeric($device['snmp_timeout']) && $device['snmp_timeout'] > 0) {
        $snmp_timeout = $device['snmp_timeout'];
    } else {
        if (isset($config['snmp']['timeout'])) {
            $snmp_timeout = $config['snmp']['timeout'];
        }
    }
    if (isset($snmp_timeout)) {
        $cmd .= ' -t ' . escapeshellarg($snmp_timeout);
    }
    // Set retries if set in the database, otherwise set to configured defaults
    if (is_numeric($device['snmp_retries']) && $device['snmp_retries'] >= 0) {
        $snmp_retries = $device['snmp_retries'];
    } else {
        if (isset($config['snmp']['retries'])) {
            $snmp_retries = $config['snmp']['retries'];
        }
    }
    if (isset($snmp_retries)) {
        $cmd .= ' -r ' . escapeshellarg($snmp_retries);
    }
    // If no specific transport is set for the device, default to UDP.
    if (empty($device['snmp_transport'])) {
        $device['snmp_transport'] = 'udp';
    }
    // If no specific port is set for the device, default to 161.
    if (!$device['snmp_port']) {
        $device['snmp_port'] = 161;
    }
    // Add the SNMP authentication settings for the device
    $cmd .= snmp_gen_auth($device);
    // Hardcode ignoring underscore parsing errors because net-snmp is dumb as a bag of rocks
    $cmd .= ' -Pu';
    // Disables the use of DISPLAY-HINT information when assigning values.
    if (is_flag_set(OBS_SNMP_HEX, $flags)) {
        $cmd .= ' -Ih';
    }
    if ($options) {
        $cmd .= ' ' . $options;
    }
    if ($mib) {
        $cmd .= ' -m ' . $mib;
    }
    // Set correct MIB directories based on passed dirs and OS definition
    // If $mibdir variable is passed, we use it directly
    if ($mibdir) {
        $cmd .= " -M {$mibdir}";
    } else {
        $cmd .= ' -M ' . snmp_mib2mibdirs($mib);
    }
    // Add the device URI to the string
    $cmd .= ' ' . escapeshellarg($device['snmp_transport']) . ':' . escapeshellarg($device['hostname']) . ':' . escapeshellarg($device['snmp_port']);
    // Add the OID(s) to the string
    $cmd .= ' ' . $oids;
    // If we're not debugging, direct errors to /dev/null.
    if (!OBS_DEBUG) {
        $cmd .= ' 2>/dev/null';
    }
    return $cmd;
}
コード例 #2
0
 /**
  * @dataProvider providerSnmpMib2MibDir
  */
 public function testSnmpMib2MibDir($result, $mib)
 {
     global $config;
     $config['mib_dir'] = '/opt/observium/mibs';
     $this->assertSame($result, snmp_mib2mibdirs($mib));
 }