Skip to content

Commit

Permalink
feat(core): add queued inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed Feb 8, 2024
1 parent 65dc686 commit 7ce6474
Show file tree
Hide file tree
Showing 10 changed files with 747 additions and 9 deletions.
78 changes: 78 additions & 0 deletions front/queuedinventory.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* FusionInventory
*
* Copyright (C) 2010-2016 by the FusionInventory Development Team.
*
* http://www.fusioninventory.org/
* https://github.com/fusioninventory/fusioninventory-for-glpi
* http://forge.fusioninventory.org/
*
* ------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of FusionInventory project.
*
* FusionInventory is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FusionInventory 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.
*
* ------------------------------------------------------------------------
*
* This file is used to manage the task form.
*
* ------------------------------------------------------------------------
*
* @package FusionInventory
* @author Stanislas Kita
* @copyright Copyright (c) 2010-2020 FusionInventory team
* @license AGPL License 3.0 or (at your option) any later version
* http://www.gnu.org/licenses/agpl-3.0-standalone.html
* @link http://www.fusioninventory.org/
* @link https://github.com/fusioninventory/fusioninventory-for-glpi
*
*/

include ("../../../inc/includes.php");

$pfQueuedinventory = new PluginFusioninventoryQueuedinventory();

if (isset($_POST['update'])) {
$pfQueuedinventory->check($_POST['id'], UPDATE);
$pfQueuedinventory->update($_POST);
Html::back();
} else if (isset($_POST['delete'])) {
$pfQueuedinventory->check($_POST['id'], DELETE);
$pfQueuedinventory->delete($_POST);
$pfQueuedinventory->redirectToList();
} else if (isset($_POST['purge'])) {
$pfQueuedinventory->check($_POST['id'], PURGE);
$pfQueuedinventory->delete($_POST, 1);
$pfQueuedinventory->redirectToList();
}

Html::header(__('FusionInventory', 'fusioninventory'), $_SERVER["PHP_SELF"],
"admin", "pluginfusioninventorymenu", "queuedinventory");

Session::checkRight('plugin_fusioninventory_queuedinventory', READ);
PluginFusioninventoryMenu::displayMenu("mini");

if (isset($_GET["id"])) {
$pfQueuedinventory->display(["id" => $_GET["id"]]);
} else {
$pfQueuedinventory->display(["id" => 0]);
}

Html::footer();

61 changes: 61 additions & 0 deletions front/queuedinventory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* FusionInventory
*
* Copyright (C) 2010-2016 by the FusionInventory Development Team.
*
* http://www.fusioninventory.org/
* https://github.com/fusioninventory/fusioninventory-for-glpi
* http://forge.fusioninventory.org/
*
* ------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of FusionInventory project.
*
* FusionInventory is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FusionInventory 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.
*
* ------------------------------------------------------------------------
*
* This file is used to manage the task search list.
*
* ------------------------------------------------------------------------
*
* @package FusionInventory
* @author Stanislas Kita
* @copyright Copyright (c) 2010-2020 FusionInventory team
* @license AGPL License 3.0 or (at your option) any later version
* http://www.gnu.org/licenses/agpl-3.0-standalone.html
* @link http://www.fusioninventory.org/
* @link https://github.com/fusioninventory/fusioninventory-for-glpi
*
*/

include ("../../../inc/includes.php");

Html::header(__('FusionInventory', 'fusioninventory'),
$_SERVER["PHP_SELF"],
"admin",
"pluginfusioninventorymenu",
"queuedinventory");

Session::checkRight('plugin_fusioninventory_queuedinventory', READ);

PluginFusioninventoryMenu::displayMenu("mini");

Search::show('PluginFusioninventoryQueuedinventory');
Html::footer();

53 changes: 48 additions & 5 deletions inc/communication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static function addLog($p_logs) {
* @param object $arrayinventory SimpleXMLElement
* @return boolean
*/
function import($arrayinventory) {
function import($arrayinventory, $xml, $is_cron = false) {

$pfAgent = new PluginFusioninventoryAgent();

Expand Down Expand Up @@ -224,9 +224,52 @@ function import($arrayinventory) {
if (isset($_SESSION['glpi_plugin_fusioninventory']['xmltags']["$xmltag"])) {
$moduleClass = $_SESSION['glpi_plugin_fusioninventory']['xmltags']["$xmltag"];
$moduleCommunication = new $moduleClass();
$errors.=$moduleCommunication->import($this->message['DEVICEID'],
$this->message['CONTENT'],
$arrayinventory);
$config = new PluginFusioninventoryConfig();
// if mode is direct or if call from cron -> import XML
if ($config->getValue('inventory_mode') == PluginFusioninventoryConfig::INVENTORY_DIRECT_MODE
|| $is_cron) {

$errors.=$moduleCommunication->import($this->message['DEVICEID'],
$this->message['CONTENT'],
$arrayinventory);

} else {

$queued = new PluginFusioninventoryQueuedinventory();
//if a row pending exist in DB, update it with new inventory
if ($queued->getFromDBByCrit([
'plugin_fusioninventory_agents_id' => $agent['id'],
'inventory_status' => PluginFusioninventoryQueuedinventory::STATUS_PENDING,
])) {
$queued->fields['date_creation'] = $_SESSION["glpi_currenttime"];
$queued->fields['xml_content'] = Toolbox::addslashes_deep($xml);
$queued->update($queued->fields);
} else {

//count all row with status pending
$count = countElementsInTable(PluginFusioninventoryQueuedinventory::getTable(),
['inventory_status' => PluginFusioninventoryQueuedinventory::STATUS_PENDING ]);

//if quto is not exceeded or if it illimited add inventory to queued
if ($count < $config->getValue('queued_max') || $config->getValue('queued_max') == 0) {
$queued_data = [
'name' => Rule::getUuid(),
'plugin_fusioninventory_agents_id' => $agent['id'],
'inventory_status' => PluginFusioninventoryQueuedinventory::STATUS_PENDING,
'date_creation' => $_SESSION["glpi_currenttime"],
'xml_content' => Toolbox::addslashes_deep($xml)
];
$queued->add($queued_data);
} else {
PluginFusioninventoryToolbox::logIfExtradebug(
'pluginFusioninventory-communication',
__("Can't add XML to queued, quota defined (".$count.") exceeded", 'fusioninventory')."\n"
);
$errors.=__("Can't add XML to queued, quota defined (".$count.") exceeded", 'fusioninventory')."\n";
}
}
}

} else {
$errors.=__('Unattended element in', 'fusioninventory').' QUERY : *'.$xmltag."*\n";
}
Expand Down Expand Up @@ -458,7 +501,7 @@ function handleOCSCommunication($rawdata, $xml = '', $output = 'ext') {
$agents_id = $agent->importToken($arrayinventory);
$_SESSION['plugin_fusioninventory_agents_id'] = $agents_id;

if (!$communication->import($arrayinventory)) {
if (!$communication->import($arrayinventory, $xml)) {

if ($deviceid != '') {

Expand Down
45 changes: 44 additions & 1 deletion inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class PluginFusioninventoryConfig extends CommonDBTM {
*/
CONST ACTION_STATUS = 1;

CONST INVENTORY_DIRECT_MODE = 0;
CONST INVENTORY_QUEUED_MODE = 1;

/**
* Initialize config values of fusioninventory plugin
Expand Down Expand Up @@ -167,6 +169,10 @@ function initConfigModule($getOnly = false) {
$input['auto_inventory_number_phone'] = '';
$input['auto_inventory_number_printer'] = '';

// options for queued inventory
$input['inventory_mode'] = self::INVENTORY_DIRECT_MODE;
$input['queued_max'] = 0;

if (!$getOnly) {
$this->addValues($input);
}
Expand Down Expand Up @@ -389,7 +395,26 @@ function showForm($id, $options = []) {
echo "</tr>";

echo "<tr class='tab_bg_1'>";
echo "<td colspan =2></td>";
echo "<td>".__('Inventory mode', 'fusioninventory')."</td>";
echo "<td >";
Dropdown::showFromArray('inventory_mode',
[self::getModes(self::INVENTORY_DIRECT_MODE), self::getModes(self::INVENTORY_QUEUED_MODE)],
['value' => $this->getValue('inventory_mode')]);
echo "</td>";

echo "<td>".__('Maximum number of XML to be add in queued', 'fusioninventory')."</td>";
echo "<td width='20%'>";
Dropdown::showNumber("queued_max",
[
'value' => $this->getValue('queued_max'),
'min' => 0,
'max' => 50000,
'step' => 10,
]
);
echo "</td>";

echo "<tr class='tab_bg_1'>";
echo "<td>".__('Maximum number of agents to wake up in a task', 'fusioninventory')."</td>";
echo "<td width='20%'>";
Dropdown::showNumber("wakeup_agent_max",
Expand Down Expand Up @@ -514,6 +539,24 @@ static function getActions($action) {
}
}

/**
* Get the action for agent action
*
* @param integer $action
* @return string
*/
static function getModes($mode) {
switch ($mode) {

case self::INVENTORY_DIRECT_MODE:
return __('Direct import', 'fusioninventory');

case self::INVENTORY_QUEUED_MODE:
return __('Queued', 'fusioninventory');

}
}


/**
* Display form for tab 'Inventory'
Expand Down
9 changes: 8 additions & 1 deletion inc/menu.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ static function getAdditionalMenuOptions() {
'deploymirror' => 'PluginFusioninventoryDeployMirror',
'deploygroup' => 'PluginFusioninventoryDeployGroup',
'deployuserinteractiontemplate' => 'PluginFusioninventoryDeployUserinteractionTemplate',
'ignoredimportdevice' => 'PluginFusioninventoryIgnoredimportdevice'
'ignoredimportdevice' => 'PluginFusioninventoryIgnoredimportdevice',
'queuedinventory' => 'PluginFusioninventoryQueuedinventory'
];
$options = [];

Expand Down Expand Up @@ -316,6 +317,12 @@ static function displayMenu($type = "big") {
$a_menu[12]['link'] = Toolbox::getItemTypeSearchURL('PluginFusioninventoryTimeslot');
}

if (Session::haveRight('plugin_fusioninventory_queuedinventory', READ)) {
$a_menu[13]['name'] = __('Inventory queue', 'fusioninventory');
$a_menu[13]['pic'] = $fi_path."/pics/menu_importxml.png";
$a_menu[13]['link'] = Toolbox::getItemTypeSearchURL('PluginFusioninventoryQueuedinventory');
}

if (!empty($a_menu)) {
$width_status = PluginFusioninventoryMenu::htmlMenu(__('Tasks', 'fusioninventory'),
$a_menu,
Expand Down
5 changes: 4 additions & 1 deletion inc/profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ function getRightsInventory() {
'field' => 'plugin_fusioninventory_lock'],
['itemtype' => 'PluginFusioninventoryCronTask',
'label' => __('Cron tasks', 'fusioninventory'),
'field' => 'plugin_fusioninventory_crontask']
'field' => 'plugin_fusioninventory_crontask'],
['itemtype' => 'PluginFusioninventoryQueuedinventory',
'label' => __('Inventory queue', 'fusioninventory'),
'field' => 'plugin_fusioninventory_queuedinventory']
];
return $rights;
}
Expand Down
Loading

0 comments on commit 7ce6474

Please sign in to comment.