예제 #1
0
/**
 * @param $threads_id
 * @param $cfg_ocs
 * @param $server
 * @param $thread_nbr
 * @param $threadid
 * @param $fields
 * @param $config
**/
function plugin_ocsinventoryng_importFromOcsServer($threads_id, $cfg_ocs, $server, $thread_nbr, $threadid, $fields, $config)
{
    global $PluginOcsinventoryngDBocs;
    echo "\tThread #" . $threadid . ": import computers from server: '" . $cfg_ocs["name"] . "'\n";
    $where_multi_thread = '';
    $limit = "";
    if ($thread_nbr != -1 && $threadid != -1 && $thread_nbr > 1) {
        $where_multi_thread = " AND `ID` % {$thread_nbr} = " . ($threadid - 1);
    }
    if ($config->fields["import_limit"] > 0) {
        $limit = " LIMIT " . $config->fields["import_limit"];
    }
    $query_ocs = "SELECT `ID`\n                 FROM `hardware`\n                 INNER JOIN `accountinfo` ON (`hardware`.`ID` = `accountinfo`.`HARDWARE_ID`)\n                 WHERE ((CHECKSUM&" . intval($cfg_ocs["checksum"]) . ") > 0\n                        OR `LASTDATE` > '" . $server->fields["max_glpidate"] . "')\n                       AND TIMESTAMP(`LASTDATE`) < (NOW()-180)\n                       AND `ID` <= " . intval($server->fields["max_ocsid"]);
    if (!empty($cfg_ocs["tag_limit"])) {
        $splitter = explode("\$", $cfg_ocs["tag_limit"]);
        if (count($splitter)) {
            $query_ocs .= " AND `accountinfo`.`TAG` IN ('" . $splitter[0] . "'";
            for ($i = 1; $i < count($splitter); $i++) {
                $query_ocs .= ",'" . $splitter[$i] . "'";
            }
            $query_ocs .= ")";
        }
    }
    $query_ocs .= "{$where_multi_thread}\n                  {$limit}";
    $result_ocs = $PluginOcsinventoryngDBocs->query($query_ocs);
    $nb = $PluginOcsinventoryngDBocs->numrows($result_ocs);
    echo "\tThread #{$threadid}: {$nb} computer(s)\n";
    $fields["total_number_machines"] += $nb;
    $thread = new PluginOcsinventoryngThread();
    $notimport = new PluginOcsinventoryngNotimportedcomputer();
    for ($i = 0; $data = $PluginOcsinventoryngDBocs->fetch_array($result_ocs); $i++) {
        if ($i == $config->fields["thread_log_frequency"]) {
            $fields["status"] = PLUGIN_OCSINVENTORYNG_STATE_RUNNING;
            $thread->update($fields);
            $i = 0;
        }
        echo ".";
        $entities_id = 0;
        $action = PluginOcsinventoryngOcsServer::processComputer($data["ID"], $cfg_ocs["id"], 1);
        PluginOcsinventoryngOcsServer::manageImportStatistics($fields, $action['status']);
        switch ($action['status']) {
            case PluginOcsinventoryngOcsServer::COMPUTER_NOT_UNIQUE:
            case PluginOcsinventoryngOcsServer::COMPUTER_FAILED_IMPORT:
            case PluginOcsinventoryngOcsServer::COMPUTER_LINK_REFUSED:
                $notimport->logNotImported($cfg_ocs["id"], $data["ID"], $action);
                break;
            default:
                $notimport->cleanNotImported($cfg_ocs["id"], $data["ID"]);
                //Log detail
                $detail = new PluginOcsinventoryngDetail();
                $detail->logProcessedComputer($data["ID"], $cfg_ocs["id"], $action, $threadid, $threads_id);
                break;
        }
    }
    return $fields;
}
/**
 * @param $threads_id
 * @param $cfg_ocs
 * @param $server
 * @param $thread_nbr
 * @param $threadid
 * @param $fields
 * @param $config
**/
function plugin_ocsinventoryng_importFromOcsServer($threads_id, $cfg_ocs, $server, $thread_nbr, $threadid, $fields, $config)
{
    echo "\tThread #" . $threadid . ": import computers from server: '" . $cfg_ocs["name"] . "'\n";
    $multiThread = false;
    if ($threadid != -1 && $thread_nbr > 1) {
        $multiThread = true;
    }
    $ocsServerId = $cfg_ocs['id'];
    $ocsClient = PluginOcsinventoryngOcsServer::getDBocs($ocsServerId);
    $ocsComputers = array();
    // Build common options
    $inventoriedBefore = new DateTime('@' . (time() - 180));
    $computerOptions = array('FILTER' => array('INVENTORIED_BEFORE' => $inventoriedBefore->format('Y-m-d H:i:s')));
    // Limit the number of imported records according to config
    if ($config->fields["import_limit"] > 0) {
        $computerOptions['MAX_RECORDS'] = $config->fields["import_limit"];
    }
    // Filter tags according to config
    if ($cfg_ocs["tag_limit"] and $tag_limit = explode("\$", trim($cfg_ocs["tag_limit"]))) {
        $computerOptions['FILTER']['TAGS'] = $tag_limit;
    }
    if ($cfg_ocs["tag_limit"] and $tag_exclude = explode("\$", trim($cfg_ocs["tag_exclude"]))) {
        $computerOptions['FILTER']['EXCLUDE_TAGS'] = $tag_exclude;
    }
    // Get newly inventoried computers
    $firstQueryOptions = $computerOptions;
    if ($server->fields["max_glpidate"] != '0000-00-00 00:00:00') {
        $firstQueryOptions['FILTER']['INVENTORIED_SINCE'] = $server->fields["max_glpidate"];
    }
    $ocsResult = $ocsClient->getComputers($firstQueryOptions);
    // Filter only useful computers
    // Some conditions can't be sent to OCS, so we have to do this in a loop
    // Maybe add this to SOAP ?
    $excludeIds = array();
    foreach ($ocsResult['COMPUTERS'] as $ID => $computer) {
        if ($ID <= intval($server->fields["max_ocsid"]) and (!$multiThread or $ID % $thread_nbr == $threadid - 1)) {
            $ocsComputers[$ID] = $computer;
        }
        $excludeIds[] = $ID;
    }
    // Get computers for which checksum has changed
    $secondQueryOptions = $computerOptions;
    $secondQueryOptions['FILTER']['EXLUDE_IDS'] = $excludeIds;
    $secondQueryOptions['FILTER']['CHECKSUM'] = intval($cfg_ocs["checksum"]);
    $ocsResult = $ocsClient->getComputers($secondQueryOptions);
    // Filter only useful computers
    // Some conditions can't be sent to OCS, so we have to do this in a loop
    // Maybe add this to SOAP ?
    if (isset($ocsResult['COMPUTERS'])) {
        foreach ($ocsResult['COMPUTERS'] as $ID => $computer) {
            if ($ID <= intval($server->fields["max_ocsid"]) and (!$multiThread or $ID % $thread_nbr == $threadid - 1)) {
                $ocsComputers[$ID] = $computer;
            }
        }
    }
    // Limit the number of imported records according to config
    if ($config->fields["import_limit"] > 0 and count($ocsComputers) > $config->fields["import_limit"]) {
        $ocsComputers = array_splice($ocsComputers, $config->fields["import_limit"]);
    }
    $nb = count($ocsComputers);
    echo "\tThread #{$threadid}: {$nb} computer(s)\n";
    $fields["total_number_machines"] += $nb;
    $thread = new PluginOcsinventoryngThread();
    $notimport = new PluginOcsinventoryngNotimportedcomputer();
    $i = 0;
    foreach ($ocsComputers as $ID => $ocsComputer) {
        if ($i == $config->fields["thread_log_frequency"]) {
            $fields["status"] = PLUGIN_OCSINVENTORYNG_STATE_RUNNING;
            $thread->update($fields);
            $i = 0;
        } else {
            $i++;
        }
        echo ".";
        $entities_id = 0;
        $action = PluginOcsinventoryngOcsServer::processComputer($ID, $ocsServerId, 1);
        PluginOcsinventoryngOcsServer::manageImportStatistics($fields, $action['status']);
        switch ($action['status']) {
            case PluginOcsinventoryngOcsServer::COMPUTER_NOT_UNIQUE:
            case PluginOcsinventoryngOcsServer::COMPUTER_FAILED_IMPORT:
            case PluginOcsinventoryngOcsServer::COMPUTER_LINK_REFUSED:
                $notimport->logNotImported($ocsServerId, $ID, $action);
                break;
            default:
                $notimport->cleanNotImported($ocsServerId, $ID);
                //Log detail
                $detail = new PluginOcsinventoryngDetail();
                $detail->logProcessedComputer($ID, $ocsServerId, $action, $threadid, $threads_id);
                break;
        }
    }
    return $fields;
}