Ejemplo n.º 1
0
function collectHostIndexedOid(&$host, $tree, $table, $name)
{
    global $cnn_id;
    static $types;
    debug("Beginning Processing for '" . $host["description"] . "[" . $host["hostname"] . "]', Table '{$name}'");
    if (!sizeof($types)) {
        $types = array_rekey(db_fetch_assoc("SELECT id, oid, description FROM plugin_hmib_types"), "oid", array("id", "description"));
    }
    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 == "hrProcessor") {
                $retrieval = SNMP_VALUE_PLAIN;
            } 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`, " . $values . "`) VALUES ";
        $sql_suffix .= ", present=1";
        // Locate the values names
        $prevIndex = "";
        $new_array = array();
        $wonky = false;
        $hrProcValid = false;
        $effective = 0;
        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 = hmib_splitBaseIndex($mib["oid"]);
                if (sizeof($splitIndex)) {
                    $index = $splitIndex[1];
                    $oid = $splitIndex[0];
                    $key = array_search($oid, $tree);
                    /* issue workaround for snmp issues */
                    if ($name == "hrProcessor" && $mib["value"] == ".0.0") {
                        if ($wonky) {
                            $key = "load";
                            $mib["value"] = $effective;
                        } elseif (!$hrProcValid) {
                            if (db_fetch_cell("SELECT count(*) FROM plugin_hmib_hrSystem WHERE sysDescr LIKE '%Linux%' AND host_id=" . $host["id"])) {
                                /* look for the hrProcessorLoad value */
                                $temp_mib = $hostMib;
                                foreach ($temp_mib as $kk => $vv) {
                                    if (substr_count($kk, ".1.3.6.1.2.1.25.3.3.1.2")) {
                                        $hrProcValid = true;
                                    }
                                }
                                if (!$hrProcValid) {
                                    $user = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.4.1.2021.11.9.0", $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"], SNMP_VALUE_LIBRARY, SNMP_WEBUI);
                                    $system = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.4.1.2021.11.10.0", $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"], SNMP_VALUE_LIBRARY, SNMP_WEBUI);
                                    $effective = ($user + $system) * 2 / sizeof($mib);
                                    $key = "load";
                                    $mib["value"] = $effective;
                                    $wonky = true;
                                }
                            } else {
                                $effective = 0;
                            }
                        }
                    }
                    if (!empty($key)) {
                        if ($key == "type") {
                            $value = explode("(", $mib["value"]);
                            if (sizeof($value) > 1) {
                                $value = trim($value[1], " \n\r)");
                                if ($table != "plugin_hmib_hrSWInstalled" && $table != "plugin_hmib_hrSWRun") {
                                    $new_array[$index][$key] = isset($types[$value]) ? $types[$value]["id"] : 0;
                                } else {
                                    $new_array[$index][$key] = $value;
                                }
                            } else {
                                if ($table != "plugin_hmib_hrSWInstalled" && $table != "plugin_hmib_hrSWRun") {
                                    $new_array[$index][$key] = isset($types[$value[0]]) ? $types[$value[0]]["id"] : 0;
                                } else {
                                    $new_array[$index][$key] = $value[0];
                                }
                            }
                        } elseif ($key == "date") {
                            $new_array[$index][$key] = hmib_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 . ", ";
                $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 = "";
            }
        }
        if ($sql_insert != '') {
            db_execute($sql_prefix . $sql_insert . $sql_suffix);
        }
        /* remove old records */
        db_execute("DELETE FROM {$table} WHERE present=0 AND host_id=" . $host["id"]);
    }
}
Ejemplo n.º 2
0
function collectHostIndexedOid(&$host, $tree, $table, $name)
{
    global $cnn_id;
    static $types;
    debug("Beginning Processing for '" . $host['description'] . '[' . $host['hostname'] . "]', Table '{$name}'");
    if (!sizeof($types)) {
        $types = array_rekey(db_fetch_assoc('SELECT id, oid, description FROM plugin_hmib_types'), 'oid', array('id', 'description'));
    }
    $cols = db_get_table_column_types($table);
    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 == 'hrProcessor') {
                $retrieval = SNMP_VALUE_PLAIN;
            } 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`, ' . $values . '`) VALUES ';
        $sql_suffix .= ', present=1';
        // Locate the values names
        $prevIndex = '';
        $new_array = array();
        $wonky = false;
        $hrProcValid = false;
        $effective = 0;
        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 = hmib_splitBaseIndex($mib['oid']);
                if (sizeof($splitIndex)) {
                    $index = $splitIndex[1];
                    $oid = $splitIndex[0];
                    $key = array_search($oid, $tree);
                    /* issue workaround for snmp issues */
                    if ($name == 'hrProcessor' && $mib['value'] == '.0.0') {
                        if ($wonky) {
                            $key = 'load';
                            $mib['value'] = $effective;
                        } elseif (!$hrProcValid) {
                            if (db_fetch_cell("SELECT count(*) FROM plugin_hmib_hrSystem WHERE sysDescr LIKE '%Linux%' AND host_id=" . $host['id'])) {
                                /* look for the hrProcessorLoad value */
                                $temp_mib = $hostMib;
                                foreach ($temp_mib as $kk => $vv) {
                                    if (substr_count($kk, '.1.3.6.1.2.1.25.3.3.1.2')) {
                                        $hrProcValid = true;
                                    }
                                }
                                if (!$hrProcValid) {
                                    $user = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.4.1.2021.11.9.0', $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'], SNMP_VALUE_LIBRARY, SNMP_WEBUI);
                                    $system = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.4.1.2021.11.10.0', $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'], SNMP_VALUE_LIBRARY, SNMP_WEBUI);
                                    $effective = ($user + $system) * 2 / sizeof($mib);
                                    $key = 'load';
                                    $mib['value'] = $effective;
                                    $wonky = true;
                                }
                            } else {
                                $effective = 0;
                            }
                        }
                    }
                    if (!empty($key)) {
                        if ($key == 'type') {
                            $value = explode('(', $mib['value']);
                            if (sizeof($value) > 1) {
                                $value = trim($value[1], " \n\r)");
                                if ($table != 'plugin_hmib_hrSWInstalled' && $table != 'plugin_hmib_hrSWRun') {
                                    $new_array[$index][$key] = isset($types[$value]) ? $types[$value]['id'] : 0;
                                } else {
                                    $new_array[$index][$key] = $value;
                                }
                            } else {
                                if ($table != 'plugin_hmib_hrSWInstalled' && $table != 'plugin_hmib_hrSWRun') {
                                    $new_array[$index][$key] = isset($types[$value[0]]) ? $types[$value[0]]['id'] : 0;
                                } else {
                                    $new_array[$index][$key] = $value[0];
                                }
                            }
                        } elseif ($key == 'date') {
                            $new_array[$index][$key] = hmib_dateParse($mib['value']);
                        } elseif ($key == 'name' && $table == 'plugin_hmib_hrSWRun') {
                            if (!empty($mib['value']) && $mib['value'] != 'NULL') {
                                $parts = explode('/', $mib['value']);
                                $new_array[$index][$key] = $parts[0];
                            } else {
                                $new_array[$index][$key] = '';
                            }
                        } elseif ($key == 'path' && $table == 'plugin_hmib_hrSWRun') {
                            if (!empty($mib['value']) && $mib['value'] != 'NULL') {
                                $new_array[$index][$key] = $mib['value'];
                            } else {
                                $new_array[$index][$key] = '';
                            }
                        } elseif ($key == 'parameters' && $table == 'plugin_hmib_hrSWRun') {
                            if (!empty($mib['value']) && $mib['value'] != 'NULL') {
                                $new_array[$index][$key] = $mib['value'];
                            } else {
                                $new_array[$index][$key] = '';
                            }
                        } elseif ($key != 'index') {
                            if (isset($cols[$key]['type'])) {
                                if (strstr($cols[$key]['type'], 'int') !== false || strstr($cols[$key]['type'], 'float') !== false || strstr($cols[$key]['type'], 'double') !== false) {
                                    if (empty($mib['value'])) {
                                        $new_array[$index][$key] = 0;
                                    } else {
                                        $new_array[$index][$key] = $mib['value'];
                                    }
                                } else {
                                    $new_array[$index][$key] = $mib['value'];
                                }
                            } else {
                                $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 . ', ';
                $i = 0;
                foreach ($tree as $key => $oid) {
                    if ($key != 'baseOID' && $key != 'index') {
                        if (isset($item[$key]) && strlen(strlen($item[$key]))) {
                            if (isset($cols[$key]['type'])) {
                                if (strstr($cols[$key]['type'], 'int') !== false || strstr($cols[$key]['type'], 'float') !== false || strstr($cols[$key]['type'], 'double') !== false) {
                                    $sql_insert .= ($i > 0 ? ', ' : '') . (isset($item[$key]) ? $item[$key] : 0);
                                } else {
                                    $sql_insert .= ($i > 0 ? ', ' : '') . (isset($item[$key]) ? db_qstr($item[$key]) : '""');
                                }
                                $i++;
                            }
                        } else {
                            if (isset($cols[$key])) {
                                if (strstr($cols[$key]['type'], 'int') !== false || strstr($cols[$key]['type'], 'float') !== false || strstr($cols[$key]['type'], 'double') !== false) {
                                    $sql_insert .= ($i > 0 ? ', ' : '') . (isset($item[$key]) ? $item[$key] : 0);
                                } else {
                                    $sql_insert .= ($i > 0 ? ', ' : '') . (isset($item[$key]) ? db_qstr($item[$key]) : '""');
                                }
                                $i++;
                            }
                        }
                    }
                }
            }
            $sql_insert .= ')';
            $count++;
            if ($count % 200 == 0) {
                db_execute($sql_prefix . $sql_insert . $sql_suffix);
                $sql_insert = '';
            }
        }
        if ($sql_insert != '') {
            db_execute($sql_prefix . $sql_insert . $sql_suffix);
        }
        /* remove old records */
        db_execute("DELETE FROM {$table} WHERE present=0 AND host_id=" . $host['id']);
    }
}