/** * @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); }
the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Ocsinventoryng plugin 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'; Session::checkSeveralRightsOr(array("plugin_ocsinventoryng" => READ, "plugin_ocsinventoryng_clean" => READ)); Html::header('OCS Inventory NG', '', "tools", "pluginocsinventoryngmenu", "ocsinventoryng"); if (isset($_SESSION["ocs_import"])) { unset($_SESSION["ocs_import"]); } if (isset($_SESSION["ocs_link"])) { unset($_SESSION["ocs_link"]); } if (isset($_SESSION["ocs_update"])) { unset($_SESSION["ocs_update"]); } if (isset($_POST["plugin_ocsinventoryng_ocsservers_id"])) { $_SESSION["plugin_ocsinventoryng_ocsservers_id"] = $_POST["plugin_ocsinventoryng_ocsservers_id"]; } else { $_SESSION["plugin_ocsinventoryng_ocsservers_id"] = PluginOcsinventoryngOcsServer::getFirstServer(); } PluginOcsinventoryngOcsServer::ocsMenu($_SESSION["plugin_ocsinventoryng_ocsservers_id"]); PluginOcsinventoryngOcsServer::checkOCSconnection($_SESSION["plugin_ocsinventoryng_ocsservers_id"]); Html::footer();
/** * @param $params array * * @return array * @return array */ static function getOcsComputerInfos($params = array()) { PluginOcsinventoryngOcsServer::checkOCSconnection($params['plugin_ocsinventoryng_ocsservers_id']); $ocsClient = PluginOcsinventoryngOcsServer::getDBocs($params['plugin_ocsinventoryng_ocsservers_id']); $options = array("DISPLAY" => array("CHECKSUM" => PluginOcsinventoryngOcsClient::CHECKSUM_BIOS)); $computer = $ocsClient->getComputer($params['ocsid'], $options); $changes = array(); if ($computer) { $ocs_serial = $computer["BIOS"]["SSN"]; if ($ocs_serial != $params['serial']) { $ocsClient->updateBios($params['serial'], $params['ocsid']); $changes[] = 'serial'; } $ocs_tag = $computer["META"]["TAG"]; if ($ocs_tag != $params['tag']) { $ocsClient->updateBios($params['tag'], $params['ocsid']); $changes[] = 'tag'; } } return $changes; }
/** * @param CommonGLPI $item * @param int $withtemplate * @return array|string */ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { global $CFG_GLPI; switch ($item->getType()) { case __CLASS__: $ocsServers = getAllDatasFromTable('glpi_plugin_ocsinventoryng_ocsservers', "`is_active`='1'"); if (!empty($ocsServers)) { $ong[0] = __('Server Setup', 'ocsinventoryng'); $ong[1] = __('Inventory Import', 'ocsinventoryng'); $ong[2] = __('IPDiscover Import', 'ocsinventoryng'); if (isset($_POST["plugin_ocsinventoryng_ocsservers_id"])) { $_SESSION["plugin_ocsinventoryng_ocsservers_id"] = $_POST["plugin_ocsinventoryng_ocsservers_id"]; } else { $_SESSION["plugin_ocsinventoryng_ocsservers_id"] = PluginOcsinventoryngOcsServer::getFirstServer(); } if (isset($_SESSION["plugin_ocsinventoryng_ocsservers_id"]) && $_SESSION["plugin_ocsinventoryng_ocsservers_id"] > 0) { if (PluginOcsinventoryngOcsServer::checkOCSconnection($_SESSION["plugin_ocsinventoryng_ocsservers_id"])) { $ocsClient = new PluginOcsinventoryngOcsServer(); $client = $ocsClient->getDBocs($_SESSION["plugin_ocsinventoryng_ocsservers_id"]); $version = $client->getTextConfig('GUI_VERSION'); $snmp = $client->getIntConfig('SNMP'); if ($version > $ocsClient::OCS2_1_VERSION_LIMIT && $snmp) { $ong[3] = __('SNMP Import', 'ocsinventoryng'); } } } } else { $ong = array(); echo "<div align='center'>"; echo Html::image($CFG_GLPI["root_doc"] . "/pics/warning.png", array('alt' => __('Warning'))); echo "<br>"; echo "<div class='red b'>"; _e('No OCSNG server defined', 'ocsinventoryng'); echo "<br>"; _e('You must to configure a OCSNG server', 'ocsinventoryng'); echo " : <a href='" . $CFG_GLPI["root_doc"] . "/plugins/ocsinventoryng/front/ocsserver.form.php'>"; _e('Add a OCSNG server', 'ocsinventoryng'); echo "</a>"; echo "</div></div>"; } return $ong; default: return ''; } }
echo "Options values :\n"; echo "\t--glpi : check missing computer in GLPI\n"; echo "\t--ocs : check missing computer in OCS\n"; echo "\t--dup : check for duplicate links (n links for 1 computer in GLPI)\n"; echo "\t--clean : delete invalid link\n"; exit(0); } $tps = microtime(true); $nbchk = 0; $nbdel = 0; $nbtodo = 0; $crit = array('is_active' => 1); foreach ($DB->request('glpi_plugin_ocsinventoryng_ocsservers', $crit) as $serv) { $ocsservers_id = $serv['id']; echo "\nServeur: " . $serv['name'] . "\n"; if (!PluginOcsinventoryngOcsServer::checkOCSconnection($ocsservers_id)) { echo "** no connexion\n"; continue; } if (isset($_GET['clean'])) { echo "+ Handle ID changes\n"; PluginOcsinventoryngOcsServer::manageDeleted($ocsservers_id); } if (isset($_GET['glpi'])) { echo "+ Search links with no computer in GLPI\n"; $query = "SELECT `glpi_plugin_ocsinventoryng_ocslinks`.`id`,\n `glpi_plugin_ocsinventoryng_ocslinks`.`ocs_deviceid`\n FROM `glpi_plugin_ocsinventoryng_ocslinks`\n LEFT JOIN `glpi_computers`\n ON `glpi_computers`.`id`=`glpi_plugin_ocsinventoryng_ocslinks`.`computers_id`\n WHERE `glpi_computers`.`id` IS NULL\n AND `plugin_ocsinventoryng_ocsservers_id`='{$ocsservers_id}'"; $result = $DB->query($query); if ($DB->numrows($result) > 0) { while ($data = $DB->fetch_array($result)) { $nbchk++; printf("%12d : %s\n", $data['id'], $data['ocs_deviceid']);
/** * @see inc/RuleCollection::prepareInputDataForProcess() * @since 0.84 * @param $params input data * @return an array of criteria value to add for processing **/ function plugin_ocsinventoryng_ruleCollectionPrepareInputDataForProcess($params) { global $PluginOcsinventoryngDBocs; switch ($params['rule_itemtype']) { case 'RuleImportEntity': case 'RuleImportComputer': if ($params['rule_itemtype'] == 'RuleImportEntity') { $ocsservers_id = $params['values']['input']['ocsservers_id']; } else { $ocsservers_id = $params['values']['params']['plugin_ocsinventoryng_ocsservers_id']; } $tables = plugin_ocsinventoryng_getTablesForQuery(); $fields = plugin_ocsinventoryng_getFieldsForQuery(); $rule_parameters = array('ocsservers_id' => $ocsservers_id); $select_sql = ""; if (isset($params['values']['params']['ocsid'])) { $ocsid = $params['values']['params']['ocsid']; } else { if ($params['values']['input']['id']) { $ocsid = $params['values']['input']['id']; } } //Get information about network ports $query = "SELECT *\n FROM `networks`\n WHERE `HARDWARE_ID` = '{$ocsid}'"; $ipblacklist = Blacklist::getIPs(); $macblacklist = Blacklist::getMACs(); foreach ($PluginOcsinventoryngDBocs->request($query) as $data) { if (isset($data['IPSUBNET'])) { $rule_parameters['IPSUBNET'][] = $data['IPSUBNET']; } if (isset($data['MACADDR']) && !in_array($data['MACADDR'], $macblacklist)) { $rule_parameters['MACADDRESS'][] = $data['MACADDR']; } if (isset($data['IPADDRESS']) && !in_array($data['IPADDRESS'], $ipblacklist)) { $rule_parameters['IPADDRESS'][] = $data['IPADDRESS']; } } //Build the select request foreach ($fields as $field) { switch (Toolbox::strtoupper($field)) { //OCS server ID is provided by extra_params -> get the configuration associated with the ocs server case "OCS_SERVER": $rule_parameters["OCS_SERVER"] = $ocsservers_id; break; //TAG and DOMAIN should come from the OCS DB //TAG and DOMAIN should come from the OCS DB default: $select_sql .= ($select_sql != "" ? " , " : "") . $field; } } //Build the FROM part of the request //Remove all the non duplicated table names $from_sql = "FROM `hardware` "; foreach ($tables as $table => $linkfield) { if ($table != 'hardware' && !empty($linkfield)) { $from_sql .= " LEFT JOIN `{$table}` ON (`{$table}`.`{$linkfield}` = `hardware`.`ID`)"; } } if ($select_sql != "") { //Build the all request $sql = "SELECT {$select_sql}\n {$from_sql}\n WHERE `hardware`.`ID` = '{$ocsid}'"; PluginOcsinventoryngOcsServer::checkOCSconnection($ocsservers_id); $result = $PluginOcsinventoryngDBocs->query($sql); $ocs_data = array(); $fields = plugin_ocsinventoryng_getFieldsForQuery(1); //May have more than one line : for example in case of multiple network cards if ($PluginOcsinventoryngDBocs->numrows($result) > 0) { while ($datas = $PluginOcsinventoryngDBocs->fetch_assoc($result)) { foreach ($fields as $field) { if ($field != "OCS_SERVER" && isset($datas[$field])) { $ocs_data[$field][] = $datas[$field]; } } } } //This cas should never happend but... //Sometimes OCS can't find network ports but fill the right ip in hardware table... //So let's use the ip to proceed rules (if IP is a criteria of course) if (in_array("IPADDRESS", $fields) && !isset($ocs_data['IPADDRESS'])) { $ocs_data['IPADDRESS'] = PluginOcsinventoryngOcsServer::getGeneralIpAddress($ocsservers_id, $ocsid); } return array_merge($rule_parameters, $ocs_data); } return $rule_parameters; } return array(); }
/** * @param $ocsid * @param $plugin_ocsinventoryng_ocsservers_id * @param $params * @return array|bool * @internal param $computers_id * */ static function linkSnmpDevice($ocsid, $plugin_ocsinventoryng_ocsservers_id, $params) { PluginOcsinventoryngOcsServer::checkOCSconnection($plugin_ocsinventoryng_ocsservers_id); $ocsClient = PluginOcsinventoryngOcsServer::getDBocs($plugin_ocsinventoryng_ocsservers_id); //TODOSNMP entites_id ? $p['itemtype'] = -1; $p['items_id'] = -1; foreach ($params as $key => $val) { $p[$key] = $val; } $ocs_id_change = true; /* $query = "SELECT * FROM `glpi_plugin_ocsinventoryng_snmpocslinks` WHERE `ocs_id` = '$ocs_id'"; $result = $DB->query($query); $ocs_id_change = false; $ocs_link_exists = false; $numrows = $DB->numrows($result); // Already link - check if the OCS computer already exists if ($numrows > 0) { $ocs_link_exists = true; $data = $DB->fetch_assoc($result); $ocsComputer = $ocsClient->getComputer($data['ocsid']); // Not found if (is_null($ocsComputer)) { $idlink = $data["id"]; $query = "UPDATE `glpi_plugin_ocsinventoryng_ocslinks` SET `ocsid` = '$ocsid' WHERE `id` = '" . $data["id"] . "'"; if ($DB->query($query)) { $ocs_id_change = true; //Add history to indicates that the ocsid changed $changes[0] = '0'; //Old ocsid $changes[1] = $data["ocsid"]; //New ocsid $changes[2] = $ocsid; PluginOcsinventoryngOcslink::history($computers_id, $changes, PluginOcsinventoryngOcslink::HISTORY_OCS_IDCHANGED); } } } // No ocs_link or ocs id change does not exists so can link if ($ocs_id_change || !$ocs_link_exists) { $ocsConfig = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id); // Set OCS checksum to max value $ocsClient->setChecksum(PluginOcsinventoryngOcsClient::CHECKSUM_ALL, $ocsid); */ if ($p['itemtype'] != -1 && $p['items_id'] > 0 && ($idlink = self::ocsSnmpLink($ocsid, $plugin_ocsinventoryng_ocsservers_id, $p['items_id'], $p['itemtype']))) { /* // automatic transfer computer if (($CFG_GLPI['transfers_id_auto'] > 0) && Session::isMultiEntitiesMode()) { // Retrieve data from glpi_plugin_ocsinventoryng_ocslinks $ocsLink = new PluginOcsinventoryngOcslink(); $ocsLink->getFromDB($idlink); if (count($ocsLink->fields)) { // Retrieve datas from OCS database $ocsComputer = $ocsClient->getComputer($ocsLink->fields['ocsid']); if (!is_null($ocsComputer)) { $ocsComputer = Toolbox::addslashes_deep($ocsComputer); PluginOcsinventoryngOcsServer::transferComputer($ocsLink->fields, $ocsComputer); } } } $comp = new Computer(); $comp->getFromDB($computers_id); $input["id"] = $computers_id; $input["entities_id"] = $comp->fields['entities_id']; $input["is_dynamic"] = 1; $input["_nolock"] = true; // Not already import from OCS / mark default state if ((!$ocs_id_change && ($ocsConfig["states_id_default"] > 0)) || (!$comp->fields['is_dynamic'] && ($ocsConfig["states_id_default"] > 0))) { $input["states_id"] = $ocsConfig["states_id_default"]; } $comp->update($input); // Auto restore if deleted if ($comp->fields['is_deleted']) { $comp->restore(array('id' => $computers_id)); } // Reset only if not in ocs id change case if (!$ocs_id_change) { if ($ocsConfig["import_general_os"]) { PluginOcsinventoryngOcsServer::resetDropdown($computers_id, "operatingsystems_id", "glpi_operatingsystems"); } if ($ocsConfig["import_device_processor"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceProcessor'); } if ($ocsConfig["import_device_iface"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceNetworkCard'); } if ($ocsConfig["import_device_memory"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceMemory'); } if ($ocsConfig["import_device_hdd"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceHardDrive'); } if ($ocsConfig["import_device_sound"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceSoundCard'); } if ($ocsConfig["import_device_gfxcard"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceGraphicCard'); } if ($ocsConfig["import_device_drive"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceDrive'); } if ($ocsConfig["import_device_modem"] || $ocsConfig["import_device_port"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DevicePci'); } if ($ocsConfig["import_device_bios"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'PluginOcsinventoryngDeviceBiosdata'); } if ($ocsConfig["import_device_motherboard"]) { PluginOcsinventoryngOcsServer::resetDevices($computers_id, 'DeviceMotherboard'); } if ($ocsConfig["import_software"]) { PluginOcsinventoryngOcsServer::resetSoftwares($computers_id); } if ($ocsConfig["import_disk"]) { PluginOcsinventoryngOcsServer::resetDisks($computers_id); } if ($ocsConfig["import_periph"]) { PluginOcsinventoryngOcsServer::resetPeripherals($computers_id); } if ($ocsConfig["import_monitor"]==1) { // Only reset monitor as global in unit management PluginOcsinventoryngOcsServer::resetMonitors($computers_id); // try to link monitor with existing } if ($ocsConfig["import_printer"]) { PluginOcsinventoryngOcsServer::resetPrinters($computers_id); } if ($ocsConfig["import_registry"]) { PluginOcsinventoryngOcsServer::resetRegistry($computers_id); } $changes[0] = '0'; $changes[1] = ""; $changes[2] = $ocsid; PluginOcsinventoryngOcslink::history($computers_id, $changes, PluginOcsinventoryngOcslink::HISTORY_OCS_LINK); } */ self::updateSnmp($idlink, $plugin_ocsinventoryng_ocsservers_id); return array('status' => PluginOcsinventoryngOcsServer::SNMP_LINKED); } /* } else { //TRANS: %s is the OCS id Session::addMessageAfterRedirect(sprintf(__('Unable to import, GLPI computer is already related to an element of OCSNG (%d)', 'ocsinventoryng'), $ocsid), false, ERROR); } */ return false; }
//Replace the ° by = the find the good filter $it = str_replace('°', '=', $it); $_GET[$it[0]] = $it[1]; } } include '../../../inc/includes.php'; $CFG_GLPI["debug"] = 0; if (!isset($_GET["server"])) { echo "*******************************************\n"; echo " This script kill babies : don't use it !! \n"; echo "*******************************************\n"; die("\nUsage : php -q -f rollbackocs.php --server=# [ --run=1 ]\n"); } $DBocs = new DBocs($_GET["server"]); echo "Connecting to " . $DBocs->dbhost . "\n"; if (!PluginOcsinventoryngOcsServer::checkOCSconnection($_GET["server"])) { die("Failed connexion to OCS\n"); } $run = isset($_GET["run"]) && $_GET["run"] > 0; $debug = isset($_GET["debug"]) && $_GET["debug"] > 0; // Find Last Machine ID + time $sql = "SELECT *\n FROM `hardware`\n ORDER BY `ID` DESC\n LIMIT 0,1"; $res = $DBocs->query($sql); if (!($res && $DBocs->numrows($res) > 0)) { die("No data from OCS\n"); } $data = $DBocs->fetch_array($res); $maxid = $data["ID"]; $maxti = $data["LASTCOME"]; echo "Last new computer : " . $data["DEVICEID"] . " ({$maxid}, {$maxti})\n"; if (!$maxid) {
/** * @param $params array **/ static function getOcsComputerInfos($params = array()) { global $PluginOcsinventoryngDBocs; PluginOcsinventoryngOcsServer::checkOCSconnection($params['plugin_ocsinventoryng_ocsservers_id']); $changes = array(); $query = "SELECT `SSN` FROM `bios`\n WHERE `HARDWARE_ID` = '" . $params['ocsid'] . "'"; $result = $PluginOcsinventoryngDBocs->query($query); if ($PluginOcsinventoryngDBocs->numrows($result) > 0) { $ocs_serial = $PluginOcsinventoryngDBocs->result($result, 0, 'SSN'); if ($ocs_serial != $params['serial']) { $query_serial = "UPDATE `bios`\n SET `SSN` = '" . $params['serial'] . "'" . " WHERE `HARDWARE_ID` = '" . $params['ocsid'] . "'"; $PluginOcsinventoryngDBocs->query($query_serial); $changes[] = 'serial'; } } $query = "SELECT `TAG`\n FROM `accountinfo`\n WHERE `HARDWARE_ID` = '" . $params['ocsid'] . "'"; $result = $PluginOcsinventoryngDBocs->query($query); if ($PluginOcsinventoryngDBocs->numrows($result) > 0) { $ocs_tag = $PluginOcsinventoryngDBocs->result($result, 0, 'TAG'); if ($ocs_tag != $params['tag']) { $query_serial = "UPDATE `accountinfo`\n SET `TAG` = '" . $params['tag'] . "'\n WHERE `HARDWARE_ID` = '" . $params['ocsid'] . "'"; $PluginOcsinventoryngDBocs->query($query_serial); $changes[] = 'tag'; } } return $changes; }
/** * check if ipdiscover object must be updated or imported * @param type $ipDiscoveryObject array * @param type $plugin_ocsinventoryng_ocsservers_id integer * @param $subnet * @return type array with the status of the import or update process * @global type $DB */ static function processIpDiscover($ipDiscoveryObject, $plugin_ocsinventoryng_ocsservers_id, $subnet) { global $DB; $ocsClient = new PluginOcsinventoryngOcsServer(); $ocsClient->checkOCSconnection($plugin_ocsinventoryng_ocsservers_id); $mac = $ipDiscoveryObject["macAdress"]; $query = "SELECT *\n FROM `glpi_plugin_ocsinventoryng_ipdiscoverocslinks`\n WHERE `glpi_plugin_ocsinventoryng_ipdiscoverocslinks`.`macaddress`\n LIKE '{$mac}' \n AND `glpi_plugin_ocsinventoryng_ipdiscoverocslinks`.`plugin_ocsinventoryng_ocsservers_id` ='{$plugin_ocsinventoryng_ocsservers_id}'"; $result = $DB->query($query); if ($DB->numrows($result)) { $datas = $DB->fetch_assoc($result); return self::updateIpDiscover($ipDiscoveryObject, $datas, $plugin_ocsinventoryng_ocsservers_id); } return self::importIpDiscover($ipDiscoveryObject, $plugin_ocsinventoryng_ocsservers_id, $subnet); }