/** * @param $computers_id * @param $date * @param $computer_updates * @return array */ static function addInfocomsForComputer($computers_id, $date, $computer_updates) { global $DB; $infocom = new Infocom(); $use_date = substr($date, 0, 10); if ($infocom->getFromDBByQuery("WHERE `items_id` = {$computers_id} AND `itemtype` = 'Computer'")) { if (empty($infocom->fields['use_date']) || $infocom->fields['use_date'] == 'NULL') { //add use_date $infocom->update(array('id' => $infocom->fields['id'], 'use_date' => $use_date)); } } else { //add infocom $infocom->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'use_date' => $use_date)); } //Add lock $ocslink = new PluginOcsinventoryngOcslink(); if ($ocslink->getFromDBforComputer($computers_id)) { $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($ocslink->fields["plugin_ocsinventoryng_ocsservers_id"]); if ($cfg_ocs["use_locks"]) { $computer_updates[] = "use_date"; $query = "UPDATE `glpi_plugin_ocsinventoryng_ocslinks`\n SET `computer_update` = '" . addslashes(exportArrayToDB($computer_updates)) . "'\n WHERE `computers_id` = '{$computers_id}'"; $DB->query($query); } } return $computer_updates; }
/** * Constructor * * @param $ID ID of the ocs server ID **/ function __construct($ID) { $this->ocsservers_id = $ID; $data = PluginOcsinventoryngOcsServer::getConfig($ID); $this->dbhost = $data["ocs_db_host"]; $this->dbuser = $data["ocs_db_user"]; $this->dbpassword = rawurldecode($data["ocs_db_passwd"]); $this->dbdefault = $data["ocs_db_name"]; $this->dbenc = $data["ocs_db_utf8"] ? "utf8" : "latin1"; parent::__construct(); }
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'; Session::checkRight("plugin_ocsinventoryng", UPDATE); Html::header('OCS Inventory NG', '', "tools", "pluginocsinventoryngmenu", "sync"); $display_list = true; if (isset($_SESSION["ocs_update"]['computers'])) { if ($count = count($_SESSION["ocs_update"]['computers'])) { $percent = min(100, round(100 * ($_SESSION["ocs_update_count"] - $count) / $_SESSION["ocs_update_count"], 0)); $key = array_pop($_SESSION["ocs_update"]['computers']); $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($_SESSION["plugin_ocsinventoryng_ocsservers_id"]); $dohistory = isset($cfg_ocs['dohistory']) ? $cfg_ocs['dohistory'] : false; $action = PluginOcsinventoryngOcsServer::updateComputer($key, $_SESSION["plugin_ocsinventoryng_ocsservers_id"], $dohistory); PluginOcsinventoryngOcsServer::manageImportStatistics($_SESSION["ocs_update"]['statistics'], $action['status']); PluginOcsinventoryngOcsServer::showStatistics($_SESSION["ocs_update"]['statistics']); Html::displayProgressBar(400, $percent); Html::redirect($_SERVER['PHP_SELF']); } else { if (isset($_SESSION["ocs_update"]['statistics'])) { PluginOcsinventoryngOcsServer::showStatistics($_SESSION["ocs_update"]['statistics'], true); } else { echo "<div class='center b red'>"; _e('No synchronization: the plugin will not synchronize these elements', 'ocsinventoryng'); echo "</div>"; } unset($_SESSION["ocs_update"]);
/** * @param $threads_id * @param $ocsservers_id * @param $thread_nbr * @param $threadid * @param $fields * @param $config **/ function SecondPass($threads_id, $ocsservers_id, $thread_nbr, $threadid, $fields, $config) { $server = new PluginOcsinventoryngServer(); $ocsserver = new PluginOcsinventoryngOcsServer(); if (!PluginOcsinventoryngOcsServer::checkOCSconnection($ocsservers_id)) { echo "\tThread #" . $threadid . ": cannot contact server\n\n"; return false; } if (!$ocsserver->getFromDB($ocsservers_id)) { echo "\tThread #" . $threadid . ": cannot get OCS server information\n\n"; return false; } if (!$server->getFromDBbyOcsServer($ocsservers_id)) { echo "\tThread #" . $threadid . ": cannot get server information\n\n"; return false; } $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); return plugin_ocsinventoryng_importFromOcsServer($threads_id, $cfg_ocs, $server, $thread_nbr, $threadid, $fields, $config); }
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'; Session::checkRight("computer", READ); if (isset($_POST["force_ocs_resynch"])) { $computer = new Computer(); $computer->check($_POST['id'], UPDATE); //Get the ocs server id associated with the machine $ocsservers_id = PluginOcsinventoryngOcsServer::getByMachineID($_POST["id"]); //Update the computer $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); $dohistory = isset($cfg_ocs['dohistory']) ? $cfg_ocs['dohistory'] : false; PluginOcsinventoryngOcsServer::updateComputer($_POST["resynch_id"], $ocsservers_id, $dohistory, 1); Html::back(); } else { if (isset($_POST["update"])) { $link = new PluginOcsinventoryngOcslink(); $values["id"] = $_POST["link_id"]; $values["use_auto_update"] = $_POST["use_auto_update"]; $link->update($values); Html::back(); } else { if (isset($_POST["delete_link"])) { $comp = new Computer(); $link = new PluginOcsinventoryngOcslink(); if ($comp->getFromDB($_POST["items_id"])) {
/** * @param $target **/ function showProcesses($target) { global $DB, $CFG_GLPI; $canedit = Session::haveRight("plugin_ocsinventoryng", UPDATE); $config = new PluginOcsinventoryngConfig(); $config->getFromDB(1); $minfreq = 9999; //$task = new CronTask(); //if ($task->getFromDBbyName('PluginOcsinventoryngThread', 'CleanOldThreads')) { //First of all, deleted old processes // $this->deleteOldProcesses($task->fields['param']); // if ($task->fields['param'] > 0) { // $minfreq=$task->fields['param']; // } //} $imported_number = new PluginOcsinventoryngMiniStat(); $synchronized_number = new PluginOcsinventoryngMiniStat(); $linked_number = new PluginOcsinventoryngMiniStat(); $failed_number = new PluginOcsinventoryngMiniStat(); $notupdated_number = new PluginOcsinventoryngMiniStat(); $notunique_number = new PluginOcsinventoryngMiniStat(); $linkedrefused_number = new PluginOcsinventoryngMiniStat(); $process_time = new PluginOcsinventoryngMiniStat(); $sql = "SELECT `id`, `processid`, SUM(`total_number_machines`) AS total_machines,\n `plugin_ocsinventoryng_ocsservers_id`, `status`, COUNT(*) AS threads_number,\n MIN(`start_time`) AS starting_date, MAX(`end_time`) AS ending_date,\n TIME_TO_SEC(MAX(`end_time`)) - TIME_TO_SEC(MIN(`start_time`)) AS duree,\n SUM(`imported_machines_number`) AS imported_machines,\n SUM(`synchronized_machines_number`) AS synchronized_machines,\n SUM(`linked_machines_number`) AS linked_machines,\n SUM(`failed_rules_machines_number`) AS failed_rules_machines,\n SUM(`notupdated_machines_number`) AS notupdated_machines,\n SUM(`not_unique_machines_number`) AS not_unique_machines_number,\n SUM(`link_refused_machines_number`) AS link_refused_machines_number,\n `end_time` >= DATE_ADD(NOW(), INTERVAL - " . $minfreq . " HOUR) AS DoStat\n FROM `" . $this->getTable() . "`\n GROUP BY `processid`\n ORDER BY `id` DESC\n LIMIT 50"; $result = $DB->query($sql); echo "<div class='center'>"; echo "<form name='processes' id='processes' action='{$target}' method='post'>"; echo "<table class='tab_cadrehov'>"; echo "<tr><th colspan='16'>" . __('Processes execution of automatic actions', 'ocsinventoryng') . "</th></tr>"; echo "<tr>"; echo "<th> </th>"; echo "<th> </th>"; echo "<th>" . __('Status') . "</th>"; echo "<th>" . __('Number of threads', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Beginning date of execution', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Ending date of execution', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers imported by automatic actions', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers synchronized', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers linked', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers not imported by automatic actions', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers not updated', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers not unique', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Computers refused', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Process time execution', 'ocsinventoryng') . "</th>"; echo "<th>" . __('Server') . "</th>"; echo "<th> </th>"; echo "</th></tr>\n"; if ($DB->numrows($result)) { while ($thread = $DB->fetch_array($result)) { if ($config->fields["is_displayempty"] || $thread["status"] != PLUGIN_OCSINVENTORYNG_STATE_FINISHED || !$config->fields["is_displayempty"] && $thread["total_machines"] > 0 && $thread["status"] == PLUGIN_OCSINVENTORYNG_STATE_FINISHED) { if ($thread["DoStat"] && $thread["status"] == PLUGIN_OCSINVENTORYNG_STATE_FINISHED) { $imported_number->AddValue($thread["imported_machines"]); $synchronized_number->AddValue($thread["synchronized_machines"]); $linked_number->AddValue($thread["linked_machines"]); $failed_number->AddValue($thread["failed_rules_machines"]); $notupdated_number->AddValue($thread["notupdated_machines"]); $notunique_number->AddValue($thread["not_unique_machines_number"]); $linkedrefused_number->AddValue($thread["link_refused_machines_number"]); $process_time->AddValue($thread["duree"]); } else { if ($imported_number->GetCount() > 0) { $this->showshowStat($minfreq, $imported_number, $synchronized_number, $linked_number, $failed_number, $notupdated_number, $notunique_number, $linkedrefused_number, $process_time); $imported_number->Reset(); } } echo "<tr class='tab_bg_1'>"; echo "<td width='10'>"; if ($canedit) { echo "<input type='checkbox' name='item[" . $thread["processid"] . "]' value='1'>"; } else { echo " "; } echo "</td>"; echo "<td class='center'>"; echo "<a href=\"./thread.form.php?pid=" . $thread["processid"] . "\">" . $thread["processid"] . "</a></td>"; echo "<td class='center'>"; $this->displayProcessStatusIcon($this->getProcessStatus($thread["processid"])); echo "</td>"; echo "<td class='center'>" . $thread["threads_number"] . "</td>"; echo "<td class='center'>" . Html::convDateTime($thread["starting_date"]) . "</td>"; echo "<td class='center'>" . Html::convDateTime($thread["ending_date"]) . "</td>"; echo "<td class='center'>" . $thread["imported_machines"] . "</td>"; echo "<td class='center'>" . $thread["synchronized_machines"] . "</td>"; echo "<td class='center'>" . $thread["linked_machines"] . "</td>"; echo "<td class='center'>" . $thread["failed_rules_machines"] . "</td>"; echo "<td class='center'>" . $thread["notupdated_machines"] . "</td>"; echo "<td class='center'>" . $thread["not_unique_machines_number"] . "</td>"; echo "<td class='center'>" . $thread["link_refused_machines_number"] . "</td>"; echo "<td class='center'>"; if ($thread["status"] == PLUGIN_OCSINVENTORYNG_STATE_FINISHED) { echo Html::timestampToString($thread["duree"]); } else { echo Dropdown::EMPTY_VALUE; } echo "</td>"; echo "<td class='center'>"; if ($thread["plugin_ocsinventoryng_ocsservers_id"] != -1) { $ocsConfig = PluginOcsinventoryngOcsServer::getConfig($thread["plugin_ocsinventoryng_ocsservers_id"]); echo "<a href=\"ocsserver.form.php?id=" . $ocsConfig["id"] . "\">" . $ocsConfig["name"] . "</a>"; } else { _e('All servers', 'ocsinventoryng'); } echo "</td>"; echo "<td class='center'>"; echo "<a href=\"detail.php?reset=reset_before&field[0]=" . "5&contains[0]=^" . $thread["processid"] . '$">' . "<img src='" . $CFG_GLPI["root_doc"] . "/pics/rdv.png'</a></td>"; echo "</tr>\n"; } } } if ($imported_number->GetCount() > 0) { $this->showshowStat($minfreq, $imported_number, $synchronized_number, $linked_number, $failed_number, $notupdated_number, $notunique_number, $linkedrefused_number, $process_time); } echo "</table>"; if ($canedit) { Html::openArrowMassives("processes"); Html::closeArrowMassives(array("delete_processes" => _x('button', 'Delete permanently'))); } Html::closeForm(); }
/** * if Computer_Item deleted * * @param $comp Computer_Item object **/ static function purgeComputer_Item(Computer_Item $comp) { global $DB; if ($device = getItemForItemtype($comp->fields['itemtype'])) { if ($device->getFromDB($comp->fields['items_id'])) { if (isset($comp->input['_ocsservers_id'])) { $ocsservers_id = $comp->input['_ocsservers_id']; } else { $ocsservers_id = PluginOcsinventoryngOcsServer::getByMachineID($comp->fields['computers_id']); } if ($ocsservers_id > 0) { //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, $comp->fields['itemtype']); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi // if ($decoConf == "delete") { // $tmp["id"] = $comp->fields['items_id']; // $device->delete(array('id' => $tmp['id']), 1); // Put periph in dustbin // } else if ($decoConf == "trash") { // $tmp["id"] = $comp->fields['items_id']; // $device->delete(array('id' => $tmp['id']), 0); // } if ($decoConf == "delete") { $tmp["id"] = $comp->getID(); $query = "DELETE\n FROM `glpi_computers_items`\n WHERE `id`='" . $tmp['id'] . "'"; $result = $DB->query($query); //Put periph in dustbin } else { if ($decoConf == "trash") { $tmp["id"] = $comp->getID(); $query = "UPDATE\n `glpi_computers_items`\n\t\t\t\t\t\t SET `is_deleted` = 1\n WHERE `id`='" . $tmp['id'] . "'"; $result = $DB->query($query); } } } } // $ocsservers_id>0 } } }
static function showOcsReportsConsole($id) { $ocsconfig = PluginOcsinventoryngOcsServer::getConfig($id); echo "<div class='center'>"; if ($ocsconfig["ocs_url"] != '') { echo "<iframe src='" . $ocsconfig["ocs_url"] . "/index.php?multi=4' width='95%' height='650'>"; } echo "</div>"; }
/** * @param $ID * @param $plugin_ocsinventoryng_ocsservers_id * @return array */ static function updateSnmp($ID, $plugin_ocsinventoryng_ocsservers_id) { global $DB; $query = "SELECT * FROM `glpi_plugin_ocsinventoryng_snmpocslinks` \n WHERE `id` = " . $ID . " \n AND `plugin_ocsinventoryng_ocsservers_id` = " . $plugin_ocsinventoryng_ocsservers_id; $rep = $DB->query($query); while ($data = $DB->fetch_array($rep)) { $ocsid = $data['ocs_id']; $itemtype = $data['itemtype']; $items_id = $data['items_id']; $linked = $data['linked']; } $ocsClient = PluginOcsinventoryngOcsServer::getDBocs($plugin_ocsinventoryng_ocsservers_id); $ocsSnmp = $ocsClient->getSnmpDevice($ocsid); $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id); $loc_id = 0; $dom_id = 0; if ($cfg_ocs['importsnmp_location'] && $linked == 0 || $cfg_ocs['linksnmp_location'] && $linked) { $loc_id = Dropdown::importExternal('Location', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsSnmp['META']['LOCATION'])); } if ($cfg_ocs['importsnmp_domain'] && $linked == 0 || $cfg_ocs['linksnmp_domain'] && $linked) { $dom_id = Dropdown::importExternal('Domain', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsSnmp['META']['DOMAIN'])); } if ($itemtype == "Printer") { self::addOrUpdatePrinter($plugin_ocsinventoryng_ocsservers_id, $itemtype, $items_id, $ocsSnmp, $loc_id, $dom_id, "update", $linked); $now = date("Y-m-d H:i:s"); $sql = "UPDATE `glpi_plugin_ocsinventoryng_snmpocslinks` SET `last_update` = '" . $now . "' WHERE `id` = " . $ID . ";"; $DB->query($sql); return array('status' => PluginOcsinventoryngOcsServer::SNMP_SYNCHRONIZED); } else { if ($itemtype == "NetworkEquipment") { self::addOrUpdateNetworkEquipment($plugin_ocsinventoryng_ocsservers_id, $itemtype, $items_id, $ocsSnmp, $loc_id, $dom_id, "update", $linked); $now = date("Y-m-d H:i:s"); $sql = "UPDATE `glpi_plugin_ocsinventoryng_snmpocslinks` SET `last_update` = '" . $now . "' WHERE `id` = " . $ID . ";"; $DB->query($sql); return array('status' => PluginOcsinventoryngOcsServer::SNMP_SYNCHRONIZED); } else { if ($itemtype == "Computer") { self::addOrUpdateComputer($plugin_ocsinventoryng_ocsservers_id, $itemtype, $items_id, $ocsSnmp, $loc_id, $dom_id, "update", $linked); $now = date("Y-m-d H:i:s"); $sql = "UPDATE `glpi_plugin_ocsinventoryng_snmpocslinks` SET `last_update` = '" . $now . "' WHERE `id` = " . $ID . ";"; $DB->query($sql); return array('status' => PluginOcsinventoryngOcsServer::SNMP_SYNCHRONIZED); } else { if ($itemtype == "Peripheral" || $itemtype == "Phone") { self::addOrUpdateOther($plugin_ocsinventoryng_ocsservers_id, $itemtype, $items_id, $ocsSnmp, $loc_id, $dom_id, "update", $linked); $now = date("Y-m-d H:i:s"); $sql = "UPDATE `glpi_plugin_ocsinventoryng_snmpocslinks` SET `last_update` = '" . $now . "' WHERE `id` = " . $ID . ";"; $DB->query($sql); return array('status' => PluginOcsinventoryngOcsServer::SNMP_SYNCHRONIZED); } } } } return array('status' => PluginOcsinventoryngOcsServer::SNMP_NOTUPDATED); }
/** * @param $item */ function plugin_ocsinventoryng_item_update($item) { global $DB; if ($item->fields['itemtype'] == "Computer") { if (in_array('use_date', $item->updates)) { $query = "SELECT *\r\n FROM `glpi_plugin_ocsinventoryng_ocslinks`\r\n WHERE `computers_id` = '" . $item->fields['items_id'] . "'"; $result = $DB->query($query); if ($DB->numrows($result) == 1) { $line = $DB->fetch_assoc($result); $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($line["plugin_ocsinventoryng_ocsservers_id"]); if ($cfg_ocs["use_locks"]) { $computer_updates = importArrayFromDB($line["computer_update"]); //Add lock $computer_updates[] = "use_date"; $query = "UPDATE `glpi_plugin_ocsinventoryng_ocslinks`\r\n SET `computer_update` = '" . addslashes(exportArrayToDB($computer_updates)) . "'\r\n WHERE `computers_id` = '" . $item->fields['items_id'] . "'"; $DB->query($query); } } } } }
/** * if Computer_Item deleted * * @param $comp Computer_Item object **/ static function purgeComputer_Item(Computer_Item $comp) { if ($device = getItemForItemtype($comp->fields['itemtype'])) { if ($device->getFromDB($comp->fields['items_id'])) { if (isset($comp->input['_ocsservers_id'])) { $ocsservers_id = $comp->input['_ocsservers_id']; } else { $ocsservers_id = PluginOcsinventoryngOcsServer::getByMachineID($comp->fields['computers_id']); } if ($ocsservers_id > 0) { //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, $comp->fields['itemtype']); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi if ($decoConf == "delete") { $tmp["id"] = $comp->fields['items_id']; $device->delete($tmp, 1); //Put periph in dustbin } else { if ($decoConf == "trash") { $tmp["id"] = $comp->fields['items_id']; $device->delete($tmp, 0); } } } } // $ocsservers_id>0 } } }
/** * * Import peripherals from OCS * @since 1.0 * @param $cfg_ocs OCSNG mode configuration * @param $computers_id computer's id in GLPI * @param $ocsid computer's id in OCS * @param $ocsservers_id OCS server id * @param $entity the entity in which the peripheral will be created * @param $dohistory record in history link between peripheral and computer */ static function importPeripheral($cfg_ocs, $computers_id, $ocsservers_id, $ocsComputer, $entity, $dohistory) { global $DB; $already_processed = array(); $p = new Peripheral(); $conn = new Computer_Item(); if (isset($ocsComputer["INPUTS"])) { if (count($ocsComputer["INPUTS"]) > 0) { foreach ($ocsComputer["INPUTS"] as $peripheral) { if ($peripheral["CAPTION"] !== '') { $peripherals[] = $peripheral; } } if (count($peripherals) > 0) { foreach ($peripherals as $peripheral) { $peripheral = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($peripheral)); $periph = array(); $periph["name"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["CAPTION"]); //Look for a monitor with the same name (and serial if possible) already connected //to this computer $query = "SELECT `p`.`id`, `gci`.`is_deleted`\n FROM `glpi_printers` as `p`, `glpi_computers_items` as `gci`\n WHERE `p`.`id` = `gci`.`items_id`\n AND `gci`.`is_dynamic`='1'\n AND `computers_id`='{$computers_id}'\n AND `itemtype`='Peripheral'\n AND `p`.`name`='" . $periph["name"] . "'"; $results = $DB->query($query); $id = false; $lock = false; if ($DB->numrows($results) > 0) { $id = $DB->result($results, 0, 'id'); $lock = $DB->result($results, 0, 'is_deleted'); } if (!$id) { // Clean peripheral object $p->reset(); if ($peripheral["MANUFACTURER"] != "NULL") { $periph["brand"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["MANUFACTURER"]); } if ($peripheral["INTERFACE"] != "NULL") { $periph["comment"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["INTERFACE"]); } $periph["peripheraltypes_id"] = Dropdown::importExternal('PeripheralType', $peripheral["TYPE"]); $id_periph = 0; if ($cfg_ocs["import_periph"] == 1) { //Config says : manage peripherals as global //check if peripherals already exists in GLPI $periph["is_global"] = 1; $query = "SELECT `id`\n FROM `glpi_peripherals`\n WHERE `name` = '" . $periph["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_periph = $DB->result($result_search, 0, "id"); } else { $input = $periph; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } else { if ($cfg_ocs["import_periph"] == 2) { //Config says : manage peripherals as single units //Import all peripherals as non global. $input = $periph; $input["is_global"] = 0; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } if ($id_periph) { $already_processed[] = $id_periph; $conn = new Computer_Item(); if ($connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Peripheral', 'items_id' => $id_periph, '_no_history' => !$dohistory, 'is_dynamic' => 1))) { //Update column "is_deleted" set value to 0 and set status to default $input = array(); $input["id"] = $id_periph; $input["is_deleted"] = 0; $input["entities_id"] = $entity; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $p->update($input); } } } else { $already_processed[] = $id; } } } } } //Look for all peripherals, not locked, not linked to the computer anymore $query = "SELECT `id`\n FROM `glpi_computers_items`\n WHERE `itemtype`='Peripheral'\n AND `computers_id`='{$computers_id}'\n AND `is_dynamic`='1'\n AND `is_deleted`='0'"; if (!empty($already_processed)) { $query .= "AND `items_id` NOT IN (" . implode(',', $already_processed) . ")"; } foreach ($DB->request($query) as $data) { // Delete all connexions //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, 'Peripheral'); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi if ($decoConf == "delete") { $query = "DELETE\n FROM `glpi_computers_items`\n WHERE `id`='" . $data['id'] . "'"; $result = $DB->query($query); //Put periph in dustbin } else { if ($decoConf == "trash") { $query = "UPDATE\n `glpi_computers_items`\n SET `is_deleted` = 1\n WHERE `id`='" . $data['id'] . "'"; $result = $DB->query($query); } } } // foreach ($DB->request($query) as $data){ // Delete all connexions // $conn->delete(array('id' => $data['id'], // '_ocsservers_id' => $ocsservers_id), true); } }
/** * import ipdiscover object * @param type $ipDiscoveryObject array * @param type $plugin_ocsinventoryng_ocsservers_id integer * @param $subnet * @return array */ static function importIpDiscover($ipDiscoveryObject, $plugin_ocsinventoryng_ocsservers_id, $subnet) { global $DB; $id = null; $identify = false; $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id); if (isset($ipDiscoveryObject["ocsItemType"]) && $ipDiscoveryObject["ocsItemType"] == Dropdown::EMPTY_VALUE) { return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT); } if ($ipDiscoveryObject["itemDescription"] == '') { return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT); } if ($ipDiscoveryObject["itemName"] == "") { $ipDiscoveryObject["itemName"] = $ipDiscoveryObject["itemDescription"]; } switch ($ipDiscoveryObject["glpiItemType"]) { //empty dropdown value case '0': return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT); } if (isset($ipDiscoveryObject["ocsItemType"])) { $identify = true; } $mac = $ipDiscoveryObject["macAdress"]; $netPort = new NetworkPort(); $netPort->getFromDBByQuery("WHERE `mac` = '{$mac}' "); if (count($netPort->fields) < 1) { $input = array('is_dynamic' => 1, 'locations_id' => 0, 'domains_id' => 0, 'entities_id' => $ipDiscoveryObject["entity"], 'name' => $ipDiscoveryObject["itemName"], 'comment' => $ipDiscoveryObject["itemDescription"]); $device = new $ipDiscoveryObject["glpiItemType"](); $id = $device->add($input, array(), $cfg_ocs['history_devices']); //ipdiscover link $date = date("Y-m-d H:i:s"); $glpiType = $ipDiscoveryObject["glpiItemType"]; $ip = $ipDiscoveryObject["itemIp"]; $glpiQuery = "INSERT INTO `glpi_plugin_ocsinventoryng_ipdiscoverocslinks`\n (`items_id`,`itemtype`,`macaddress`,`last_update`,`subnet`,`plugin_ocsinventoryng_ocsservers_id`)\n VALUES('{$id}','{$glpiType}','{$mac}','{$date}','{$subnet}','{$plugin_ocsinventoryng_ocsservers_id}')"; $DB->query($glpiQuery); //add port $port_input = array('name' => $ipDiscoveryObject["itemName"] . "-" . $ip, 'mac' => $mac, 'items_id' => $id, 'itemtype' => $glpiType, 'instantiation_type' => "NetworkPortEthernet", "entities_id" => $ipDiscoveryObject["entity"], "NetworkName__ipaddresses" => array("-100" => $ip), '_create_children' => 1, 'is_deleted' => 0); $netPort->add($port_input, array(), $cfg_ocs['history_network']); /* $netPortInput = array( "itemtype" => $glpiType, "items_id" => $id, 'entities_id' => $ipDiscoveryObject["entity"], "name" => $ipDiscoveryObject["itemName"] . "-" . $ip, "instantiation_type" => "NetworkPortEthernet", "mac" => $mac ); $netPort->splitInputForElements($netPortInput); $NewNetPortId = $netPort->add($netPortInput); $netPort->updateDependencies(1); //make link to IPAdress manualy //add ipAdress $networkName= new NetworkName(); $networkNameId=$networkName->add(array("items_id"=>$NewNetPortId,"itemtype"=>"NetworkPort")); $ipAdresses = new IPAddress(); $input = array('name' => $ip, 'itemtype' => 'NetworkName', 'items_id' => $networkNameId, 'is_deleted' => 0, 'mainitems_id'=>$id, 'mainitemtype'=>$glpiType); $ipAdresses->add($input);*/ } if ($id && $identify) { //identify object //WAS IS DAS ? CHMA $userId = Session::getLoginUserID(); $query = "SELECT `glpi_users`.`name` \n FROM `glpi_users`\n WHERE glpi_users.id like '{$userId}'"; $queryResult = $DB->query($query); $userAssoc = $DB->fetch_assoc($queryResult); if ($userAssoc) { $ocsClient = new PluginOcsinventoryngOcsServer(); $DBOCS = $ocsClient->getDBocs($plugin_ocsinventoryng_ocsservers_id)->getDB(); $ocsType = $ipDiscoveryObject["ocsItemType"]; $description = $ipDiscoveryObject["itemDescription"]; $user = $userAssoc["name"]; $ocsQuery = "INSERT INTO `network_devices` (`description`,`type`,`macaddr`,`user`)\n VALUES('{$description}','{$ocsType}','{$mac}','{$user}')"; $DBOCS->query($ocsQuery); } } if ($id) { return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_IMPORTED); } else { Session::addMessageAfterRedirect($mac . " : " . __('Unable to add. an object with same MAC address already exists.', 'ocsinventoryng'), false, ERROR); return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT); } }