Skip to content

Commit

Permalink
use consistant WMI access interface
Browse files Browse the repository at this point in the history
  • Loading branch information
guillomovitch committed Mar 10, 2012
1 parent 25361c9 commit a850719
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/Networks.pm
Expand Up @@ -3,16 +3,10 @@ package FusionInventory::Agent::Task::Inventory::Input::Win32::Networks;
use strict;
use warnings;

use Win32::OLE qw(in CP_UTF8);
use Win32::OLE::Const;
use Win32::OLE::Enum;

use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Network;
use FusionInventory::Agent::Tools::Win32;

Win32::OLE-> Option(CP=>CP_UTF8);

# http://techtasks.com/code/viewbookcode/1417
sub isEnabled {
return 1;
Expand Down Expand Up @@ -63,37 +57,35 @@ sub doInventory {

sub _getInterfaces {

my $WMIService = Win32::OLE->GetObject(
'winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2'
) or die "WMI connection failed: " . Win32::OLE->LastError();

my @ip6s;
my @interfaces;

my $nics = $WMIService->ExecQuery(
'SELECT * FROM Win32_NetworkAdapterConfiguration'
);
foreach my $nic (in $nics) {
foreach my $object (getWmiObjects(
class => 'Win32_NetworkAdapterConfiguration',
properties => [ qw/Index Description IPEnabled DHCPServer MACAddress
MTU DefaultIPGateway DNSServerSearchOrder IPAddress
IPSubnet/ ]
)) {

my $interface = {
DESCRIPTION => $nic->Description,
STATUS => $nic->IPEnabled ? "Up" : "Down",
IPDHCP => $nic->DHCPServer,
MACADDR => $nic->MACAddress,
MTU => $nic->MTU
DESCRIPTION => $object->{Description},
STATUS => $object->{IPEnabled} ? "Up" : "Down",
IPDHCP => $object->{DHCPServer},
MACADDR => $object->{MACAddress},
MTU => $object->{MTU}
};

if ($nic->DefaultIPGateway()) {
$interface->{IPGATEWAY} = $nic->DefaultIPGateway()->[0];
if ($object->{DefaultIPGateway}) {
$interface->{IPGATEWAY} = $object->{DefaultIPGateway}->[0];
}

if ($nic->DNSServerSearchOrder()) {
$interface->{dns} = $nic->DNSServerSearchOrder()->[0];
if ($object->{DNSServerSearchOrder}) {
$interface->{dns} = $object->{DNSServerSearchOrder}->[0];
}

if ($nic->IPAddress) {
foreach my $addrId (0..(@{$nic->IPAddress}-1)) {
my $address = $nic->IPAddress->[$addrId];
my $mask = $nic->IPSubnet->[$addrId];
if ($object->{IPAddress}) {
foreach my $address (@{$object->{IPAddress}}) {
my $mask = shift @{$object->{IPSubnet}};
if ($address =~ /$ip_address_pattern/) {
push @{$interface->{IPADDRESS}}, $address;
push @{$interface->{IPMASK}}, $mask;
Expand All @@ -108,37 +100,39 @@ sub _getInterfaces {
}
}

$interfaces[$nic->Index] = $interface;
$interfaces[$object->{Index}] = $interface;
}

$nics = $WMIService->ExecQuery('SELECT * FROM Win32_NetworkAdapter');
foreach my $nic (in $nics) {
my $interface = $interfaces[$nic->Index];

foreach my $object (getWmiObjects(
class => 'Win32_NetworkAdapter',
properties => [ qw/Index PNPDeviceId Speed MACAddress PhysicalAdapter
AdapterType/ ]
)) {
# http://comments.gmane.org/gmane.comp.monitoring.fusion-inventory.devel/34
next unless $nic->PNPDeviceID;
next unless $object->{PNPDeviceId};

my $interface = $interfaces[$object->{Index}];

$interface->{SPEED} = $nic->Speed;
$interface->{MACADDR} = $nic->MACAddress;
$interface->{PNPDEVICEID} = $nic->PNPDeviceID;
$interface->{SPEED} = $object->{Speed};
$interface->{MACADDR} = $object->{MACAddress};
$interface->{PNPDEVICEID} = $object->{PNPDeviceId};

# PhysicalAdapter only work on OS > XP
if (defined $nic->PhysicalAdapter) {
$interface->{VIRTUALDEV} = $nic->PhysicalAdapter ? 0 : 1;
if (defined $object->{PhysicalAdapter}) {
$interface->{VIRTUALDEV} = $object->{PhysicalAdapter} ? 0 : 1;
# http://forge.fusioninventory.org/issues/1166
} elsif ($interface->{description}
&& $interface->{description} =~ /RAS/
&& $interface->{description} =~ /Adapter/i) {
$interface->{VIRTUALDEV} = 1;
} else {
$interface->{VIRTUALDEV} = $nic->PNPDeviceID =~ /^ROOT/ ? 1 : 0;
$interface->{VIRTUALDEV} = $object->{PNPDeviceId} =~ /^ROOT/ ? 1 : 0;
}

if (defined $nic->AdapterType) {
$interface->{TYPE} = $nic->AdapterType;
if (defined $object->{AdapterType}) {
$interface->{TYPE} = $object->{AdapterType};
$interface->{TYPE} =~ s/Ethernet.*/Ethernet/;
}

}

return @interfaces;
Expand Down

0 comments on commit a850719

Please sign in to comment.