Exemplo n.º 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;
}
 /**
  * @param $params array
  *
  * @return bool
  * @return bool
  */
 static function computerImport($params = array())
 {
     if (isset($params['id'])) {
         $notimported = new PluginOcsinventoryngNotimportedcomputer();
         $notimported->getFromDB($params['id']);
         $changes = self::getOcsComputerInfos($notimported->fields);
         if (isset($params['force'])) {
             $result = PluginOcsinventoryngOcsServer::processComputer($notimported->fields['ocsid'], $notimported->fields['plugin_ocsinventoryng_ocsservers_id'], 0, $params['entity'], 0);
         } else {
             $result = PluginOcsinventoryngOcsServer::processComputer($notimported->fields['ocsid'], $notimported->fields['plugin_ocsinventoryng_ocsservers_id'], 0, -1, -1);
         }
         if (in_array($result['status'], array(PluginOcsinventoryngOcsServer::COMPUTER_IMPORTED, PluginOcsinventoryngOcsServer::COMPUTER_LINKED, PluginOcsinventoryngOcsServer::COMPUTER_SYNCHRONIZED))) {
             $notimported->delete(array('id' => $params['id']));
             //If serial has been changed in order to import computer
             if (in_array('serial', $changes)) {
                 PluginOcsinventoryngOcsServer::mergeOcsArray($result['computers_id'], array('serial'), "computer_update");
             }
             return true;
         } else {
             Session::addMessageAfterRedirect(self::getReason($result['status']), false, ERROR);
             return false;
         }
         $tmp = $notimported->fields;
         $tmp['reason'] = $result['status'];
         if (isset($result['entities_id'])) {
             $tmp["entities_id"] = $result['entities_id'];
         } else {
             $tmp['entities_id'] = 0;
         }
         $tmp["rules_id"] = json_encode($result['rule_matched']);
         $notimported->update($tmp);
         return false;
     }
 }
Exemplo n.º 4
0
if (isset($_SESSION["ocs_import"]["id"])) {
    if ($count = count($_SESSION["ocs_import"]["id"])) {
        $percent = min(100, round(100 * ($_SESSION["ocs_import_count"] - $count) / $_SESSION["ocs_import_count"], 0));
        $key = array_pop($_SESSION["ocs_import"]["id"]);
        if (isset($_SESSION["ocs_import"]["entities_id"][$key])) {
            $entity = $_SESSION["ocs_import"]["entities_id"][$key];
        } else {
            $entity = -1;
        }
        if (isset($_SESSION["ocs_import"]["locations_id"][$key])) {
            $location = $_SESSION["ocs_import"]["locations_id"][$key];
        } else {
            $location = -1;
        }
        $conf = PluginOcsinventoryngOcsServer::getConfig($_SESSION["plugin_ocsinventoryng_ocsservers_id"]);
        $action = PluginOcsinventoryngOcsServer::processComputer($key, $_SESSION["plugin_ocsinventoryng_ocsservers_id"], 0, $entity, $location);
        PluginOcsinventoryngOcsServer::manageImportStatistics($_SESSION["ocs_import"]['statistics'], $action['status']);
        PluginOcsinventoryngOcsServer::showStatistics($_SESSION["ocs_import"]['statistics']);
        Html::displayProgressBar(400, $percent);
        Html::redirect($_SERVER['PHP_SELF']);
    } else {
        //displayProgressBar(400, 100);
        PluginOcsinventoryngOcsServer::showStatistics($_SESSION["ocs_import"]['statistics'], true);
        unset($_SESSION["ocs_import"]);
        echo "<div class='center b'><br>";
        echo "<a href='" . $_SERVER['PHP_SELF'] . "'>" . __('Back') . "</a></div>";
        $display_list = false;
    }
}
if (!isset($_POST["import_ok"])) {
    if (!isset($_GET['check'])) {