/**
  * Get port OID list for the SNMP model && create ports in DB if they don't exists
  *
  * @param $ID_Device : ID of device
  * @param $type : type of device (NETWORKING_TYPE, PRINTER_TYPE ...)
  * @param $oidsModel : oid list from model SNMP
  * @param $oidvalues : list of values from agent query
  * @param $ArrayPort_LogicalNum_SNMPNum : array logical port number => SNMP port number (ifindex)
  * @param $ArrayPort_LogicalNum_SNMPName : array logical port number => SNMP Port name
  *
  * @return $oidList : array with logic number => portsID from snmp
  *
  **/
 static function getOIDPorts($ID_Device, $type, $oidsModel, $oidvalues, $ArrayPort_LogicalNum_SNMPName, $ArrayPort_LogicalNum_SNMPNum)
 {
     global $DB, $LANG;
     if ($_SESSION['fusioninventory_logs'] == "1") {
         $logs = new PluginFusioninventoryLogs();
     }
     $manufCisco = new PluginFusioninventoryManufacturerCisco();
     $netwire = new Netwire();
     $np = new Netport();
     $ptp = new PluginFusioninventoryPort();
     if ($_SESSION['fusioninventory_logs'] == "1") {
         $logs->write("fusioninventory_fullsync", ">>>>>>>>>> Get OID ports list (SNMP model) and create ports in DB if not exists <<<<<<<<<<", $type, $ID_Device, 1);
     }
     $portcounter = $oidvalues[$oidsModel[1][0][""]][""];
     if ($_SESSION['fusioninventory_logs'] == "1") {
         $logs->write("fusioninventory_fullsync", "oid port counter : " . $oidsModel[1][0][""] . " = " . $portcounter, $type, $ID_Device, 1);
     }
     $oid_ifType = $oidsModel[0][1]['ifType'];
     if ($_SESSION['fusioninventory_logs'] == "1") {
         $logs->write("fusioninventory_fullsync", "type of port : " . $oid_ifType, $type, $ID_Device, 1);
     }
     asort($ArrayPort_LogicalNum_SNMPNum);
     // Reorder ports with good logic number
     $query = "SELECT `last_PID_update`\n             FROM `glpi_plugin_fusioninventory_networking`\n             WHERE `FK_networking`='" . $ID_Device . "'\n                   AND `last_PID_update`='0';";
     $result = $DB->query($query);
     if ($DB->numrows($result) == 1) {
         foreach ($ArrayPort_LogicalNum_SNMPNum as $num => $ifIndex) {
             $query_update = "UPDATE `glpi_networking_ports`\n                          SET `logical_number`='" . $ifIndex . "'\n                          WHERE `on_device`='" . $ID_Device . "'\n                                AND `device_type`='" . $type . "'\n                                AND `name`='" . $ArrayPort_LogicalNum_SNMPName[$num] . "';";
             $DB->query($query_update);
         }
     }
     // Get query SNMP to have number of ports
     if (isset($portcounter) and !empty($portcounter)) {
         // ** Add ports in DataBase if they don't exists
         $logicalnumberlist = "(";
         foreach ($ArrayPort_LogicalNum_SNMPNum as $num => $ifIndex) {
             //$i is the logical number
             $logicalnumberlist .= $ifIndex . ",";
             //for ($i=0 ; $i < $portcounter ; $i++) {
             // Get type of port
             $ifType = $oidvalues[$oid_ifType . "." . $ArrayPort_LogicalNum_SNMPNum[$num]][""];
             $oidList[$i] = $ArrayPort_LogicalNum_SNMPNum[$num];
             if (strstr($ifType, "ethernetCsmacd") or $ifType == "6" or $ifType == "ethernet-csmacd(6)" or strstr($ifType, "iso88023Csmacd") or $ifType == "7") {
                 $goodname = 1;
                 if ($manufCisco->ListVirtualPorts($oidvalues[".1.3.6.1.2.1.1.1.0"][""], $ArrayPort_LogicalNum_SNMPName[$num]) == true) {
                     $goodname = 0;
                     $deleteportname[] = $ifIndex;
                     unset($oidList[$ifIndex]);
                 }
                 if ($goodname == 1) {
                     $query = "SELECT `ID`, `name`\n                         FROM `glpi_networking_ports`\n                         WHERE `on_device`='" . $ID_Device . "'\n                               AND `device_type`='" . $type . "'\n                               AND `logical_number`='" . $ifIndex . "';";
                     $result = $DB->query($query);
                     if ($DB->numrows($result) == 0) {
                         unset($array);
                         $array["logical_number"] = $ifIndex;
                         $array["name"] = $ArrayPort_LogicalNum_SNMPName[$num];
                         $array["on_device"] = $ID_Device;
                         $array["device_type"] = $type;
                         $IDport = $np->add($array);
                         Event::log(0, "networking", 5, "inventory", "FusionInventory " . $LANG["log"][70]);
                         if ($_SESSION['fusioninventory_logs'] == "1") {
                             $logs->write("fusioninventory_fullsync", "Add port in DB (glpi_networking_ports) : " . $ArrayPort_LogicalNum_SNMPName[$i], $type, $ID_Device, 1);
                         }
                     } else {
                         $IDport = $DB->result($result, 0, "ID");
                         if ($DB->result($result, 0, "name") != $ArrayPort_LogicalNum_SNMPName[$num]) {
                             unset($array);
                             $array["name"] = $ArrayPort_LogicalNum_SNMPName[$num];
                             $array["ID"] = $DB->result($result, 0, "ID");
                             $np->update($array);
                             if ($_SESSION['fusioninventory_logs'] == "1") {
                                 $logs->write("fusioninventory_fullsync", "Update port in DB (glpi_networking_ports) : ID" . $DB->result($result, 0, "ID") . " & name " . $ArrayPort_LogicalNum_SNMPName[$i], $type, $ID_Device, 1);
                             }
                         }
                     }
                     if ($type == NETWORKING_TYPE) {
                         $queryFusionInventoryPort = "SELECT `ID`\n                                       FROM `glpi_plugin_fusioninventory_networking_ports`\n                                       WHERE `FK_networking_ports`='" . $IDport . "';";
                         $resultFusionInventoryPort = $DB->query($queryFusionInventoryPort);
                         if ($DB->numrows($resultFusionInventoryPort) == 0) {
                             $queryInsert = "INSERT INTO `glpi_plugin_fusioninventory_networking_ports`\n                                                 (`FK_networking_ports`)\n                                     VALUES ('" . $IDport . "');";
                             $DB->query($queryInsert);
                             if ($_SESSION['fusioninventory_logs'] == "1") {
                                 $logs->write("fusioninventory_fullsync", "Add port in DB (glpi_plugin_fusioninventory_networking_ports) : ID " . $IDport, $type, $ID_Device, 1);
                             }
                         }
                     }
                 }
             }
         }
         $logicalnumberlist .= ")";
     }
     // Delete all ports that will be not here
     $nn = new NetworkPort_NetworkPort();
     foreach ($deleteportname as $id => $i) {
         $query = "SELECT *\n                FROM `glpi_networking_ports`\n                WHERE `on_device`='" . $ID_Device . "'\n                      AND `device_type`='" . $type . "'\n                      AND `logical_number`='" . $i . "';";
         $result = $DB->query($query);
         $data = $DB->fetch_assoc($result);
         PluginFusioninventorySnmphistory::addLogConnection("remove", $netwire->getOppositeContact($data['ID']), $FK_process);
         PluginFusioninventorySnmphistory::addLogConnection("remove", $data['ID'], $FK_process);
         if ($nn->getFromDBForNetworkPort($data['ID'])) {
             $nn->delete($data);
         }
         $ptp->deleteFromDB($data["ID"], 1);
         $np->delete($data);
     }
     // Delete ports where logical number in glpi_plugin_fusioninventory_networking_ports
     // not exist on switch : it's ports reorder not well
     $logicalnumberlist = str_replace(",)", ")", $logicalnumberlist);
     $query = "SELECT *\n             FROM `glpi_networking_ports`\n             WHERE `on_device`='" . $ID_Device . "'\n                   AND `device_type`='" . $type . "'\n                   AND `logical_number` NOT IN " . $logicalnumberlist . ";";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         PluginFusioninventorySnmphistory::addLogConnection("remove", $netwire->getOppositeContact($data['ID']), $FK_process);
         PluginFusioninventorySnmphistory::addLogConnection("remove", $data['ID'], $FK_process);
         if ($nn->getFromDBForNetworkPort($data['ID'])) {
             $nn->delete($data);
         }
         $np->delete($data);
         $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_networking_ports`\n                       WHERE `FK_networking_ports`='" . $data["ID"] . "';";
         $DB->query($query_delete);
     }
     return $oidList;
 }
 function hubNetwork($p_oPort)
 {
     global $DB;
     $nw = new Netwire();
     $np = new Netport();
     $nn = new NetworkPort_NetworkPort();
     // List of macs : $p_oPort->getPortsToConnect
     // recherche dans unknown_device table le hub
     // qui a un port connecte sur le $port_ID
     // Get port connected on switch port
     if ($ID = $nw->getOppositeContact($p_oPort->getValue('ID'))) {
         $np->getFromDB($ID);
         if ($np->fields["device_type"] == $this->type) {
             $this->getFromDB($np->fields["on_device"]);
             if ($this->fields["hub"] == "1") {
                 // We will update ports and wire
                 return;
             }
         }
     }
     // sinon on cree un nouveau unknown_device type hub
     // + creation des ports qui sont connectes aux mac
     $input = array();
     $input['hub'] = "1";
     $input['name'] = "hub";
     $id_unknown = $this->add($input);
     $input = array();
     $input["on_device"] = $id_unknown;
     $input["device_type"] = $this->type;
     $input["name"] = "Link";
     $id_port = $np->add($input);
     $nn->add(array('networkports_id_1' => $p_oPort->getValue('ID'), 'networkports_id_2' => $id_port));
     foreach ($p_oPort->getMacsToConnect() as $ifmac) {
         $input = array();
         $input["on_device"] = $id_unknown;
         $input["device_type"] = $this->type;
         $id_port = $np->add($input);
         // TODO : recherche le port qui a cet $ifmac
         $query = "SELECT * FROM `glpi_networking_ports`\n            WHERE `ifmac` = '" . $ifmac . "' ";
         $result = $DB->query($query);
         if ($DB->numrows($result) == 1) {
             $line = $DB->fetch_assoc($result);
             $nn->add(array('networkports_id_1' => $line['ID'], 'networkports_id_2' => $id_port));
         } else {
             // Create device inconnu
         }
     }
 }
Beispiel #3
0
 function addToDB()
 {
     if ($this->getName()) {
         $cfg_ocs = getOcsConf($_SESSION["ocs_server_id"]);
         parent::getEmpty();
         $this->fields = array();
         $this->fields["name"] = $this->getName();
         $this->fields["comments"] = $this->port . '\\n' . $this->error;
         $this->fields["date_mod"] = date("Y-m-d H:i:s");
         $this->fields["is_global"] = 0;
         $this->fields["model"] = ocsImportDropdown("glpi_dropdown_model_printers", "name", $this->driver);
         if ($this->fgLPT) {
             $this->fields["flags_par"] = 1;
         }
         if ($this->fgCOM) {
             $this->fields["flags_serial"] = 1;
         }
         if ($this->fgUSB) {
             $this->fields["flags_usb"] = 1;
         }
         if ($this->id = parent::addToDB()) {
             if ($cfg_ocs["default_state"]) {
                 updateState(PRINTER_TYPE, $this->id, $cfg_ocs["default_state"], 0, 0);
             }
             // Si connexion directe : connexion avec lien du lieu et du contact
             foreach ($this->servers as $server => &$queue) {
                 // Si serveur : le lieu et le contact seront ceux du serveur
                 if ($server === $this->server) {
                     $queue->connectToServer($this->id);
                 } else {
                     $queue->connectToUC($this->id);
                 }
             }
             // Connexion des files d'impression
             foreach ($this->queues as $host => &$queue) {
                 if ($host != $queue->server) {
                     $queue->connectToUC($this->id);
                 }
             }
             // Connexion IP
             if ($this->IP != '') {
                 $netport = array();
                 $netport["ifaddr"] = $this->IP;
                 $netport["ifmac"] = '';
                 //$line2["MACADDR"];
                 $netport["iface"] = ocsImportDropdown("glpi_dropdown_iface", "name", "Ethernet");
                 $netport["name"] = "Ethernet";
                 $netport["on_device"] = $this->id;
                 $netport["logical_number"] = 0;
                 $netport["device_type"] = PRINTER_TYPE;
                 $np = new Netport();
                 $np->add($netport);
             }
         }
     }
     return $this->id;
 }
 /**
  * Manage connection to unknown device
  *
  *@param $p_mac MAC address
  *@param $p_ip IP address
  *@return nothing
  **/
 function PortUnknownConnection($p_mac, $p_ip)
 {
     $ptud = new PluginFusioninventoryUnknownDevice();
     $unknown_infos["name"] = '';
     $newID = $ptud->add($unknown_infos);
     // Add networking_port
     $np = new Netport();
     $port_add["on_device"] = $newID;
     $port_add["device_type"] = PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN;
     $port_add["ifaddr"] = $p_ip;
     $port_add['ifmac'] = $p_mac;
     $dport = $np->add($port_add);
     $ptsnmp = new PluginFusioninventorySNMP();
     $this->connectDB($dport);
 }
 function import_netdiscovery($p_xml, $agentKey)
 {
     global $DB, $LANG;
     $test = '';
     $p_criteria = array();
     $walks = new PluginFusioninventoryWalk();
     $ptap = new PluginFusioninventoryAgentsProcesses();
     $pta = new PluginFusioninventoryAgents();
     $config_discovery = new PluginFusioninventoryConfig();
     $np = new Netport();
     $ptud = new PluginFusioninventoryUnknownDevice();
     if (isset($p_xml->AGENT->START)) {
         $ptap->updateProcess($p_xml->PROCESSNUMBER, array('start_time_discovery' => date("Y-m-d H:i:s")));
     } else {
         if (isset($p_xml->AGENT->END)) {
             $ptap->updateProcess($p_xml->PROCESSNUMBER, array('end_time_discovery' => date("Y-m-d H:i:s")));
         } else {
             if (isset($p_xml->AGENT->EXIT)) {
                 $ptap->endProcess($p_xml->PROCESSNUMBER, date("Y-m-d H:i:s"));
             } else {
                 if (isset($p_xml->AGENT->NBIP)) {
                     $ptap->updateProcess($p_xml->PROCESSNUMBER, array('discovery_nb_ip' => $p_xml->AGENT->NBIP));
                 }
             }
         }
     }
     if (isset($p_xml->AGENT->AGENTVERSION)) {
         $agent = $pta->InfosByKey($agentKey);
         $agent['fusioninventory_agent_version'] = $p_xml->AGENT->AGENTVERSION;
         $agent['last_agent_update'] = date("Y-m-d H:i:s");
         $pta->update($agent);
     }
     $walkdata = '';
     $count_discovery_devices = 0;
     foreach ($p_xml->DEVICE as $discovery) {
         $count_discovery_devices++;
     }
     $ptap->updateProcess($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('discovery_nb_found' => $count_discovery_devices));
     foreach ($p_xml->DEVICE as $discovery) {
         if ($discovery->MODELSNMP != "") {
             $query = "SELECT *\n                      FROM `glpi_plugin_fusioninventory_model_infos`\n                      WHERE `discovery_key`='" . $discovery->MODELSNMP . "'\n                      LIMIT 0,1;";
             $result = $DB->query($query);
             $data = $DB->fetch_assoc($result);
             $FK_model = $data['ID'];
         } else {
             $FK_model = 0;
         }
         $discovery->MAC = strtolower($discovery->MAC);
         if (empty($FK_model)) {
             $FK_model = 0;
         }
         unset($p_criteria);
         $p_criteria['ip'] = $discovery->IP;
         if (!empty($discovery->NETBIOSNAME)) {
             $p_criteria['name'] = $discovery->NETBIOSNAME;
         } else {
             if (!empty($discovery->SNMPHOSTNAME)) {
                 $p_criteria['name'] = $discovery->SNMPHOSTNAME;
             }
         }
         $p_criteria['serial'] = $discovery->SERIAL;
         $p_criteria['macaddr'] = $discovery->MAC;
         $discovery_criteria = PluginFusioninventoryDiscovery::criteria($p_criteria);
         if (!$discovery_criteria) {
             $ptap->updateProcess($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('discovery_nb_import' => '1'));
             // Add in unknown device
             $ptud->getEmpty();
             if (!empty($discovery->NETBIOSNAME)) {
                 $ptud->fields['name'] = $discovery->NETBIOSNAME;
             } else {
                 if (!empty($discovery->SNMPHOSTNAME)) {
                     $ptud->fields['name'] = $discovery->SNMPHOSTNAME;
                 }
             }
             $ptud->fields['dnsname'] = $discovery->DNSHOSTNAME;
             $ptud->fields['FK_entities'] = $discovery->ENTITY;
             $ptud->fields['serial'] = $discovery->SERIAL;
             $ptud->fields['contact'] = $discovery->USERSESSION;
             if (!empty($discovery->WORKGROUP)) {
                 $ptud->fields['domain'] = Dropdown::importExternal("Domain", $discovery->WORKGROUP, $discovery->ENTITY);
             }
             $ptud->fields['comments'] = $discovery->DESCRIPTION;
             $ptud->fields['type'] = $discovery->TYPE;
             $ptud->fields['FK_model_infos'] = $FK_model;
             $ptud->fields['FK_snmp_connection'] = $discovery->AUTHSNMP;
             if ($discovery->AUTHSNMP != "") {
                 $ptud->fields['snmp'] = 1;
             }
             $ptud->fields['location'] = 0;
             $ptud->fields['deleted'] = 0;
             if ($ptud->fields['domain'] == '') {
                 $ptud->fields['domain'] = 0;
             }
             if ($ptud->fields['type'] == '') {
                 $ptud->fields['type'] = 0;
             }
             if ($ptud->fields['snmp'] == '') {
                 $ptud->fields['snmp'] = 0;
             }
             if ($ptud->fields['FK_model_infos'] == '') {
                 $ptud->fields['FK_model_infos'] = 0;
             }
             if ($ptud->fields['FK_snmp_connection'] == '') {
                 $ptud->fields['FK_snmp_connection'] = 0;
             }
             if ($ptud->fields['accepted'] == '') {
                 $ptud->fields['accepted'] = 0;
             }
             $explodeprocess = explode("/", $_SESSION['glpi_plugin_fusioninventory_processnumber']);
             $ptud->fields['FK_agent'] = intval($explodeprocess[1]);
             $ptud->fields['hub'] = 0;
             $data = $ptud->fields;
             unset($data['ID']);
             $newID = $ptud->add($data);
             unset($data);
             // Add networking_port
             $port_add["on_device"] = $newID;
             $port_add["device_type"] = PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN;
             $port_add["ifaddr"] = $discovery->IP;
             $port_add['ifmac'] = $discovery->MAC;
             $port_add['name'] = $discovery->NETPORTVENDOR;
             $port_ID = $np->add($port_add);
             unset($port_add);
         } else {
             # Update device
             //echo "discovery_criteria :".$discovery_criteria;
             $a_device = explode("||", $discovery_criteria);
             // $a_device[0] == id, $a_device[1] = type
             $ci = new commonitem();
             $ci->getFromDB($a_device[1], $a_device[0]);
             $a_lockable = PluginFusioninventoryLock::getLockFields($a_device[1], $a_device[0]);
             $data = array();
             $data['ID'] = $ci->getField('ID');
             if ($a_device[1] == PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN) {
                 $data['FK_snmp_connection'] = 0;
                 if ($ci->getField('name') && !in_array('name', $a_lockable)) {
                     if (!empty($discovery->NETBIOSNAME)) {
                         $data['name'] = $discovery->NETBIOSNAME;
                     } else {
                         if (!empty($discovery->SNMPHOSTNAME)) {
                             $data['name'] = $discovery->SNMPHOSTNAME;
                         }
                     }
                 }
                 if ($ci->getField('dnsname') && !in_array('dnsname', $a_lockable)) {
                     $data['dnsname'] = $discovery->DNSHOSTNAME;
                 }
                 if ($ci->getField('FK_entities') && !in_array('FK_entities', $a_lockable)) {
                     $data['FK_entities'] = $discovery->ENTITY;
                 }
                 if ($ci->getField('serial') && !in_array('serial', $a_lockable)) {
                     $data['serial'] = $discovery->SERIAL;
                 }
                 if ($ci->getField('contact') && !in_array('contact', $a_lockable)) {
                     $data['contact'] = $discovery->USERSESSION;
                 }
                 if ($ci->getField('domain') && !in_array('domain', $a_lockable)) {
                     $data['domain'] = 0;
                     if (!empty($discovery->WORKGROUP)) {
                         $data['domain'] = Dropdown::importExternal("Domain", $discovery->WORKGROUP, $discovery->ENTITY);
                     }
                 }
                 if ($ci->getField('FK_snmp_connection') && !in_array('FK_snmp_connection', $a_lockable)) {
                     $data['FK_snmp_connection'] = $discovery->AUTHSNMP;
                 }
                 if ($ci->getField('snmp') && !in_array('snmp', $a_lockable)) {
                     $data['snmp'] = 0;
                     if ($discovery->AUTHSNMP != "") {
                         $data['snmp'] = 1;
                     }
                 }
                 $data['type'] = $discovery->TYPE;
                 $explodeprocess = explode("/", $_SESSION['glpi_plugin_fusioninventory_processnumber']);
                 $data['FK_agent'] = intval($explodeprocess[1]);
             }
             if ($ci->getField('FK_model_infos') && !in_array('FK_model_infos', $a_lockable)) {
                 $data['FK_model_infos'] = $FK_model;
             }
             if ($ci->getField('comments') && !in_array('comments', $a_lockable)) {
                 $data['comments'] = $discovery->DESCRIPTION;
             }
             if ($a_device[1] == NETWORKING_TYPE) {
                 if (!in_array('ifaddr', $a_lockable)) {
                     $data["ifaddr"] = $discovery->IP;
                 }
                 if (!in_array('ifmac', $a_lockable)) {
                     $data['ifmac'] = $discovery->MAC;
                 }
             } else {
                 // TODO: manage ports
                 $np = new Netport();
                 $query = "SELECT ID FROM glpi_networking_ports\n                  WHERE (on_device = '" . $a_device[0] . "' AND device_type = '" . $a_device[1] . "')\n                     AND `ifaddr` NOT IN ('', '127.0.0.1')\n                  ORDER BY name, logical_number";
                 if ($result = $DB->query($query)) {
                     if ($DB->numrows($result) == 1) {
                         $data2 = $DB->fetch_assoc($result);
                         $np->getFromDB($data2["ID"]);
                         $port = array();
                         $port['ID'] = $data2["ID"];
                         $port["ifaddr"] = $discovery->IP;
                         $port['ifmac'] = $discovery->MAC;
                         $port['name'] = $discovery->NETPORTVENDOR;
                         $np->update($port);
                     } else {
                         if ($DB->numrows($result) > 1) {
                             $ptae = new PluginFusioninventoryAgentsErrors();
                             $error_input['ID'] = $a_device[0];
                             $error_input['TYPE'] = $a_device[1];
                             $error_input['MESSAGE'] = 'Unable to determine network port of device to update with values : ' . $discovery->IP . '(ip),
                     ' . $discovery->MAC . '(mac), ' . $discovery->NETPORTVENDOR . '(name)';
                             $error_input['agent_type'] = 'NETDISCOVERY';
                             $ptae->addError($error_input);
                         }
                     }
                 }
             }
             $ci->obj->update($data);
             $ptap->updateProcess($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('discovery_nb_exists' => '1'));
         }
     }
 }