function getDatasForTemplate($event, $options = array()) { global $CFG_GLPI, $DB; $this->datas['##notimported.entity##'] = Dropdown::getDropdownName('glpi_entities', $options['entities_id']); foreach ($options['notimported'] as $id => $item) { $tmp = array(); $tmp['##notimported.name##'] = $item['name']; $tmp['##notimported.serial##'] = $item['serial']; $tmp['##notimported.entity##'] = Dropdown::getDropdownName('glpi_entities', $options['entities_id']); $tmp['##notimported.ocsid##'] = $item['ocsid']; $tmp['##notimported.deviceid##'] = $item['ocs_deviceid']; $tmp['##notimported.tag##'] = $item['tag']; $tmp['##notimported.ocsserver##'] = Dropdown::getDropdownName('glpi_plugin_ocsinventoryng_ocsservers', $item['ocsid']); $tmp['##notimported.reason##'] = PluginOcsinventoryngNotimportedcomputer::getReason($item['reason']); $url = $CFG_GLPI["url_base"] . "/index.php?redirect=plugin_ocsinventoryng_" . $item['id']; $tmp['##notimported.url##'] = urldecode($url); $this->datas['notimported'][] = $tmp; } $this->getTags(); foreach ($this->tag_descriptions[NotificationTarget::TAG_LANGUAGE] as $tag => $values) { if (!isset($this->datas[$tag])) { $this->datas[$tag] = $values['label']; } } }
/** * @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; }
/** * @since version 0.85 * * @see CommonDBTM::processMassiveActionsForOneItemtype() * @param MassiveAction $ma * @param CommonDBTM $item * @param array $ids * @return nothing|void */ static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids) { $notimport = new PluginOcsinventoryngNotimportedcomputer(); switch ($ma->getAction()) { case "plugin_ocsinventoryng_import": $input = $ma->getInput(); foreach ($ids as $id) { if (PluginOcsinventoryngNotimportedcomputer::computerImport(array('id' => $id, 'force' => true, 'entity' => $input['entity']))) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); } } return; case "plugin_ocsinventoryng_replayrules": foreach ($ids as $id) { if (PluginOcsinventoryngNotimportedcomputer::computerImport(array('id' => $id))) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); } } return; case "plugin_ocsinventoryng_delete": foreach ($ids as $id) { if ($notimport->deleteNotImportedComputer($id)) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); } } return; } parent::processMassiveActionsForOneItemtype($ma, $item, $ids); }
/** * @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 $type * @param $id * @param $data * @param $num **/ function plugin_ocsinventoryng_giveItem($type, $id, $data, $num) { global $CFG_GLPI, $DB; $searchopt =& Search::getOptions($type); $table = $searchopt[$id]["table"]; $field = $searchopt[$id]["field"]; switch ("{$table}.{$field}") { case "glpi_plugin_ocsinventoryng_details.action": $detail = new PluginOcsinventoryngDetail(); return $detail->giveActionNameByActionID($data["ITEM_{$num}"]); case "glpi_plugin_ocsinventoryng_details.computers_id": $comp = new Computer(); $comp->getFromDB($data["ITEM_{$num}"]); return "<a href='" . Toolbox::getItemTypeFormURL('Computer') . "?id=" . $data["ITEM_{$num}"] . "'>" . $comp->getName() . "</a>"; case "glpi_plugin_ocsinventoryng_details.plugin_ocsinventoryng_ocsservers_id": $ocs = new PluginOcsinventoryngOcsServer(); $ocs->getFromDB($data["ITEM_{$num}"]); return "<a href='" . Toolbox::getItemTypeFormURL('PluginOcsinventoryngOcsServer') . "?id=" . $data["ITEM_{$num}"] . "'>" . $ocs->getName() . "</a>"; case "glpi_plugin_ocsinventoryng_details.rules_id": $detail = new PluginOcsinventoryngDetail(); $detail->getFromDB($data['id']); return PluginOcsinventoryngNotimportedcomputer::getRuleMatchedMessage($detail->fields['rules_id']); case "glpi_plugin_ocsinventoryng_notimportedcomputers.reason": return PluginOcsinventoryngNotimportedcomputer::getReason($data["ITEM_{$num}"]); } return ''; }
ocsinventoryng is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ocsinventoryng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ocsinventoryng. If not, see <http://www.gnu.org/licenses/>. -------------------------------------------------------------------------- */ include '../../../inc/includes.php'; $dropdown = new PluginOcsinventoryngNotimportedcomputer(); if (isset($_POST['action'])) { switch ($_POST['action']) { case 'plugin_ocsinventoryng_import': $_POST['force'] = true; case 'plugin_ocsinventoryng_replayrules': if (PluginOcsinventoryngNotimportedcomputer::computerImport($_POST)) { $dropdown->redirectToList(); } else { Html::redirect(Html::getItemTypeFormURL('PluginOcsinventoryngNotimportedcomputer') . '?id=' . $_POST['id']); } break; case 'plugin_ocsinventoryng_link': $dropdown->linkComputer($_POST); $dropdown->redirectToList(); break;
/** * @param $params array **/ static function linkComputer($params = array()) { if (isset($params['id'])) { $notimported = new PluginOcsinventoryngNotimportedcomputer(); $notimported->getFromDB($params['id']); $changes = self::getOcsComputerInfos($notimported->fields); if (PluginOcsinventoryngOcsServer::linkComputer($notimported->fields['ocsid'], $notimported->fields['plugin_ocsinventoryng_ocsservers_id'], $params['computers_id'])) { $notimported->delete(array('id' => $params['id'])); //If serial has been changed in order to import computer if (in_array('serial', $changes)) { PluginOcsinventoryngOcsServer::mergeOcsArray($params['id'], array('serial'), "computer_update"); } } } }