Skip to content

feat(inventory): Map network port metrics and logical_number for computer assets#24508

Draft
eduardomozart wants to merge 17 commits into
glpi-project:mainfrom
eduardomozart:feat-netadapter-invent-stats
Draft

feat(inventory): Map network port metrics and logical_number for computer assets#24508
eduardomozart wants to merge 17 commits into
glpi-project:mainfrom
eduardomozart:feat-netadapter-invent-stats

Conversation

@eduardomozart

@eduardomozart eduardomozart commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Checklist before requesting a review

Please delete options that are not relevant.

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.
  • I have added tests that prove my fix is effective or that my feature works.
  • This change requires a documentation update.

Description

Closes #24509

Note

(There is an ongoing discussion about this feature at #24509)

This PR introduces server-side support for mapping network port statistics and interface metrics to computer assets during inventory. Vital interface statistics like the Number of I/O bytes (ifinbytes, ifoutbytes, ifinerrors, ifouterrors) and the Port Number (logical_number / ifnumber) are now correctly extracted from the payload and mapped to the respective NetworkCard and NetworkPort records.

image

This brings host-level network monitoring parity to computer assets. While metrics are traditionally associated with network equipment (like manageable switches), capturing them at the computer/host level provides direct insight into the actual traffic processed by the server's or workstation's OS, regardless of the upstream network topology. This mapping does not conflict with or affect switch metrics. Computer network ports and switch network ports remain separate entities in the database, meaning each retains its own independent telemetry data.

Key Under-the-Hood Changes:

  • Processes network_ports data from the inventory payload and links it to NetworkCard entries by matching MAC addresses and interface names.
  • Correctly maps ifnumber to the interface's logical_number.
  • Validates network port data and refactors NetworkCard port handling for simplicity and accuracy.
  • Fixes typing, method guards, and updates PHPStan baselines accordingly.

Warning

Dependency & Compatibility Notice

1. Agent Requirement: For this feature to work, the GLPI-Agent must be patched to collect and send these statistics. See the companion PR: glpi-project/glpi-agent#1184.

2. Backward Compatibility Warning: Although sending NETWORK_PORTS for computers is technically valid according to the inventory schema, older versions of GLPI do not expect this data for non-NetworkEquipment assets. If an updated agent sends this payload to an older GLPI server, it will trigger a fatal error:

ERROR: 500 Internal Server Error, Call to undefined method Glpi\Inventory\MainAsset\Computer::getManagementPorts()

(A similar error also occurs with isStackedSwitch()).

Because of this, the GLPI-Agent must perform a version check against GLPI to ensure it only transmits the NETWORK_PORTS payload to compatible GLPI instances (such as the version containing this PR).

How to test:

  1. Run an inventory using a GLPI-Agent patched with PR #1184.
  2. Go to Assets > Computers > [Your Computer] > Network ports.
  3. Ensure the "Port number" and I/O metrics columns (such as "Bytes received", "Bytes sent") are visible and accurately populated.
  4. Verify that the speed is correctly displayed in "Mbps" or "Gbps" and matches the expected hardware speed.
  5. Verify that the telemetry data reflects the agent's natively reported values.

Add icon entries to CommonDevice menu options using getIcon() so device/type menu items include icons. In NetworkCard, map 'model' to devicenetworkcardmodels_id and populate designation from model or description when designation is missing, ensuring cards have a sensible name when model data is present.
Add support for an extra_data.network_ports array in NetworkCard and use it to match ports by MAC or name to populate logical_number from ifnumber. Also add method_exists guards in NetworkPort before calling getManagementPorts and isStackedSwitch to avoid fatal errors when those methods are not present. Small mapping reorder (speed) was adjusted while integrating the new matching logic.
Ensure safer handling of network port data and asset type checks: in NetworkCard, verify extra_data['network_ports'] is iterable, assign it to a local variable, add a docblock, and cast port properties to string before lowercasing to avoid warnings on unexpected types. In NetworkPort, replace method_exists checks with instanceof checks against Glpi\Inventory\MainAsset\NetworkEquipment and Glpi\Inventory\MainAsset\Printer (fully-qualified) when accessing management ports and stacked-switch logic, improving type safety and clarity.
Add use statements for NetworkEquipment and Printer in NetworkPort.php and replace fully-qualified class names with the imported names for clearer instanceof checks. Also apply minor spacing cleanup around (string) casts in NetworkCard.php. These are refactors only and do not change behavior.
Remove network_ports from NetworkCard extra_data and simplify port normalization logic: set logical_number only when missing and normalize virtualdev to 0 unless explicitly 1. This removes the previous cross-check against extra_data['network_ports'] (matching by MAC/name) to derive logical_number. Add a functional test (testComputerNetworkPortMetrics) that exercises NetworkCard then NetworkPort processing, asserting that network port metrics are saved and that logical_number is set as expected.
Add use statement for NetworkCard and replace fully-qualified instantiations with imported class names (Converter, Conf, NetworkCard) in tests/functional/Glpi/Inventory/Assets/NetworkPortTest.php. Also apply minor whitespace cleanup to improve readability and consistency in the test.
Add support for linking extra_data['network_ports'] entries to inventory ports and using their ifnumber as the port's logical_number. Introduce 'network_ports' to NetworkCard::extra_data, adjust virtualdev/logical_number initialization, and iterate network_ports to match by mac and name, setting logical_number when a match with ifnumber is found. Update phpstan baseline: add an ignore for undefined property $mtu in NetworkCard.php and increment a related classConstant.nonObject count for NetworkPort.php.
Increment the 'count' for the offsetAccess.invalidOffset entry in .phpstan-baseline.specific.php (path: src/Glpi/Inventory/Asset/NetworkPort.php) from 4 to 5 to account for an additional reported issue.
Delete the 'mtu' entry from NetworkCard's attribute mapping and remove the corresponding phpstan baseline ignore for an undefined $mtu property. Cleans up an unused mapping and eliminates the need for a suppression entry in .phpstan-baseline.php.
@trasher

trasher commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Will wait for Agent PR.

@eduardomozart eduardomozart marked this pull request as draft June 10, 2026 22:48
@eduardomozart eduardomozart marked this pull request as ready for review June 11, 2026 02:47
Comment thread src/Glpi/Inventory/Asset/NetworkCard.php Outdated
Comment thread tests/functional/Glpi/Inventory/Assets/NetworkPortTest.php
@eduardomozart eduardomozart requested a review from stonebuzz June 11, 2026 13:18
@trasher trasher marked this pull request as draft June 12, 2026 05:33
@trasher

trasher commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Back to draft since agent change has not been validated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants