function collectHostIndexedOid(&$host, $tree, $table, $name, $preserve = false)
{
    global $cnn_id;
    static $types;
    debug("Beginning Processing for '" . $host["description"] . "[" . $host["hostname"] . "]', Table '{$name}'");
    if (sizeof($host)) {
        /* mark for deletion */
        db_execute("UPDATE {$table} SET present=0 WHERE host_id=" . $host["id"]);
        debug("Polling {$name} from '" . $host["description"] . "[" . $host["hostname"] . "]'");
        $hostMib = array();
        foreach ($tree as $mname => $oid) {
            if ($name == "processor") {
                $retrieval = SNMP_VALUE_PLAIN;
            } elseif ($mname == "mac") {
                $retrieval = SNMP_VALUE_LIBRARY;
            } elseif ($mname == "date") {
                $retrieval = SNMP_VALUE_LIBRARY;
            } elseif ($mname != "baseOID") {
                $retrieval = SNMP_VALUE_PLAIN;
            } else {
                continue;
            }
            $walk = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $oid, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), $host["max_oids"], $retrieval, SNMP_WEBUI);
            $hostMib = array_merge($hostMib, $walk);
        }
        $set_string = "";
        $values = "";
        $sql_suffix = "";
        $sql_prefix = "INSERT INTO {$table}";
        if (sizeof($tree)) {
            foreach ($tree as $bname => $oid) {
                if ($bname != "baseOID" && $bname != "index") {
                    $values .= (strlen($values) ? "`, `" : "`") . $bname;
                    $sql_suffix .= (!strlen($sql_suffix) ? " ON DUPLICATE KEY UPDATE `index`=VALUES(`index`), `" : ", `") . $bname . "`=VALUES(`" . $bname . "`)";
                }
            }
        }
        $sql_prefix .= " (`host_id`, `index`, " . ($preserve ? "`last_seen`, " : "") . $values . "`) VALUES ";
        $sql_suffix .= ", present=1" . ($preserve ? ", last_seen=NOW()" : "");
        // Locate the values names
        $prevIndex = "";
        $new_array = array();
        if (sizeof($hostMib)) {
            foreach ($hostMib as $mib) {
                /* do some cleanup */
                if (substr($mib["oid"], 0, 1) != ".") {
                    $mib["oid"] = "." . $mib["oid"];
                }
                if (substr($mib["value"], 0, 4) == "OID:") {
                    $mib["value"] = trim(str_replace("OID:", "", $mib["value"]));
                }
                $splitIndex = mikrotik_splitBaseIndex($mib["oid"]);
                if (sizeof($splitIndex)) {
                    $index = $splitIndex[1];
                    $oid = $splitIndex[0];
                    $key = array_search($oid, $tree);
                    if (!empty($key)) {
                        if ($key == "type") {
                            if ($mib["value"] == ".1.3.6.1.2.1.25.2.1.1") {
                                $new_array[$index][$key] = 11;
                            } elseif ($mib["value"] == ".1.3.6.1.2.1.25.2.1.4") {
                                $new_array[$index][$key] = 14;
                            }
                        } elseif ($key == "name") {
                            $new_array[$index][$key] = strtoupper($mib["value"]);
                        } elseif ($key == "date") {
                            $new_array[$index][$key] = mikrotik_dateParse($mib["value"]);
                        } elseif ($key != "index") {
                            $new_array[$index][$key] = $mib["value"];
                        }
                    }
                    if (!empty($key) && $key != "index") {
                        debug("Key:'" . $key . "', Orig:'" . $mib["oid"] . "', Val:'" . $new_array[$index][$key] . "', Index:'" . $index . "', Base:'" . $oid . "'");
                    }
                } else {
                    echo "WARNING: Error parsing OID value\n";
                }
            }
        }
        /* dump the output to the database */
        $sql_insert = "";
        $count = 0;
        if (sizeof($new_array)) {
            foreach ($new_array as $index => $item) {
                $sql_insert .= (strlen($sql_insert) ? "), (" : "(") . $host["id"] . ", " . $index . ", " . ($preserve ? "NOW(), " : "");
                $i = 0;
                foreach ($tree as $mname => $oid) {
                    if ($mname != "baseOID" && $mname != "index") {
                        $sql_insert .= ($i > 0 ? ", " : "") . (isset($item[$mname]) && strlen(strlen($item[$mname])) ? $cnn_id->qstr($item[$mname]) : "''");
                        $i++;
                    }
                }
            }
            $sql_insert .= ")";
            $count++;
            if ($count % 200 == 0) {
                db_execute($sql_prefix . $sql_insert . $sql_suffix);
                $sql_insert = "";
            }
        }
        //print $sql_prefix . $sql_insert . $sql_suffix . "\n";
        if (strlen($sql_insert)) {
            db_execute($sql_prefix . $sql_insert . $sql_suffix);
        }
    }
}
Ejemplo n.º 2
0
function collectHostIndexedOid(&$host, $tree, $table, $name, $preserve = false, $depth = 1)
{
    static $types;
    debug("Beginning Processing for '" . $host['description'] . '[' . $host['hostname'] . "]', Table '{$name}'");
    if (sizeof($host)) {
        /* mark for deletion */
        if ($name == 'users') {
            db_execute("UPDATE {$table} SET present=0 WHERE host_id=" . $host['id'] . ' AND userType=0');
        } else {
            db_execute("UPDATE {$table} SET present=0 WHERE host_id=" . $host['id']);
        }
        debug("Polling {$name} from '" . $host['description'] . '[' . $host['hostname'] . "]'");
        $treeMib = array();
        $goodVals = array();
        foreach ($tree as $mname => $oid) {
            if ($name == 'processor') {
                $retrieval = SNMP_VALUE_PLAIN;
            } elseif ($mname == 'mac') {
                $retrieval = SNMP_VALUE_LIBRARY;
            } elseif ($mname == 'apBSSID') {
                $retrieval = SNMP_VALUE_LIBRARY;
            } elseif ($mname == 'date') {
                $retrieval = SNMP_VALUE_LIBRARY;
            } elseif ($mname != 'baseOID') {
                $retrieval = SNMP_VALUE_PLAIN;
            } else {
                continue;
            }
            $walk = cacti_snmp_walk($host['hostname'], $host['snmp_community'], $oid, $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), $host['max_oids'], $retrieval, SNMP_WEBUI);
            if (sizeof($walk)) {
                $goodVals[$mname] = true;
            } else {
                $goodVals[$mname] = false;
            }
            if (($mname == 'index' || $mname == 'name' || $mname == 'apSSID' || $mname == 'Strength') && !sizeof($walk)) {
                debug('No Index Information for OID: ' . $oid . ' on ' . $host['description'] . ' returning');
                return;
            }
            if ($goodVals[$mname]) {
                $treeMib = array_merge($treeMib, $walk);
            }
            debug('Polled: ' . $host['description'] . ', OID: ' . $oid . ', Size: ' . sizeof($walk));
        }
        $set_string = '';
        $values = '';
        $sql_suffix = '';
        $sql_prefix = "INSERT INTO {$table}";
        if (sizeof($tree)) {
            foreach ($tree as $mname => $oid) {
                if ($mname != 'baseOID' && $mname != 'index') {
                    if ($goodVals[$mname] == true) {
                        $values .= (strlen($values) ? '`, `' : '`') . $mname;
                        $sql_suffix .= (!strlen($sql_suffix) ? ' ON DUPLICATE KEY UPDATE `index`=VALUES(`index`), `' : ', `') . $mname . '`=VALUES(`' . $mname . '`)';
                    }
                }
            }
        }
        $sql_prefix .= ' (`host_id`, `index`, ' . ($preserve ? '`last_seen`, ' : '') . $values . '`) VALUES ';
        $sql_suffix .= ', present=1' . ($preserve ? ', last_seen=NOW()' : '');
        // Locate the values names
        $prevIndex = '';
        $new_array = array();
        if (sizeof($treeMib)) {
            foreach ($treeMib as $mib) {
                /* do some cleanup */
                if (substr($mib['oid'], 0, 1) != '.') {
                    $mib['oid'] = '.' . $mib['oid'];
                }
                if (substr($mib['value'], 0, 4) == 'OID:') {
                    $mib['value'] = trim(str_replace('OID:', '', $mib['value']));
                }
                $splitIndex = mikrotik_splitBaseIndex($mib['oid'], $depth);
                if (sizeof($splitIndex)) {
                    if ($name == 'wireless_registrations') {
                        $parts = explode('.', $splitIndex[1]);
                        $index = '';
                        for ($i = 0; $i < 6; $i++) {
                            $index .= ($i > 0 ? ':' : '') . strtoupper(substr('0' . dechex($parts[$i]), -2));
                        }
                    } else {
                        $index = $splitIndex[1];
                    }
                    $oid = $splitIndex[0];
                    $key = array_search($oid, $tree);
                    if (!empty($key) && $goodVals[$key] == true) {
                        if ($key == 'type') {
                            if ($mib['value'] == '.1.3.6.1.2.1.25.2.1.1') {
                                $new_array[$index][$key] = 11;
                            } elseif ($mib['value'] == '.1.3.6.1.2.1.25.2.1.4') {
                                $new_array[$index][$key] = 14;
                            }
                        } elseif ($key == 'name') {
                            $new_array[$index][$key] = str_replace('<', '', str_replace('>', '', strtoupper($mib['value'])));
                        } elseif ($key == 'date') {
                            $new_array[$index][$key] = mikrotik_dateParse($mib['value']);
                        } elseif ($key == 'mac') {
                            $new_array[$index][$key] = mikrotik_macParse($mib['value']);
                        } elseif ($key != 'index') {
                            $new_array[$index][$key] = $mib['value'];
                        }
                        if (!empty($key) && $key != 'index') {
                            debug("Key:'" . $key . "', Orig:'" . $mib['oid'] . "', Val:'" . (isset($new_array[$index]) ? $new_array[$index][$key] : '(Index not defined)') . "', Index:'" . $index . "', Base:'" . $oid . "'");
                        }
                    }
                } else {
                    echo "WARNING: Error parsing OID value\n";
                }
            }
        }
        /* dump the output to the database */
        $sql_insert = '';
        $count = 0;
        if (sizeof($new_array)) {
            foreach ($new_array as $index => $item) {
                $sql_insert .= (strlen($sql_insert) ? '), (' : '(') . $host['id'] . ", '" . $index . "', " . ($preserve ? 'NOW(), ' : '');
                $i = 0;
                foreach ($tree as $mname => $oid) {
                    if ($mname != 'baseOID' && $mname != 'index') {
                        if ($goodVals[$mname] == true) {
                            $sql_insert .= ($i > 0 ? ', ' : '') . (isset($item[$mname]) && strlen(strlen($item[$mname])) ? db_qstr($item[$mname]) : "''");
                            $i++;
                        }
                    }
                }
            }
            $sql_insert .= ')';
            $count++;
            if ($count % 200 == 0) {
                db_execute($sql_prefix . $sql_insert . $sql_suffix);
                $sql_insert = '';
            }
        }
        //print $sql_prefix . $sql_insert . $sql_suffix . "\n";
        if (strlen($sql_insert)) {
            db_execute($sql_prefix . $sql_insert . $sql_suffix);
        }
    }
}