/** * @param $cfg_ocs * @param $ocsComputer * @param $computers_id * @param $entities_id */ static function importNetwork($cfg_ocs, $ocsComputer, $computers_id, $entities_id) { global $DB; // Group by DESCRIPTION, MACADDR, TYPE, TYPEMIB, SPEED, VIRTUALDEV // to get an array in IPADDRESS $ocsNetworks = array(); foreach ($ocsComputer as $ocsNetwork) { $key = $ocsNetwork['DESCRIPTION'] . $ocsNetwork['MACADDR'] . $ocsNetwork['TYPE'] . $ocsNetwork['TYPEMIB'] . $ocsNetwork['SPEED'] . $ocsNetwork['VIRTUALDEV']; if (!isset($ocsNetworks[$key])) { $ocsNetworks[$key] = $ocsNetwork; $ocsNetworks[$key]['IPADDRESS'] = array($ocsNetwork['IPADDRESS']); } else { $ocsNetworks[$key]['IPADDRESS'][] = $ocsNetwork['IPADDRESS']; } } $network_ports = array(); $network_ifaces = array(); foreach ($ocsNetworks as $line) { $line = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line)); $mac = $line['MACADDR']; if (!isset($network_ports[$mac])) { $network_ports[$mac] = array('virtual' => array()); } $name = PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $line['DESCRIPTION']); if (!empty($line['IPADDRESS'])) { $ip = $line['IPADDRESS']; } else { $ip = false; } $networkport_type = new PluginOcsinventoryngNetworkPortType(); $networkport_type->getFromTypeAndTypeMIB($line); $speed = NetworkPortEthernet::transformPortSpeed($line['SPEED'], false); if (!empty($speed)) { $networkport_type->fields['speed'] = $speed; } $values = array('name' => $name, 'type' => array_push($network_ifaces, $networkport_type) - 1, 'ip' => $ip, 'result' => $line); // Virtual dev can be : // 1°) specifically defined from OCS // 2°) if there is already one main device // 3°) if the networkport is issued by VMWare if (isset($line['VIRTUALDEV']) && $line['VIRTUALDEV'] == '1' || isset($network_ports[$mac]['main']) || preg_match('/^vm(k|nic)([0-9]+)$/', $name)) { $network_ports[$mac]['virtual'][] = $values; } else { $network_ports[$mac]['main'] = $values; } } $already_known_ports = array(); $already_known_ifaces = array(); foreach ($network_ports as $mac => $ports) { if (isset($ports['main'])) { $main = $ports['main']; $type = $network_ifaces[$main['type']]; // First search for the Network Card $item_device = new Item_DeviceNetworkCard(); $item_device->getFromDBByQuery("INNER JOIN `glpi_devicenetworkcards`\n ON (`glpi_devicenetworkcards`.`designation`='" . $main['name'] . "')\n WHERE `glpi_items_devicenetworkcards`.`itemtype`='Computer'\n AND `glpi_items_devicenetworkcards`.`items_id`='{$computers_id}'\n AND `glpi_items_devicenetworkcards`.`mac`='{$mac}'\n AND `glpi_items_devicenetworkcards`.`devicenetworkcards_id`=\n `glpi_devicenetworkcards`.`id`"); // If not found, then, create it if ($item_device->isNewItem()) { $deviceNetworkCard = new DeviceNetworkCard(); $device_input = array('designation' => $main['name'], 'bandwidth' => $type->fields['speed'], 'entities_id' => $entities_id); $net_id = $deviceNetworkCard->import($device_input); if ($net_id) { $item_device->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicenetworkcards_id' => $net_id, 'mac' => $mac, '_no_history' => !$cfg_ocs['history_network'], 'is_dynamic' => 1, 'is_deleted' => 0)); } } if (!$item_device->isNewItem()) { $already_known_ifaces[] = $item_device->getID(); } if ($type->fields['instantiation_type'] == __CLASS__) { $result = $main['result']; $inst_input = array('TYPE' => $result['TYPE'], 'TYPEMIB' => $result['TYPEMIB'], 'speed' => $result['SPEED']); } else { $inst_input = $type->fields; foreach (array('id', 'name', 'OCS_TYPE', 'OCS_TYPEMIB', 'instantiation_type', 'comment') as $field) { unset($inst_input[$field]); } } $inst_input['items_devicenetworkcards_id'] = $item_device->getID(); $networkports_id = self::updateNetworkPort($mac, $main['name'], $computers_id, $type->fields['instantiation_type'], $inst_input, $main['ip'], false, $cfg_ocs['history_network'], $already_known_ports); if ($networkports_id < 0) { continue; } $already_known_ports[] = $networkports_id; } else { $networkports_id = 0; } foreach ($ports['virtual'] as $port) { $inst_input = array('networkports_id_alias' => $networkports_id); $id = self::updateNetworkPort($mac, $port['name'], $computers_id, 'NetworkPortAlias', $inst_input, $port['ip'], true, $cfg_ocs['history_network'], $already_known_ports); if ($id > 0) { $already_known_ports[] = $id; } } } $query = "SELECT `id`\n FROM `glpi_networkports`\n WHERE `itemtype` = 'Computer'\n AND `items_id` = '{$computers_id}'\n AND `is_dynamic` = '1'"; if (count($already_known_ports) > 0) { $query .= " AND `id` NOT IN ('" . implode("', '", $already_known_ports) . "')"; } $network_ports = new NetworkPort(); foreach ($DB->request($query) as $line) { $network_ports->delete($line, true, $cfg_ocs['history_network']); } $query = "SELECT `id`\n FROM `glpi_items_devicenetworkcards`\n WHERE `itemtype` = 'Computer'\n AND `items_id` = '{$computers_id}'\n AND `is_dynamic` = '1'"; if (count($already_known_ifaces) > 0) { $query .= " AND `id` NOT IN ('" . implode("', '", $already_known_ifaces) . "')"; } $item_device = new Item_DeviceNetworkCard(); foreach ($DB->request($query) as $line) { $item_device->delete($line, true, $cfg_ocs['history_network']); } }