line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Inventory::Linux::Networks; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
30639144
|
use strict; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
90
|
|
4
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
84
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
401
|
use FusionInventory::Agent::Tools; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
219
|
|
7
|
1
|
|
|
1
|
|
487
|
use FusionInventory::Agent::Tools::Network; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
220
|
|
8
|
1
|
|
|
1
|
|
561
|
use FusionInventory::Agent::Tools::Unix; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
77
|
|
9
|
1
|
|
|
1
|
|
502
|
use FusionInventory::Agent::Tools::Linux; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1378
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub isEnabled { |
12
|
0
|
|
|
0
|
0
|
|
my (%params) = @_; |
13
|
0
|
0
|
|
|
|
|
return 0 if $params{no_category}->{network}; |
14
|
0
|
|
|
|
|
|
return 1; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub doInventory { |
18
|
0
|
|
|
0
|
0
|
|
my (%params) = @_; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $inventory = $params{inventory}; |
21
|
0
|
|
|
|
|
|
my $logger = $params{logger}; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $routes = getRoutingTable(command => 'netstat -nr', logger => $logger); |
24
|
0
|
|
|
|
|
|
my $default = $routes->{'0.0.0.0'}; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my @interfaces = _getInterfaces(logger => $logger); |
27
|
0
|
|
|
|
|
|
foreach my $interface (@interfaces) { |
28
|
|
|
|
|
|
|
# if the default gateway address and the interface address belongs to |
29
|
|
|
|
|
|
|
# the same network, that's the gateway for this network |
30
|
0
|
0
|
|
|
|
|
$interface->{IPGATEWAY} = $default if isSameNetwork( |
31
|
|
|
|
|
|
|
$default, $interface->{IPADDRESS}, $interface->{IPMASK} |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$inventory->addEntry( |
35
|
|
|
|
|
|
|
section => 'NETWORKS', |
36
|
|
|
|
|
|
|
entry => $interface |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$inventory->setHardware({ |
41
|
0
|
|
|
|
|
|
DEFAULTGATEWAY => $default |
42
|
|
|
|
|
|
|
}); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _getInterfaces { |
46
|
0
|
|
|
0
|
|
|
my (%params) = @_; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $logger = $params{logger}; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my @interfaces = _getInterfacesBase(logger => $logger); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
foreach my $interface (@interfaces) { |
53
|
0
|
|
|
|
|
|
$interface->{IPSUBNET} = getSubnetAddress( |
54
|
|
|
|
|
|
|
$interface->{IPADDRESS}, |
55
|
|
|
|
|
|
|
$interface->{IPMASK} |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
$interface->{IPDHCP} = getIpDhcp( |
59
|
|
|
|
|
|
|
$logger, |
60
|
|
|
|
|
|
|
$interface->{DESCRIPTION} |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# check if it is a physical interface |
64
|
0
|
0
|
|
|
|
|
if (-d "/sys/class/net/$interface->{DESCRIPTION}/device") { |
65
|
0
|
|
|
|
|
|
my $info = _getUevent($interface->{DESCRIPTION}); |
66
|
0
|
0
|
|
|
|
|
$interface->{DRIVER} = $info->{DRIVER} |
67
|
|
|
|
|
|
|
if $info->{DRIVER}; |
68
|
0
|
0
|
|
|
|
|
$interface->{PCISLOT} = $info->{PCI_SLOT_NAME} |
69
|
|
|
|
|
|
|
if $info->{PCI_SLOT_NAME}; |
70
|
0
|
0
|
0
|
|
|
|
$interface->{PCIID} = |
71
|
|
|
|
|
|
|
$info->{PCI_ID} . ':' . $info->{PCI_SUBSYS_ID} |
72
|
|
|
|
|
|
|
if $info->{PCI_SUBSYS_ID} && $info->{PCI_ID}; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$interface->{VIRTUALDEV} = 0; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# check if it is a wifi interface, otherwise assume ethernet |
77
|
0
|
0
|
|
|
|
|
if (-d "/sys/class/net/$interface->{DESCRIPTION}/wireless") { |
|
|
0
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'wifi'; |
79
|
0
|
|
|
|
|
|
my $info = _parseIwconfig(name => $interface->{DESCRIPTION}); |
80
|
0
|
|
|
|
|
|
$interface->{WIFI_MODE} = $info->{mode}; |
81
|
0
|
|
|
|
|
|
$interface->{WIFI_SSID} = $info->{SSID}; |
82
|
0
|
|
|
|
|
|
$interface->{WIFI_BSSID} = $info->{BSSID}; |
83
|
0
|
|
|
|
|
|
$interface->{WIFI_VERSION} = $info->{version}; |
84
|
|
|
|
|
|
|
} elsif (-f "/sys/class/net/$interface->{DESCRIPTION}/mode") { |
85
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'infiniband'; |
86
|
|
|
|
|
|
|
} else { |
87
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'ethernet'; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} else { |
91
|
0
|
|
|
|
|
|
$interface->{VIRTUALDEV} = 1; |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
if ($interface->{DESCRIPTION} eq 'lo') { |
94
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'loopback'; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if ($interface->{DESCRIPTION} =~ m/^ppp/) { |
98
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'dialup'; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# check if it is an alias or a tagged interface |
102
|
0
|
0
|
|
|
|
|
if ($interface->{DESCRIPTION} =~ m/^([\w\d]+)[:.]\d+$/) { |
103
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'alias'; |
104
|
0
|
|
|
|
|
|
$interface->{BASE} = $1; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
# check if is is a bridge |
107
|
0
|
0
|
|
|
|
|
if (-d "/sys/class/net/$interface->{DESCRIPTION}/brif") { |
108
|
0
|
|
|
|
|
|
$interface->{SLAVES} = _getSlaves($interface->{DESCRIPTION}); |
109
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'bridge'; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# check if it is a bonding master |
113
|
0
|
0
|
|
|
|
|
if (-d "/sys/class/net/$interface->{DESCRIPTION}/bonding") { |
114
|
0
|
|
|
|
|
|
$interface->{SLAVES} = _getSlaves($interface->{DESCRIPTION}); |
115
|
0
|
|
|
|
|
|
$interface->{TYPE} = 'aggregate'; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# check if it is a bonding slave |
120
|
0
|
0
|
|
|
|
|
if (-d "/sys/class/net/$interface->{DESCRIPTION}/bonding_slave") { |
121
|
0
|
|
|
|
|
|
$interface->{MACADDR} = getFirstMatch( |
122
|
|
|
|
|
|
|
command => "ethtool -P $interface->{DESCRIPTION}", |
123
|
|
|
|
|
|
|
pattern => qr/^Permanent address: ($mac_address_pattern)$/, |
124
|
|
|
|
|
|
|
logger => $logger |
125
|
|
|
|
|
|
|
); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if (-r "/sys/class/net/$interface->{DESCRIPTION}/speed") { |
129
|
0
|
|
|
|
|
|
$interface->{SPEED} = getFirstLine( |
130
|
|
|
|
|
|
|
file => "/sys/class/net/$interface->{DESCRIPTION}/speed" |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
return @interfaces; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub _getInterfacesBase { |
139
|
0
|
|
|
0
|
|
|
my (%params) = @_; |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my $logger = $params{logger}; |
142
|
0
|
|
|
|
|
|
$logger->debug("retrieving interfaces list:"); |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
if (canRun('/sbin/ip')) { |
145
|
0
|
|
|
|
|
|
my @interfaces = getInterfacesFromIp(logger => $logger); |
146
|
0
|
|
|
|
|
|
$logger->debug_result( |
147
|
|
|
|
|
|
|
action => 'running /sbin/ip command', |
148
|
|
|
|
|
|
|
data => scalar @interfaces |
149
|
|
|
|
|
|
|
); |
150
|
0
|
0
|
|
|
|
|
return @interfaces if @interfaces; |
151
|
|
|
|
|
|
|
} else { |
152
|
0
|
|
|
|
|
|
$logger->debug_result( |
153
|
|
|
|
|
|
|
action => 'running /sbin/ip command', |
154
|
|
|
|
|
|
|
status => 'command not available' |
155
|
|
|
|
|
|
|
); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
0
|
0
|
|
|
|
|
if (canRun('/sbin/ifconfig')) { |
159
|
0
|
|
|
|
|
|
my @interfaces = getInterfacesFromIfconfig(logger => $logger); |
160
|
0
|
|
|
|
|
|
$logger->debug_result( |
161
|
|
|
|
|
|
|
action => 'running /sbin/ifconfig command', |
162
|
|
|
|
|
|
|
data => scalar @interfaces |
163
|
|
|
|
|
|
|
); |
164
|
0
|
0
|
|
|
|
|
return @interfaces if @interfaces; |
165
|
|
|
|
|
|
|
} else { |
166
|
0
|
|
|
|
|
|
$logger->debug_result( |
167
|
|
|
|
|
|
|
action => 'running /sbin/ifconfig command', |
168
|
|
|
|
|
|
|
status => 'command not available' |
169
|
|
|
|
|
|
|
); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
return; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub _getSlaves { |
176
|
0
|
|
|
0
|
|
|
my ($name) = @_; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my @slaves = |
179
|
0
|
|
|
|
|
|
map { $_ =~ /\/lower_(\w+)$/ } |
180
|
|
|
|
|
|
|
glob("/sys/class/net/$name/lower_*"); |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
return join (",", @slaves); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub _getUevent { |
186
|
0
|
|
|
0
|
|
|
my ($name) = @_; |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
my $file = "/sys/class/net/$name/device/uevent"; |
189
|
0
|
|
|
|
|
|
my $handle = getFileHandle(file => $file); |
190
|
0
|
0
|
|
|
|
|
return unless $handle; |
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
my $info; |
193
|
0
|
|
|
|
|
|
while (my $line = <$handle>) { |
194
|
0
|
0
|
|
|
|
|
next unless $line =~ /^(\w+)=(\S+)$/; |
195
|
0
|
|
|
|
|
|
$info->{$1} = $2; |
196
|
|
|
|
|
|
|
} |
197
|
0
|
|
|
|
|
|
close $handle; |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
return $info; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub _parseIwconfig { |
203
|
0
|
|
|
0
|
|
|
my (%params) = @_; |
204
|
|
|
|
|
|
|
|
205
|
0
|
0
|
|
|
|
|
my $handle = getFileHandle( |
206
|
|
|
|
|
|
|
%params, |
207
|
|
|
|
|
|
|
command => $params{name} ? "iwconfig $params{name}" : undef |
208
|
|
|
|
|
|
|
); |
209
|
0
|
0
|
|
|
|
|
return unless $handle; |
210
|
|
|
|
|
|
|
|
211
|
0
|
|
|
|
|
|
my $info; |
212
|
0
|
|
|
|
|
|
while (my $line = <$handle>) { |
213
|
0
|
0
|
|
|
|
|
$info->{version} = $1 |
214
|
|
|
|
|
|
|
if $line =~ /IEEE (\S+)/; |
215
|
0
|
0
|
|
|
|
|
$info->{SSID} = $1 |
216
|
|
|
|
|
|
|
if $line =~ /ESSID:"([^"]+)"/; |
217
|
0
|
0
|
|
|
|
|
$info->{mode} = $1 |
218
|
|
|
|
|
|
|
if $line =~ /Mode:(\S+)/; |
219
|
0
|
0
|
|
|
|
|
$info->{BSSID} = $1 |
220
|
|
|
|
|
|
|
if $line =~ /Access Point: ($mac_address_pattern)/; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
0
|
|
|
|
|
|
close $handle; |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
return $info; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
1; |