Beispiel #1
0
function snmpget_cache_multi($device, $oids, $array, $mib = NULL, $mibdir = NULL, $flags = OBS_QUOTES_TRIM)
{
    global $snmp_stats, $mibs_loaded;
    $output = 'QUs';
    $numeric_oids = is_flag_set(OBS_SNMP_NUMERIC, $flags);
    // Numeric oids, do not parse oid part
    if (is_flag_set(OBS_SNMP_NUMERIC_INDEX, $flags)) {
        $output .= 'b';
    }
    if ($numeric_oids) {
        $output .= 'n';
    }
    if (is_flag_set(OBS_SNMP_ENUM, $flags)) {
        $output .= 'e';
    }
    if (is_flag_set(OBS_SNMP_HEX, $flags)) {
        $output .= 'x';
    }
    $options = "-O{$output}";
    if (is_array($oids)) {
        $data = '';
        $oid_chunks = array_chunk($oids, 16);
        $GLOBALS['snmp_status'] = FALSE;
        foreach ($oid_chunks as $oid_chunk) {
            $oid_text = implode($oid_chunk, ' ');
            $cmd = snmp_command('snmpget', $device, $oid_text, $options, $mib, $mibdir, $flags);
            $start = microtime(TRUE);
            $this_data = trim(external_exec($cmd));
            $runtime = microtime(TRUE) - $start;
            $GLOBALS['snmp_status'] = $GLOBALS['exec_status']['exitcode'] === 0 ? TRUE : $GLOBALS['snmp_status'];
            snmp_log_errors('snmpget', $device, $oid_text, $options, $mib, $mibdir);
            $data .= $this_data . "\n";
            $GLOBALS['snmp_stats']['snmpget']['count']++;
            $GLOBALS['snmp_stats']['snmpget']['time'] += $runtime;
        }
    } else {
        $cmd = snmp_command('snmpget', $device, $oids, $options, $mib, $mibdir, $flags);
        $start = microtime(TRUE);
        $data = trim(external_exec($cmd));
        $runtime = microtime(TRUE) - $start;
        $GLOBALS['snmp_status'] = $GLOBALS['exec_status']['exitcode'] === 0 ? TRUE : FALSE;
        snmp_log_errors('snmpget', $device, $oids, $options, $mib, $mibdir);
        $GLOBALS['snmp_stats']['snmpget']['count']++;
        $GLOBALS['snmp_stats']['snmpget']['time'] += $runtime;
    }
    foreach (explode("\n", $data) as $entry) {
        list($oid, $value) = explode('=', $entry, 2);
        $oid = trim($oid);
        $value = trim_quotes($value, $flags);
        if (strpos($value, 'Wrong Type') === 0) {
            // Remove Wrong Type string
            $value = preg_replace('/Wrong Type .*?: (.*)/s', '\\1', $value);
        }
        // For numeric oids do not split oid and index part
        if ($numeric_oids && isset($oid[0]) && is_valid_snmp_value($value)) {
            $array[$oid] = $value;
            continue;
        }
        list($oid, $index) = explode('.', $oid, 2);
        if (isset($oid[0]) && isset($index) && is_valid_snmp_value($value)) {
            $array[$index][$oid] = $value;
        }
    }
    if (empty($array)) {
        $GLOBALS['snmp_status'] = FALSE;
        snmp_log_errors('snmpget', $device, $oids, $options, $mib, $mibdir);
    }
    if (OBS_DEBUG) {
        print_message('SNMP STATUS[' . ($GLOBALS['snmp_status'] ? '%gTRUE' : '%rFALSE') . '%n]', 'color');
    }
    return $array;
}
function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL, $strip_quotes = 1)
{
    global $debug, $config, $runtime_stats;
    $cmd = snmp_command('snmpwalk', $device, $oid, $options, $mib, $mibdir);
    $data = trim(external_exec($cmd));
    $GLOBALS['snmp_status'] = $GLOBALS['exec_status']['exitcode'] === 0 ? TRUE : FALSE;
    if ($strip_quotes) {
        $data = str_replace("\"", "", $data);
    }
    if (is_string($data) && preg_match("/No Such (Object|Instance)/i", $data)) {
        $data = false;
        $GLOBALS['snmp_status'] = FALSE;
    } else {
        if (preg_match('/No more variables left in this MIB View \\(It is past the end of the MIB tree\\)$/', $data) || preg_match('/End of MIB$/', $data)) {
            # Bit ugly :-(
            $d_ex = explode("\n", $data);
            unset($d_ex[count($d_ex) - 1]);
            $data = implode("\n", $d_ex);
        }
    }
    $runtime_stats['snmpwalk']++;
    return $data;
}
function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL, $strip_quotes = 1)
{
    global $runtime_stats;
    $cmd = snmp_command('snmpwalk', $device, $oid, $options, $mib, $mibdir);
    $data = trim(external_exec($cmd));
    $GLOBALS['snmp_status'] = $GLOBALS['exec_status']['exitcode'] === 0 ? TRUE : FALSE;
    if ($strip_quotes) {
        $data = str_replace("\"", "", $data);
    }
    if (is_string($data) && preg_match("/No Such (Object|Instance)/i", $data)) {
        $data = FALSE;
        $GLOBALS['snmp_status'] = FALSE;
    } else {
        if (preg_match('/No more variables left in this MIB View \\(It is past the end of the MIB tree\\)$/', $data) || preg_match('/End of MIB$/', $data)) {
            # Bit ugly :-(
            $d_ex = explode("\n", $data);
            $d_ex_count = count($d_ex);
            if ($d_ex_count > 1) {
                // Remove last line
                unset($d_ex[$d_ex_count - 1]);
                $data = implode("\n", $d_ex);
            } else {
                $data = FALSE;
                $GLOBALS['snmp_status'] = FALSE;
            }
        }
    }
    $runtime_stats['snmpwalk']++;
    if (OBS_DEBUG) {
        print_message('SNMP_STATUS[' . ($GLOBALS['snmp_status'] ? '%gTRUE' : '%rFALSE') . '%n]', 'color');
    }
    return $data;
}