Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Enhance rule mactched logs with display in item. We add / display the…
Browse files Browse the repository at this point in the history
… criteria used in the rules
  • Loading branch information
David Durieux authored and ddurieux committed Oct 24, 2019
1 parent 4e624cc commit 65aa297
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 5 deletions.
9 changes: 9 additions & 0 deletions css/views.css
Original file line number Diff line number Diff line change
Expand Up @@ -980,3 +980,12 @@ table.runs td.comment {
.beta + h2 {
text-align: center;
}

hr.criteriarule {
border: 0;
height: 1px;
background-image: -webkit-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
background-image: -moz-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
background-image: -ms-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
background-image: -o-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
}
2 changes: 1 addition & 1 deletion inc/communicationnetworkdiscovery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ function addtaskjoblog() {
* @return string
*/
static function getMethod() {
return 'netdiscovery';
return 'networkdiscovery';
}


Expand Down
5 changes: 3 additions & 2 deletions inc/communicationnetworkinventory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ function rulepassed($items_id, $itemtype, $ports_id = 0) {
}
$inputrulelog['items_id'] = $items_id;
$inputrulelog['itemtype'] = $itemtype;
$inputrulelog['method'] = 'snmpinventory';
$inputrulelog['method'] = 'networkinventory';
$inputrulelog['criteria'] = exportArrayToDB(unserialize($_SESSION['plugin_fusinvsnmp_datacriteria']));
$pfRulematchedlog->add($inputrulelog);
$pfRulematchedlog->cleanOlddata($items_id, $itemtype);
unset($_SESSION['plugin_fusioninventory_rules_id']);
Expand Down Expand Up @@ -574,7 +575,7 @@ function addtaskjoblog() {
* @return string
*/
static function getMethod() {
return 'snmpinventory';
return 'networkinventory';
}


Expand Down
21 changes: 21 additions & 0 deletions inc/inventorynetworkequipmentlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ function importConnectionLLDP($a_lldp, $networkports_id) {

// Entity?
$rule = new PluginFusioninventoryInventoryRuleImportCollection();
$_SESSION['plugin_fusinvsnmp_datacriteria'] = serialize($input_crit);

// * Reload rules (required for unit tests)
$rule->getCollectionPart();
Expand Down Expand Up @@ -461,6 +462,7 @@ function importConnectionMac($a_portconnection, $networkports_id) {
// Pass all MAC addresses in the import rules
foreach ($a_portconnection as $ifmac) {
$this->data_device = ['mac' => $ifmac];
$_SESSION['plugin_fusinvsnmp_datacriteria'] = serialize(['mac' => $ifmac]);
$data = $rule->processAllRules(['mac' => $ifmac], [], ['class'=>$this]);
}
$list_all_ports_found = [];
Expand Down Expand Up @@ -716,6 +718,7 @@ function rulepassed($items_id, $itemtype, $ports_id = 0) {
$itemtype = "PluginFusioninventoryUnmanaged";
}
$class = new $itemtype;
$dbu = new DbUtils();
if ($items_id == "0") {
// create the device
$input = $this->data_device;
Expand All @@ -727,6 +730,24 @@ function rulepassed($items_id, $itemtype, $ports_id = 0) {
$input['name'] = $manufacturer;
}
$items_id = $class->add($input);

if (isset($_SESSION['plugin_fusioninventory_rules_id'])) {
$pfRulematchedlog = new PluginFusioninventoryRulematchedlog();
$inputrulelog = [];
$inputrulelog['date'] = date('Y-m-d H:i:s');
$inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
if (isset($_SESSION['plugin_fusioninventory_agents_id'])) {
$inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id'];
}
$inputrulelog['items_id'] = $items_id;
$inputrulelog['itemtype'] = $itemtype;
$inputrulelog['method'] = 'networkinventory';
$inputrulelog['criteria'] = $dbu->exportArrayToDB(unserialize($_SESSION['plugin_fusinvsnmp_datacriteria']));
$pfRulematchedlog->add($inputrulelog);
$pfRulematchedlog->cleanOlddata($items_id, $itemtype);
unset($_SESSION['plugin_fusioninventory_rules_id']);
}

// Create the network port
$input = [
'items_id' => $items_id,
Expand Down
11 changes: 11 additions & 0 deletions inc/inventoryruleimport.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,11 @@ function executeActions($output, $params, array $input = []) {
if (!isset($params['return'])) {
$inputrulelog['items_id'] = $items_id;
$inputrulelog['itemtype'] = $itemtype;
$critinput = $this->criteriaInput;
if (isset($critinput['class'])) {
unset($critinput['class']);
}
$inputrulelog['criteria'] = exportArrayToDB($critinput);
$pfRulematchedlog->add($inputrulelog);
$pfRulematchedlog->cleanOlddata($items_id, $itemtype);
$class->rulepassed($items_id, $itemtype, $this->criterias_results['found_port']);
Expand Down Expand Up @@ -1283,4 +1288,10 @@ static function getItemTypesForRules() {
ksort($types);
return $types;
}

function prepareInputDataForProcess($input, $params) {
$this->criteriaInput = $input;
return $input;
}

}
23 changes: 21 additions & 2 deletions inc/rulematchedlog.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ function showForm($items_id, $itemtype) {
if ($class) {
$class->showDownloadInventoryFile($items_id);
}

if (isset($_GET["start"])) {
$start = $_GET["start"];
} else {
Expand All @@ -246,7 +245,7 @@ function showForm($items_id, $itemtype) {
echo "<table class='tab_cadre_fixe' cellpadding='1'>";

echo "<tr>";
echo "<th colspan='4'>";
echo "<th colspan='5'>";
echo __('Rule import logs', 'fusioninventory');

echo "</th>";
Expand All @@ -268,6 +267,10 @@ function showForm($items_id, $itemtype) {
echo "<th>";
echo __('Module', 'fusioninventory');

echo "</th>";
echo "<th>";
echo _n('Criterion', 'Criteria', 2);

echo "</th>";
echo "</tr>";

Expand Down Expand Up @@ -300,6 +303,22 @@ function showForm($items_id, $itemtype) {
}
}
echo "</td>";
echo "<td>";
$criteria = importArrayFromDB($data['criteria']);
if (count($criteria) > 0) {
echo "<ul style='list-style-type:disc'>";
foreach ($criteria as $key=>$value) {
echo "<li>".$key.": ";
if (is_array($value)) {
echo implode("<br>", $value);
} else {
echo $value;
}
echo "</li>";
}
echo "</ul><hr class='criteriarule'>";
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
Expand Down
1 change: 1 addition & 0 deletions install/mysql/plugin_fusioninventory-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ CREATE TABLE `glpi_plugin_fusioninventory_rulematchedlogs` (
`rules_id` int(11) NOT NULL DEFAULT '0',
`plugin_fusioninventory_agents_id` int(11) NOT NULL DEFAULT '0',
`method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`criteria` text DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `item` (`itemtype`,`items_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Expand Down
3 changes: 3 additions & 0 deletions install/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,9 @@ function do_rulematchedlog_migration($migration) {
$migration->addField($newTable,
"method",
"varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL");
$migration->addField($newTable,
"criteria",
'text COLLATE utf8_unicode_ci DEFAULT NULL');
$migration->migrationOneTable($newTable);
$DB->list_fields($newTable, false);
}
Expand Down
1 change: 1 addition & 0 deletions phpunit/1_Unit/ImportRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ public function rulepassedNetworkEquipment_nodevice() {
$pf->data_device = [
"name" => "test01"
];
$_SESSION['plugin_fusinvsnmp_datacriteria'] = serialize(["name" => "test01"]);
$pf->rulepassed(0, "", 0);

$this->assertEquals(1, $dbu->countElementsInTable('glpi_networkports'));
Expand Down
Loading

0 comments on commit 65aa297

Please sign in to comment.