Skip to content

Commit

Permalink
feat: Add SNMP_LINK_TAG conf option for linking SNMP devices to compu…
Browse files Browse the repository at this point in the history
…ter TAG
  • Loading branch information
Lea9250 committed May 30, 2024
1 parent 1eb3b5a commit 4efedf7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
50 changes: 41 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,47 @@ 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 = ?");
print Dumper($computerTag->{TAG});
$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 4efedf7

Please sign in to comment.