File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/MacOS/Networks.pm
Criterion Covered Total %
statement 18 63 28.5
branch 0 28 0.0
condition n/a
subroutine 6 10 60.0
pod 0 2 0.0
total 24 103 23.3


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Inventory::MacOS::Networks;
2              
3 1     1   82833009 use strict;
  1         7  
  1         60  
4 1     1   12 use warnings;
  1         3  
  1         86  
5              
6 1     1   10 use English qw(-no_match_vars);
  1         62  
  1         42  
7              
8 1     1   1593 use FusionInventory::Agent::Tools;
  1         4  
  1         198  
9 1     1   688 use FusionInventory::Agent::Tools::Network;
  1         3  
  1         214  
10 1     1   669 use FusionInventory::Agent::Tools::Unix;
  1         2  
  1         715  
11              
12             sub isEnabled {
13 0     0 0   my (%params) = @_;
14 0 0         return 0 if $params{no_category}->{network};
15 0           return canRun('ifconfig');
16             }
17              
18             sub doInventory {
19 0     0 0   my (%params) = @_;
20              
21 0           my $inventory = $params{inventory};
22 0           my $logger = $params{logger};
23              
24 0           my $routes = getRoutingTable(logger => $logger);
25 0           my $default = $routes->{'0.0.0.0'};
26              
27 0           my @interfaces = _getInterfaces(logger => $logger);
28 0           foreach my $interface (@interfaces) {
29             # if the default gateway address and the interface address belongs to
30             # the same network, that's the gateway for this network
31             $interface->{IPGATEWAY} = $default if isSameNetwork(
32             $default, $interface->{IPADDRESS}, $interface->{IPMASK}
33 0 0         );
34              
35 0           $inventory->addEntry(
36             section => 'NETWORKS',
37             entry => $interface
38             );
39             }
40              
41             $inventory->setHardware({
42 0           DEFAULTGATEWAY => $default
43             });
44             }
45              
46             sub _getInterfaces {
47 0     0     my (%params) = @_;
48              
49             my @interfaces = _parseIfconfig(
50             command => '/sbin/ifconfig -a',
51             logger => $params{logger}
52 0           );
53              
54 0           foreach my $interface (@interfaces) {
55             $interface->{IPSUBNET} = getSubnetAddress(
56             $interface->{IPADDRESS},
57             $interface->{IPMASK}
58 0           );
59             }
60              
61 0           return @interfaces;
62             }
63              
64             sub _parseIfconfig {
65              
66 0     0     my $handle = getFileHandle(@_);
67 0 0         return unless $handle;
68              
69 0           my @interfaces;
70             my $interface;
71              
72 0           while (my $line = <$handle>) {
73 0 0         if ($line =~ /^(\S+):/) {
74             # new interface
75 0 0         push @interfaces, $interface if $interface;
76 0           $interface = {
77             STATUS => 'Down',
78             DESCRIPTION => $1,
79             VIRTUALDEV => 1
80             };
81             }
82              
83 0 0         if ($line =~ /inet ($ip_address_pattern)/) {
84 0           $interface->{IPADDRESS} = $1;
85             }
86 0 0         if ($line =~ /inet6 (\S+)/) {
87 0           $interface->{IPADDRESS6} = $1;
88             # Drop the interface from the address. e.g:
89             # fe80::1%lo0
90             # fe80::214:51ff:fe1a:c8e2%fw0
91 0           $interface->{IPADDRESS6} =~ s/%.*$//;
92             }
93 0 0         if ($line =~ /netmask 0x($hex_ip_address_pattern)/) {
94 0           $interface->{IPMASK} = hex2canonical($1);
95             }
96 0 0         if ($line =~ /(?:address:|ether|lladdr) ($mac_address_pattern)/) {
97 0           $interface->{MACADDR} = $1;
98             }
99 0 0         if ($line =~ /mtu (\S+)/) {
100 0           $interface->{MTU} = $1;
101             }
102 0 0         if ($line =~ /media (\S+)/) {
103 0           $interface->{TYPE} = $1;
104             }
105 0 0         if ($line =~ /status:\s+active/i) {
106 0           $interface->{STATUS} = 'Up';
107             }
108 0 0         if ($line =~ /supported\smedia:/) {
109 0           $interface->{VIRTUALDEV} = 0;
110             }
111             }
112 0           close $handle;
113              
114             # last interface
115 0 0         push @interfaces, $interface if $interface;
116              
117 0           return @interfaces;
118             }
119              
120             1;