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')); } } }
function plugin_fusioninventory_MassiveActionsProcess($data) { global $LANG; switch ($data['action']) { case "plugin_fusioninventory_get_model": if ($data['device_type'] == NETWORKING_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { $PluginFusioninventoryModelInfos = new PluginFusioninventoryModelInfos(); $PluginFusioninventoryModelInfos->getrightmodel($key, NETWORKING_TYPE); } } } else { if ($data['device_type'] == PRINTER_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { $PluginFusioninventoryModelInfos = new PluginFusioninventoryModelInfos(); $PluginFusioninventoryModelInfos->getrightmodel($key, PRINTER_TYPE); } } } } break; case "plugin_fusioninventory_assign_model": if ($data['device_type'] == NETWORKING_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { PluginFusioninventoryMassiveaction::assign($key, NETWORKING_TYPE, "model", $data["snmp_model"]); } } } else { if ($data['device_type'] == PRINTER_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { PluginFusioninventoryMassiveaction::assign($key, PRINTER_TYPE, "model", $data["snmp_model"]); } } } } break; case "plugin_fusioninventory_assign_auth": if ($data['device_type'] == NETWORKING_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { PluginFusioninventoryMassiveaction::assign($key, NETWORKING_TYPE, "auth", $data["FK_snmp_connection"]); } } } else { if ($data['device_type'] == PRINTER_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { PluginFusioninventoryMassiveaction::assign($key, PRINTER_TYPE, "auth", $data["FK_snmp_connection"]); } } } } break; case "plugin_fusioninventory_manage_locks": if ($data['device_type'] == NETWORKING_TYPE or $data['device_type'] == PRINTER_TYPE) { foreach ($data['item'] as $key => $val) { if ($val == 1) { if (isset($data["lockfield_fusioninventory"]) && count($data["lockfield_fusioninventory"])) { $tab = PluginFusioninventoryLock::exportChecksToArray($data["lockfield_fusioninventory"]); PluginFusioninventoryLock::setLockArray($data['type'], $key, $tab); } else { PluginFusioninventoryLock::setLockArray($data['type'], $key, array()); } } } } break; case "plugin_fusioninventory_discovery_import": if (PluginFusioninventory::haveRight("unknowndevices", "w")) { $Import = 0; $NoImport = 0; foreach ($data['item'] as $key => $val) { if ($val == 1) { list($Import, $NoImport) = PluginFusioninventoryDiscovery::import($key, $Import, $NoImport); } } addMessageAfterRedirect($LANG['plugin_fusioninventory']["discovery"][5] . " : " . $Import); addMessageAfterRedirect($LANG['plugin_fusioninventory']["discovery"][9] . " : " . $NoImport); } break; } }
// logEvent($_POST["ID"], "computers", 4, "inventory", $_SESSION["glpiname"]." ".$LANG['log'][22]); glpi_header($CFG_GLPI["root_doc"] . "plugins/fusioninventory/front/unknown.php"); } else { if (isset($_POST["restore"])) { } else { if (isset($_POST["purge"]) || isset($_GET["purge"])) { } else { if (isset($_POST["update"])) { $ptud->check($_POST['ID'], 'w'); $ptud->update($_POST); glpi_header($_SERVER['HTTP_REFERER']); } else { if (isset($_POST["import"])) { $Import = 0; $NoImport = 0; list($Import, $NoImport) = PluginFusioninventoryDiscovery::import($_POST['ID'], $Import, $NoImport); addMessageAfterRedirect($LANG['plugin_fusioninventory']["discovery"][5] . " : " . $Import); addMessageAfterRedirect($LANG['plugin_fusioninventory']["discovery"][9] . " : " . $NoImport); if ($Import == "0") { glpi_header($_SERVER['HTTP_REFERER']); } else { glpi_header($CFG_GLPI["root_doc"] . "/plugins/fusioninventory/front/unknown.php"); } } } } } } $ptud->showTabs($ID, '', $_SESSION['glpi_tab']); $ptud->showForm($_SERVER["PHP_SELF"], $ID); echo "<div id='tabcontent'></div>";
/** * Import INFO *@param $p_info INFO code to import * *@return errors string to be alimented if import ko / '' if ok **/ function importInfo($p_info, $p_device) { global $LANG; $this->addLog('Function importInfo().'); $errors = ''; $criteria['serial'] = $p_info->SERIAL; $criteria['name'] = $p_info->NAME; $criteria['macaddr'] = $p_info->MAC; //TODO get mac in PORT for printer if ($p_info->TYPE == 'NETWORKING') { $this->deviceId = PluginFusioninventoryDiscovery::criteria($criteria, NETWORKING_TYPE); if ($this->deviceId != '') { $errors .= $this->importInfoNetworking($p_info); } else { $errors .= $LANG['plugin_fusioninventory']["errors"][23] . '<br/> type : ' . $p_info->TYPE . '<br/> ID : ' . $p_info->ID . '<br/> serial : ' . $p_info->SERIAL . '<br/> name : ' . $p_info->NAME . '<br/> macaddress : ' . $p_info->MAC . '\\n'; } } elseif ($p_info->TYPE == 'PRINTER') { //TODO Get MAC address in port foreach ($p_device->children() as $child) { switch ($child->getName()) { case 'PORTS': foreach ($child->children() as $child_port) { switch ($child_port->getName()) { case 'PORT': $criteria['macaddr'] = $child_port->MAC; if ($this->deviceId == '') { $this->deviceId = PluginFusioninventoryDiscovery::criteria($criteria, PRINTER_TYPE); } break; } } break; } } //$this->deviceId = PluginFusioninventoryDiscovery::criteria($criteria, PRINTER_TYPE); if ($this->deviceId != '') { $errors .= $this->importInfoPrinter($p_info); } else { $errors .= $LANG['plugin_fusioninventory']["errors"][23] . '<br/> type : ' . $p_info->TYPE . '<br/> ID : ' . $p_info->ID . '<br/> serial : ' . $p_info->SERIAL . '<br/> name : ' . $p_info->NAME . '<br/> macaddress : ' . $p_info->MAC . '\\n'; } } if (!empty($errors)) { $pfiae = new PluginFusioninventoryAgentsErrors(); $a_input = array(); $a_input['ID'] = $p_info->ID; if ($p_info->TYPE == 'NETWORKING') { $a_input['TYPE'] = NETWORKING_TYPE; } elseif ($p_info->TYPE == 'PRINTER') { $a_input['TYPE'] = PRINTER_TYPE; } $a_input['MESSAGE'] = $errors; $a_input['agent_type'] = 'SNMPQUERY'; $pfiae->addError($a_input); } return $errors; }