Skip to content

Commit 6daff3d

Browse files
author
Florian Blanchet
authored
Merge pull request #3 from itsmng/add-rules-snmp
Add snmp to rule entity process
2 parents 56913dc + 2e92060 commit 6daff3d

File tree

2 files changed

+105
-50
lines changed

2 files changed

+105
-50
lines changed

hook.php

+61-50
Original file line numberDiff line numberDiff line change
@@ -1389,68 +1389,79 @@ function plugin_ocsinventoryng_ruleCollectionPrepareInputDataForProcess($params)
13891389

13901390
$ocsClient = PluginOcsinventoryngOcsServer::getDBocs($ocsservers_id);
13911391

1392-
$tables = array_keys(plugin_ocsinventoryng_getTablesForQuery($params['rule_itemtype']));
1393-
$fields = plugin_ocsinventoryng_getFieldsForQuery($params['rule_itemtype']);
1392+
if(!isset($params['values']['input']['type'])) {
1393+
$tables = array_keys(plugin_ocsinventoryng_getTablesForQuery($params['rule_itemtype']));
1394+
$fields = plugin_ocsinventoryng_getFieldsForQuery($params['rule_itemtype']);
1395+
1396+
$ocsComputer = $ocsClient->getOcsComputer($ocsid, $tables);
1397+
1398+
if (!is_null($ocsComputer) && count($ocsComputer) > 0) {
1399+
if (isset($ocsComputer['NETWORKS'])) {
1400+
$networks = $ocsComputer['NETWORKS'];
1401+
1402+
$ipblacklist = Blacklist::getIPs();
1403+
$macblacklist = Blacklist::getMACs();
1404+
1405+
foreach ($networks as $data) {
1406+
if (isset($data['IPSUBNET'])) {
1407+
$rule_parameters['IPSUBNET'][] = $data['IPSUBNET'];
1408+
}
1409+
if (isset($data['MACADDR']) && !in_array($data['MACADDR'], $macblacklist)) {
1410+
$rule_parameters['MACADDRESS'][] = $data['MACADDR'];
1411+
}
1412+
if (isset($data['IPADDRESS']) && !in_array($data['IPADDRESS'], $ipblacklist)) {
1413+
$rule_parameters['IPADDRESS'][] = $data['IPADDRESS'];
1414+
}
1415+
}
1416+
}
1417+
$ocs_data = [];
13941418

1395-
$ocsComputer = $ocsClient->getOcsComputer($ocsid, $tables);
1419+
foreach ($fields as $field) {
1420+
// TODO cleaner way of getting fields
1421+
$field = explode('.', $field);
1422+
if (count($field) < 2) {
1423+
continue;
1424+
}
13961425

1397-
if (!is_null($ocsComputer) && count($ocsComputer) > 0) {
1398-
if (isset($ocsComputer['NETWORKS'])) {
1399-
$networks = $ocsComputer['NETWORKS'];
1426+
$table = strtoupper($field[0]);
14001427

1401-
$ipblacklist = Blacklist::getIPs();
1402-
$macblacklist = Blacklist::getMACs();
1428+
$fieldSql = explode(' ', $field[1]);
1429+
$ocsField = $fieldSql[0];
1430+
$glpiField = $fieldSql[count($fieldSql) - 1];
14031431

1404-
foreach ($networks as $data) {
1405-
if (isset($data['IPSUBNET'])) {
1406-
$rule_parameters['IPSUBNET'][] = $data['IPSUBNET'];
1407-
}
1408-
if (isset($data['MACADDR']) && !in_array($data['MACADDR'], $macblacklist)) {
1409-
$rule_parameters['MACADDRESS'][] = $data['MACADDR'];
1432+
$section = [];
1433+
if (isset($ocsComputer[$table])) {
1434+
$section = $ocsComputer[$table];
14101435
}
1411-
if (isset($data['IPADDRESS']) && !in_array($data['IPADDRESS'], $ipblacklist)) {
1412-
$rule_parameters['IPADDRESS'][] = $data['IPADDRESS'];
1436+
if (array_key_exists($ocsField, $section)) {
1437+
// Not multi
1438+
$ocs_data[$glpiField][] = $section[$ocsField];
1439+
} else {
1440+
foreach ($section as $sectionLine) {
1441+
$ocs_data[$glpiField][] = $sectionLine[$ocsField];
1442+
}
14131443
}
14141444
}
1415-
}
1416-
$ocs_data = [];
14171445

1418-
foreach ($fields as $field) {
1419-
// TODO cleaner way of getting fields
1420-
$field = explode('.', $field);
1421-
if (count($field) < 2) {
1422-
continue;
1423-
}
1424-
1425-
$table = strtoupper($field[0]);
1426-
1427-
$fieldSql = explode(' ', $field[1]);
1428-
$ocsField = $fieldSql[0];
1429-
$glpiField = $fieldSql[count($fieldSql) - 1];
1430-
1431-
$section = [];
1432-
if (isset($ocsComputer[$table])) {
1433-
$section = $ocsComputer[$table];
1434-
}
1435-
if (array_key_exists($ocsField, $section)) {
1436-
// Not multi
1437-
$ocs_data[$glpiField][] = $section[$ocsField];
1438-
} else {
1439-
foreach ($section as $sectionLine) {
1440-
$ocs_data[$glpiField][] = $sectionLine[$ocsField];
1441-
}
1446+
//This case should never happend but...
1447+
//Sometimes OCS can't find network ports but fill the right ip in hardware table...
1448+
//So let's use the ip to proceed rules (if IP is a criteria of course)
1449+
if (in_array("IPADDRESS", $fields) && !isset($ocs_data['IPADDRESS'])) {
1450+
$ocs_data['IPADDRESS']
1451+
= PluginOcsinventoryngOcsProcess::getGeneralIpAddress($ocsservers_id, $ocsid);
14421452
}
1453+
return array_merge($rule_parameters, $ocs_data);
14431454
}
1444-
1445-
//This case should never happend but...
1446-
//Sometimes OCS can't find network ports but fill the right ip in hardware table...
1447-
//So let's use the ip to proceed rules (if IP is a criteria of course)
1448-
if (in_array("IPADDRESS", $fields) && !isset($ocs_data['IPADDRESS'])) {
1449-
$ocs_data['IPADDRESS']
1450-
= PluginOcsinventoryngOcsProcess::getGeneralIpAddress($ocsservers_id, $ocsid);
1455+
} else {
1456+
// Set empty TAG (maybe use the snmp TAG accountinfo in the futur ?)
1457+
$rule_parameters['TAG'][] = "";
1458+
if(isset($params['values']['params']['snmpdata']['NetworkName__ipaddresses'])) {
1459+
foreach($params['values']['params']['snmpdata']['NetworkName__ipaddresses'] as $key => $ipaddress) {
1460+
$rule_parameters['IPADDRESS'][] = $ipaddress;
1461+
}
14511462
}
1452-
return array_merge($rule_parameters, $ocs_data);
14531463

1464+
return $rule_parameters;
14541465
}
14551466
}
14561467
return [];

inc/snmplinkrework.class.php

+44
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,28 @@ static function importSnmp($ocsids, $OCSServerId, $params) {
502502
$inputNetwork = array_merge($defaultInputNetwork, $inputNetwork);
503503
$NetworkPort = new NetworkPort();
504504
$NetworkPort->add($inputNetwork);
505+
$input = array_merge($input, $inputNetwork);
506+
}
507+
508+
//Process entity rules
509+
$ruleCollection = new RuleImportEntityCollection();
510+
$fields = $ruleCollection->processAllRules(
511+
[
512+
'ocsservers_id' => $OCSServerId,
513+
'_source' => 'ocsinventoryng',
514+
'type' => 'snmp'
515+
],
516+
[],
517+
[
518+
'ocsid' => $OCSSnmpRowId,
519+
'snmpdata' => $input
520+
]
521+
);
522+
523+
if(isset($fields['entities_id']) && $fields['entities_id'] >= 0) {
524+
unset($fields["_ruleid"]);
525+
$fields['id'] = $objId;
526+
$Equipment->update($fields);
505527
}
506528

507529
$date = date("Y-m-d H:i:s");
@@ -637,6 +659,28 @@ static function updateSnmp($ocsids, $OCSServerId) {
637659
$update = $NetworkPort->update($inputNetwork);
638660
}
639661
}
662+
$input = array_merge($input, $inputNetwork);
663+
}
664+
665+
//Process entity rules
666+
$ruleCollection = new RuleImportEntityCollection();
667+
$fields = $ruleCollection->processAllRules(
668+
[
669+
'ocsservers_id' => $OCSServerId,
670+
'_source' => 'ocsinventoryng',
671+
'type' => 'snmp'
672+
],
673+
[],
674+
[
675+
'ocsid' => $ITSMSnmpRowData["items_id"],
676+
'snmpdata' => $input
677+
]
678+
);
679+
680+
if(isset($fields['entities_id']) && $fields['entities_id'] >= 0) {
681+
unset($fields["_ruleid"]);
682+
$fields['id'] = $ITSMSnmpRowData["items_id"];
683+
$Equipment->update($fields);
640684
}
641685

642686
$date = date("Y-m-d H:i:s");

0 commit comments

Comments
 (0)