-
Notifications
You must be signed in to change notification settings - Fork 125
/
Networks.pm
237 lines (194 loc) · 5.68 KB
/
Networks.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package FusionInventory::Agent::Task::Inventory::Input::HPUX::Networks;
use strict;
use warnings;
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Unix;
use FusionInventory::Agent::Tools::Network;
#TODO Get pcislot virtualdev
sub isEnabled {
return canRun('lanscan');
}
sub doInventory {
my (%params) = @_;
my $inventory = $params{inventory};
my $logger = $params{logger};
# set list of network interfaces
my $routes = getRoutingTable(command => 'netstat -nr', logger => $logger);
my @interfaces = _getInterfaces(logger => $logger);
foreach my $interface (@interfaces) {
$interface->{IPGATEWAY} = $params{routes}->{$interface->{IPSUBNET}}
if $interface->{IPSUBNET};
$inventory->addEntry(
section => 'NETWORKS',
entry => $interface
);
}
$inventory->setHardware({
DEFAULTGATEWAY => $routes->{'0.0.0.0'}
});
}
sub _getInterfaces {
my (%params) = @_;
my @ifLanScan = _parseLanscan(
command => 'lanscan -iap',
logger => $params{logger}
);
my %ifStatNrv = _parseNetstatNrv();
my @interfaces;
foreach my $ifLanScan (@ifLanScan) {
my $lanadminInfo = _getLanadminInfo(
command => "lanadmin -g $ifLanScan->{lan_id}",
logger => $params{logger}
);
$ifLanScan->{TYPE} = $lanadminInfo->{'Type (value)'};
$ifLanScan->{SPEED} = $lanadminInfo->{Speed} > 1000000 ?
$lanadminInfo->{Speed} / 1000000 :
$lanadminInfo->{Speed};
# Interface found in "netstat -nrv", let's use it
if ($ifStatNrv{$ifLanScan->{DESCRIPTION}}) {
foreach my $ifStatNrv (@{$ifStatNrv{$ifLanScan->{DESCRIPTION}}}) {
foreach my $key (keys %$ifLanScan) {
next unless $ifLanScan->{$key};
$ifStatNrv->{$key} = $ifLanScan->{$key};
}
push @interfaces, $ifStatNrv;
}
# O
} else {
my $ifconfigInfo = _getIfconfigInfo(
command => "ifconfig $ifLanScan->{DESCRIPTION}",
logger => $params{logger}
);
$ifLanScan->{STATUS} = $ifconfigInfo->{status};
$ifLanScan->{IPADDRESS} = $ifconfigInfo->{address};
$ifLanScan->{IPMASK} = $ifconfigInfo->{netmask};
push @interfaces, $ifLanScan;
}
}
foreach my $interface (@interfaces) {
if ($interface->{IPADDRESS} && $interface->{IPADDRESS} eq '0.0.0.0') {
$interface->{IPADDRESS} = undef;
$interface->{IPMASK} = undef;
} else {
$interface->{IPSUBNET} = getSubnetAddress(
$interface->{IPADDRESS},
$interface->{IPMASK}
);
}
}
return @interfaces;
}
sub _parseLanscan {
my (%params) = @_;
my $handle = getFileHandle(%params);
return unless $handle;
my @interfaces;
while (my $line = <$handle>) {
next unless $line =~ /^0x($alt_mac_address_pattern)\s(\S+)\s(\S+)\s+(\S+)/;
my $interface = {
MACADDR => alt2canonical($1),
STATUS => 'Down',
DESCRIPTION => $2,
lan_id => $4,
};
push @interfaces, $interface;
}
close $handle;
return @interfaces;
}
sub _getLanadminInfo {
my $handle = getFileHandle(@_);
return unless $handle;
my $info;
while (my $line = <$handle>) {
next unless $line =~ /^(\S.+\S) \s+ = \s (.+)$/x;
$info->{$1} = $2;
}
close $handle;
return $info;
}
sub _getIfconfigInfo {
my $handle = getFileHandle(@_);
return unless $handle;
my $info;
while (my $line = <$handle>) {
if ($line =~ /<UP/) {
$info->{status} = 'Up';
}
if ($line =~ /inet ($ip_address_pattern)/) {
$info->{address} = $1;
}
if ($line =~ /netmask ($hex_ip_address_pattern)/) {
$info->{netmask} = hex2canonical($1);
}
}
close $handle;
return $info;
}
# will be need to get the bonding configuration
sub _getNwmgrInfo {
my $handle = getFileHandle(@_);
return unless $handle;
my $info;
while (my $line = <$handle>) {
if ($line =~ /^(\w+)\s+(\w+)\s+0x(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})\s+(\w+)\s+(\w*)/) {
my $netif = $1;
$info->{$netif} = {
status => $2,
mac => join(':', ($3, $4, $5, $6, $7, $8)),
driver => $9,
media => $10,
related_if => $11
}
}
}
close $handle;
return $info;
}
sub _parseNetstatNrv {
my (%params) = (
command => 'netstat -nrv',
@_
);
my $handle = getFileHandle(%params);
return unless $handle;
my %interfaces;
while (my $line = <$handle>) {
next unless $line =~ /^
(
$ip_address_pattern
)
\/
(
$ip_address_pattern
)
\s+
(
$ip_address_pattern # Gateway
)
\s+
\w*H\w* # Host only
.*\s
(
\w+ # Interface name
)
(|:\d+) # ignore interface alias, e.g: lan0:1
\s+
(
\d+ # MTU
)
$
/x;
my $ipgateway = $3 if $3 ne $1;
push @{$interfaces{$4}}, {
IPADDRESS => $1,
IPMASK => $2,
IPGATEWAY => $ipgateway,
DESCRIPTION => $4,
MTU => $6
}
}
close $handle;
return %interfaces;
}
1;