Skip to content

Commit

Permalink
Merge pull request #458 from OCSInventory-NG/snmp_link_tag
Browse files Browse the repository at this point in the history
feat: Add SNMP_LINK_TAG conf option
  • Loading branch information
charleneauger committed Jun 3, 2024
2 parents 1eb3b5a + bfb4cab commit 80bc2aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
49 changes: 40 additions & 9 deletions Apache/Ocsinventory/Server/Capacities/Snmp/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,46 @@ sub insert_snmp_inventory{
$id = $rowCheck->{ID};
}

# If id is undefined insert row in snmp_accountinfo
if(!defined $id) {
my $queryInsert = $dbh->prepare("INSERT INTO `snmp_accountinfo`(SNMP_TYPE, SNMP_RECONCILIATION_FIELD, SNMP_RECONCILIATION_VALUE) VALUES(?,?,?)");
$queryInsert->bind_param(1, $key);
$queryInsert->bind_param(2, $reconciliation_field);
$queryInsert->bind_param(3, $value->{$reconciliation_field});

my $resultInsert = $queryInsert->execute or return undef;
if(!defined $resultInsert) { return 1; }
# handle SNMP_LINK_TAG enabled : assign the computer's TAG to the SNMP equipment
my $computerTag;
my $result = $Apache::Ocsinventory::CURRENT_CONTEXT{'XML_ENTRY'};
my $deviceid = $result->{DEVICEID};
if ($ENV{'OCS_OPT_SNMP_LINK_TAG'}) {
$computerTag = $dbh->prepare("SELECT TAG FROM accountinfo WHERE HARDWARE_ID = (SELECT ID FROM hardware WHERE DEVICEID = ?)");
$computerTag->bind_param(1, $deviceid);
$computerTag->execute;
$computerTag = $computerTag->fetchrow_hashref;

# if no accountinfo entry for this equipment
if (!defined $id) {
my $queryInsert = $dbh->prepare("INSERT INTO `snmp_accountinfo`(SNMP_TYPE, SNMP_RECONCILIATION_FIELD, SNMP_RECONCILIATION_VALUE, TAG) VALUES(?, ?, ?, ?)");
$queryInsert->bind_param(1, $key);
$queryInsert->bind_param(2, $reconciliation_field);
$queryInsert->bind_param(3, $value->{$reconciliation_field});
$queryInsert->bind_param(4, $computerTag->{TAG});

my $resultInsert = $queryInsert->execute or return undef;
if(!defined $resultInsert) { return 1; }
} else {
my $queryUpdate = $dbh->prepare("UPDATE `snmp_accountinfo` SET TAG = ? WHERE ID = ?");
$queryUpdate->bind_param(1, $computerTag->{TAG});
$queryUpdate->bind_param(2, $id);

my $resultUpdate = $queryUpdate->execute or return undef;
if(!defined $resultUpdate) { return 1; }
}

} else {
# no accountinfo entry and no link tag
if(!defined $id) {
my $queryInsert = $dbh->prepare("INSERT INTO `snmp_accountinfo`(SNMP_TYPE, SNMP_RECONCILIATION_FIELD, SNMP_RECONCILIATION_VALUE, TAG) VALUES(?, ?, ?)");
$queryInsert->bind_param(1, $key);
$queryInsert->bind_param(2, $reconciliation_field);
$queryInsert->bind_param(3, $value->{$reconciliation_field});

my $resultInsert = $queryInsert->execute or return undef;
if(!defined $resultInsert) { return 1; }
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions Apache/Ocsinventory/Server/System/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,14 @@ our %CONFIG = (
level => IMPORTANT,
filter => qr '^(1|0)$'
},
SNMP_LINK_TAG => {
type => 'IVALUE',
default => 0,
unit => 'NA',
description => 'Link SNMP device to computer TAG',
level => IMPORTANT,
filter => qr '^(1|0)$'
},
SCAN_TYPE_SNMP => {
type => 'TVALUE',
default => 'ICMP',
Expand Down

0 comments on commit 80bc2aa

Please sign in to comment.