glpi-project / glpi-inventory-plugin

GLPI Inventory plugin
GNU Affero General Public License v3.0
46 stars 27 forks source link

Agent don't use the right IP address for SNMP inventory #415

Closed NetItUp-FrancescoPasqualatto closed 1 year ago

NetItUp-FrancescoPasqualatto commented 1 year ago

Describe the bug

Hi, I've found a bug that when you run an inventory task on known snmp device, it will use the first IP address in the alphabetical order in the ip address table(it may not be the same address used for the snmp discovery).

To reproduce

  1. Run discovery snmp task
  2. Run inventory snmp task (and ip address are as I have explained)

Expected behavior

It import inventory information for the device.

Operating system

Windows, Linux

GLPI Agent version

1.5, 1.4

GLPI version

10.0.9, 10.0.7

GLPIInventory plugin

1.2.3

Additional context

I've found it with many devices but I'll use Synolgy as an example. When I run the inventory task and force inventory I see this and the device entry don't update with inventory information; bug 1 If you go on network ports tab you can see that the first ip address is not the one used for snmp(and if I remove that the inventory works fine): bug 2 I also checked this by packet capture:

24SEND
stonebuzz commented 1 year ago

Hi @NetItUp-FrancescoPasqualatto

your inventory task task is based on IPRange or on asset ?

NetItUp-FrancescoPasqualatto commented 1 year ago

I've tried both approaches but the result is the same: case 1 case 2 with glpi-injector the inventory is imported(or with changes on the ip table).

g-bougard commented 1 year ago

Hi @NetItUp-FrancescoPasqualatto have you checked the glpi-agent log to see which ip it finally uses during netinventory ? Just to be sure what ip was sent by the plugin.

NetItUp-FrancescoPasqualatto commented 1 year ago

Yes, this is part of the log that prove my point:

12SEND
stonebuzz commented 1 year ago

from IPRange target I can easily check the list of IPs and only (first) use the one that corresponds to the IPRange

from Asset target i can't compare with IPRange (logic !)

At one point we tried to keep a record of the IP that responded to the SNMP discovery, but it was much more complex than we expected.

Can you try this (try only with IPRAnge target) https://github.com/glpi-project/glpi-inventory-plugin/pull/416

NetItUp-FrancescoPasqualatto commented 1 year ago

with your code it gave me: Error "device has no ip": [debug] WakeOnLan task execution not requested [error] invalid device: no address defined [error] invalid job: no valid device defined [error] no valid job found, aborting

Once I have programmed this bad code:

$ip_a = PluginGlpiinventoryToolbox::getIPforDevice( $jobstate->fields['itemtype'], $jobstate->fields['items_id'] ); $ip = ''; if(count($ip_a) > 1) { $ip1 = current($ip_a); $ip2 = next($ip_a); if($ip2 != '') { $ip = $ip2; } else { $ip = $ip1; } } else { $ip = current($ip_a); }

It update everything but works only if the snmp address is in the second place. (I don't know if it can be useful)

stonebuzz commented 1 year ago

Are you sure you tested with an IPRAnge?

It seems not, I've adjusted the PR to keep the same behaviour as now (first IP is used) when the target is an Asset

NetItUp-FrancescoPasqualatto commented 1 year ago

I've used IPRange as target for the task... case 3

stonebuzz commented 1 year ago

Can you run this SQL query and past here the result

SELECT * FROM glpi_plugin_glpiinventory_taskjobstates WHERE plugin_glpiinventory_taskjobs_id = ID_OF_TASK
NetItUp-FrancescoPasqualatto commented 1 year ago

I've used the id of the IPRange task.

table.txt

cedric-anne commented 1 year ago

You should look at my last comment on #416 . I guess there was a mistake in the proposal.

stonebuzz commented 1 year ago

The problem is that an SNMP inventory task creates a job for each asset (and not a job with the IP Range).

So I can't check the IP from Range

stonebuzz commented 1 year ago

I don't know if I can do any better.

Since I can't compare the asset IPs with the IPRange and since we don't keep the contact IP, it's impossible for me to deduce the correct IP.

Perhaps you could add unwanted IPs to the black list to prevent them from being back

image

NetItUp-FrancescoPasqualatto commented 1 year ago

What If we try all Ip address? or keep the contact IP in a new field?

stonebuzz commented 1 year ago

What If we try all Ip address?

When Agent want a JOB configuration plugin return one IP (and XML body have only one IP node)

keep the contact IP in a new field

It's going to take a lot of effort for a single reported issue

g-bougard commented 1 year ago

@NetItUp-FrancescoPasqualatto And to amend @stonebuzz comment, we will probably rewrite that code in a near future, mayby making it native to glpi. So we don't want to pass too much time on this legacy code. Indeed, identifying such problem can just help us to start the rewrite earlier than actually expected.

NetItUp-FrancescoPasqualatto commented 1 year ago

Ok, thanks. I understand that there isn't a quick solution to this problem. I think it should be pretty common because it's easy to have IP addresses in the wrong order.

stonebuzz commented 1 year ago

Perhaps you could add unwanted IPs to the black list to prevent them from being back

Should solved this issue.

not being able to go any further for the moment I take the liberty of closing this issue

Best regards