FusionInventory-Agent
view release on metacpan or search on metacpan
lib/FusionInventory/Agent/Tools/Linux.pm view on Meta::CPAN
my ($data) = ($smartctl{$s} =~ ($regexp->{$s} // $regexp->{__default}));
$info->{$attr} = exists $val->{func} ? &{$val->{func}}($data) : $data;
last;
}
}
return $info;
}
sub getInterfacesFromIfconfig {
my (%params) = (
command => '/sbin/ifconfig -a',
@_
);
my $handle = getFileHandle(%params);
return unless $handle;
my @interfaces;
my $interface;
my %types = (
Ethernet => 'ethernet',
);
while (my $line = <$handle>) {
if ($line =~ /^$/) {
# end of interface section
push @interfaces, $interface if $interface;
next;
}
if ($line =~ /^([\w\d.]+)/) {
# new interface
$interface = {
STATUS => 'Down',
DESCRIPTION => $1
}
}
if ($line =~ /
inet \s ($ip_address_pattern) \s+
netmask \s ($ip_address_pattern) \s+
broadcast \s $ip_address_pattern
/x) {
$interface->{IPADDRESS} = $1;
$interface->{IPMASK} = $2;
}
if ($line =~ /
ether \s ($mac_address_pattern)
.+
\( Ethernet \)
/x) {
$interface->{MACADDR} = $1;
$interface->{TYPE} = 'ethernet';
}
if ($line =~ /inet6 \s (\S+)/x) {
$interface->{IPADDRESS6} = $1;
}
if ($line =~ /inet addr:($ip_address_pattern)/i) {
$interface->{IPADDRESS} = $1;
}
if ($line =~ /Mask:($ip_address_pattern)/) {
$interface->{IPMASK} = $1;
}
if ($line =~ /inet6 addr: (\S+)/i) {
$interface->{IPADDRESS6} = $1;
}
if ($line =~ /hwadd?r\s+($mac_address_pattern)/i) {
$interface->{MACADDR} = $1;
}
if ($line =~ /^\s+UP\s/) {
$interface->{STATUS} = 'Up';
}
if ($line =~ /flags=.*[<,]UP[>,]/) {
$interface->{STATUS} = 'Up';
}
if ($line =~ /Link encap:(\S+)/) {
$interface->{TYPE} = $types{$1};
}
}
close $handle;
return @interfaces;
}
sub getInterfacesInfosFromIoctl {
my (%params) = (
interface => 'eth0',
@_
);
return unless $params{interface};
my $logger = $params{logger};
socket(my $socket, PF_INET, SOCK_DGRAM, 0)
or return ;
# Pack command in ethtool_cmd struct
my $cmd = pack("L3SC6L2SC2L3", ETHTOOL_GSET);
# Pack request for ioctl
my $request = pack("a16p", $params{interface}, $cmd);
my $retval = ioctl($socket, SIOCETHTOOL, $request) || -1;
return if ($retval < 0);
# Unpack returned datas
my @datas = unpack("L3SC6L2SC2L3", $cmd);
# Actually only speed value is requested and extracted
my $datas = {
SPEED => $datas[3]|$datas[12]<<16
};
# Forget speed value if got unknown speed special value
if ($datas->{SPEED} == SPEED_UNKNOWN) {
delete $datas->{SPEED};
$logger->debug2("Unknown speed found on $params{interface}")
if $logger;
}
return $datas;
}
sub getInterfacesFromIp {
my (%params) = (
command => '/sbin/ip addr show',
@_
);
my $handle = getFileHandle(%params);
return unless $handle;
my (@interfaces, @addresses, $interface);
while (my $line = <$handle>) {
if ($line =~ /^\d+:\s+(\S+): <([^>]+)>/) {
if (@addresses) {
push @interfaces, @addresses;
undef @addresses;
} elsif ($interface) {
push @interfaces, $interface;
}
my ($name, $flags) = ($1, $2);
my $status =
(any { $_ eq 'UP' } split(/,/, $flags)) ? 'Up' : 'Down';
$interface = {
DESCRIPTION => $name,
STATUS => $status
};
} elsif ($line =~ /link\/\S+ ($any_mac_address_pattern)?/) {
$interface->{MACADDR} = $1;
} elsif ($line =~ /inet6 (\S+)\/(\d{1,2})/) {
my $address = $1;
my $mask = getNetworkMaskIPv6($2);
my $subnet = getSubnetAddressIPv6($address, $mask);
push @addresses, {
IPADDRESS6 => $address,
IPMASK6 => $mask,
IPSUBNET6 => $subnet,
STATUS => $interface->{STATUS},
DESCRIPTION => $interface->{DESCRIPTION},
MACADDR => $interface->{MACADDR}
};
} elsif ($line =~ /
inet \s
($ip_address_pattern)(?:\/(\d{1,3}))? \s
.* \s
(\S+)$
/x) {
my $address = $1;
my $mask = getNetworkMask($2);
my $subnet = getSubnetAddress($address, $mask);
my $name = $3;
# the name associated with the address differs from the current
# interface if the address is actually attached to an alias
push @addresses, {
IPADDRESS => $address,
IPMASK => $mask,
IPSUBNET => $subnet,
STATUS => $interface->{STATUS},
DESCRIPTION => $name,
MACADDR => $interface->{MACADDR}
};
}
}
close $handle;
if (@addresses) {
push @interfaces, @addresses;
undef @addresses;
} elsif ($interface) {
push @interfaces, $interface;
}
return @interfaces;
}
1;
__END__
=head1 NAME
FusionInventory::Agent::Tools::Linux - Linux generic functions
=head1 DESCRIPTION
This module provides some generic functions for Linux.
=head1 FUNCTIONS
( run in 2.254 seconds using v1.01-cache-2.11-cpan-d7f47b0818f )