<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package GLPI::Agent::Task::Inventory::Generic::Dmidecode::Memory;

use strict;
use warnings;

use parent 'GLPI::Agent::Task::Inventory::Module';

use GLPI::Agent::Tools;
use GLPI::Agent::Tools::Generic;
use GLPI::Agent::Tools::PartNumber;

use constant    category    =&gt; "memory";

# Run after virtualization to decide if found component is virtual
our $runAfterIfEnabled = [ qw(
    GLPI::Agent::Task::Inventory::Vmsystem
    GLPI::Agent::Task::Inventory::Win32::Hardware
    GLPI::Agent::Task::Inventory::Linux::Memory
    GLPI::Agent::Task::Inventory::BSD::Memory
)];

sub isEnabled {
    return 1;
}

sub doInventory {
    my (%params) = @_;

    my $inventory = $params{inventory};
    my $logger    = $params{logger};

    my $memories = _getMemories(logger =&gt; $logger);

    return unless $memories;

    # If only one component is defined and we are under a vmsystem, we can update
    # component capacity to real found size. This permits to support memory size updates.
    my $vmsystem = $inventory-&gt;getHardware('VMSYSTEM');
    if ($vmsystem &amp;&amp; $vmsystem ne 'Physical') {
        my @components = grep { exists $_-&gt;{CAPACITY} } @$memories;
        if ( @components == 1) {
            my $real_memory = $inventory-&gt;getHardware('MEMORY');
            my $component = shift @components;
            if (!$real_memory) {
                $logger-&gt;debug2("Can't verify real memory capacity on this virtual machine");
            } elsif (!$component-&gt;{CAPACITY} || $component-&gt;{CAPACITY} != $real_memory) {
                $logger-&gt;debug2($component-&gt;{CAPACITY} ?
                    "Updating virtual component memory capacity to found real capacity: $component-&gt;{CAPACITY} =&gt; $real_memory"
                    : "Setting virtual component memory capacity to $real_memory"
                );
                $component-&gt;{CAPACITY} = $real_memory;
            }
        }
    }

    foreach my $memory (@$memories) {
        $inventory-&gt;addEntry(
            section =&gt; 'MEMORIES',
            entry   =&gt; $memory
        );
    }
}

sub _getMemories {
    my $infos = getDmidecodeInfos(@_);

    my ($memories, $slot, %defaults);

    my $bios_infos   = $infos-&gt;{0}-&gt;[0];
    my $bios_vendor  = $bios_infos-&gt;{Vendor} // "";
    my $bios_version = $bios_infos-&gt;{Version}      // "";

    if ($bios_vendor =~ /^Microsoft/i &amp;&amp; $bios_version =~ /^Hyper-V/i) {
        %defaults = (
            Description  =&gt; "Hyper-V Memory",
            Manufacturer =&gt; $bios_vendor,
        );
    }

    if ($infos-&gt;{17}) {

        foreach my $info (@{$infos-&gt;{17}}) {
            $slot++;

            # Flash is 'in general' an unrelated internal BIOS storage
            # See bug: #1334
            next if $info-&gt;{'Type'} &amp;&amp; $info-&gt;{'Type'} =~ /Flash/i;

            my $manufacturer;
            if (
                $info-&gt;{'Manufacturer'}
                    &amp;&amp;
                ( $info-&gt;{'Manufacturer'} !~ /
                  Manufacturer
                      |
                  Undefined
                      |
                  None
                      |
                  ^0x
                      |
                  00000
                      |
                  \sDIMM
                  /ix )
            ) {
                $manufacturer = $info-&gt;{'Manufacturer'};
            }

            my $memory = {
                NUMSLOTS         =&gt; $slot,
                DESCRIPTION      =&gt; $info-&gt;{'Form Factor'} // $defaults{Description},
                CAPTION          =&gt; $info-&gt;{'Locator'},
                SPEED            =&gt; getCanonicalSpeed($info-&gt;{'Speed'}),
                TYPE             =&gt; $info-&gt;{'Type'},
                SERIALNUMBER     =&gt; $info-&gt;{'Serial Number'},
                MEMORYCORRECTION =&gt; $infos-&gt;{16}[0]{'Error Correction Type'},
                MANUFACTURER     =&gt; $manufacturer // $defaults{Manufacturer}
            };

            if ($info-&gt;{'Size'} &amp;&amp; $info-&gt;{'Size'} =~ /^(\d+ \s .B)$/x) {
                $memory-&gt;{CAPACITY} = getCanonicalSize($1, 1024);
            }

            if ($info-&gt;{'Part Number'}
                    &amp;&amp;
                $info-&gt;{'Part Number'} !~ /
                    DIMM            |
                    Part\s*Num      |
                    Ser\s*Num
                /xi
            ) {
                $memory-&gt;{MODEL} = trimWhitespace(
                    getSanitizedString( hex2char($info-&gt;{'Part Number'}) )
                );
                my $partnumber_factory = GLPI::Agent::Tools::PartNumber-&gt;new(@_);
                my $partnumber = $partnumber_factory-&gt;match(
                    partnumber  =&gt; $memory-&gt;{MODEL},
                    category    =&gt; "memory",
                );
                if ($partnumber) {
                    $memory-&gt;{MANUFACTURER} = $partnumber-&gt;manufacturer;
                    $memory-&gt;{SPEED} = $partnumber-&gt;speed
                        if !$memory-&gt;{SPEED} &amp;&amp; $partnumber-&gt;speed;
                    $memory-&gt;{TYPE} = $partnumber-&gt;type
                        if !$memory-&gt;{TYPE} &amp;&amp; $partnumber-&gt;type;
                }
            }

            push @$memories, $memory;
        }
    } elsif ($infos-&gt;{6}) {

        foreach my $info (@{$infos-&gt;{6}}) {
            $slot++;

            my $memory = {
                NUMSLOTS =&gt; $slot,
                TYPE     =&gt; $info-&gt;{'Type'},
            };

            if ($info-&gt;{'Installed Size'} &amp;&amp; $info-&gt;{'Installed Size'} =~ /^(\d+\s*.B)/i) {
                $memory-&gt;{CAPACITY} = getCanonicalSize($1, 1024);
            }

            push @$memories, $memory;
        }
    }

    return $memories;
}

1;
</pre></body></html>