diff options
| author | Petter Reinholdtsen <pere@hungry.com> | 2016-11-26 18:03:24 +0100 |
|---|---|---|
| committer | Petter Reinholdtsen <pere@hungry.com> | 2016-11-26 18:03:24 +0100 |
| commit | 73b95020cb796d845e81487bbd83ec582e9ea9e1 (patch) | |
| tree | 029fb57d28af5a5204bb769fc3eefb81dc306d1f /SiteSummary.pm | |
| parent | c4d9af4af5739cd280a1817c6aa7ab5d37e8266b (diff) | |
| download | sitesummary-73b95020cb796d845e81487bbd83ec582e9ea9e1.tar.gz sitesummary-73b95020cb796d845e81487bbd83ec582e9ea9e1.tar.bz2 sitesummary-73b95020cb796d845e81487bbd83ec582e9ea9e1.tar.xz | |
Adjust parser for collected information to handle the new ifconfig output format (Closes: #832342).
Diffstat (limited to 'SiteSummary.pm')
| -rw-r--r-- | SiteSummary.pm | 90 |
1 files changed, 50 insertions, 40 deletions
diff --git a/SiteSummary.pm b/SiteSummary.pm index e9f2a93..3d13334 100644 --- a/SiteSummary.pm +++ b/SiteSummary.pm @@ -123,12 +123,13 @@ sub get_hostclass { sub get_primary_ip_address { my $hostid = shift; my $path = get_filepath_current($hostid, "/system/ifconfig-a"); - # XXX Not properly implemented, just pick the first IP + # XXX Not properly implemented, just pick the first non-local IP my $ip; if (open (FILE, $path)) { while(<FILE>) { chomp; - if (m/inet addr:(\S+)\s+/) { + if ((m/inet addr:(\S+)\s+/ || m/\s*inet\s+(\S+)\s+/) + && "127.0.0.1" ne $1) { $ip = $1; last; } @@ -142,22 +143,47 @@ sub get_primary_ip_address { # # Return all MAC addresses -sub get_macaddresses { - my $hostid = shift; - my $path = get_filepath_current($hostid, "/system/ifconfig-a"); - if (open (FILE, $path)) { - my @macs; - while(<FILE>) { - chomp; - if (m/Link encap:Ethernet\s+HWaddr (\S+)\s+/) { - push(@macs, $1); +sub get_macaddresses_from_ifconfig { + my $ifconfigoutput = shift; + my %macs; + open(IFCONFIG, $ifconfigoutput) || return (); + my $line = ""; + while (<IFCONFIG>) { + chomp; + if (m/^(\w+)\s+Link encap:Ethernet HWaddr (\S+)/) { + # Old ifconfig format + $macs{$1} = $2; + while (<IFCONFIG>) { + chomp; + last if (/^\s*$/); + } + } elsif (m/flags=/) { + # New ifconfig format + my $line = $_; + while (<IFCONFIG>) { + chomp; + $line .= $_; + last if (/^\s*$/); + } + if ($line =~ m/^(\S+): .+\sether\s+(\S+)\s/) { + $macs{$1} = $2; } } - close(FILE); - return @macs; - } else { - return undef; } + close (IFCONFIG); + my $if = (sort keys %macs)[0]; + my $mac = $macs{$if}; + return lc("$mac"); + + return undef; +} + +# +# Return all MAC addresses +sub get_macaddresses { + my $hostid = shift; + my $path = get_filepath_current($hostid, "/system/ifconfig-a"); + return get_macaddresses_from_ifconfig($path); } # Return current default route used on host @@ -177,22 +203,13 @@ sub get_default_route { } # -# Return the IP address on the primary network interface +# Return the MAC address on the primary network interface # sub get_primary_macaddress { my $hostid = shift; my $path = get_filepath_current($hostid, "/system/ifconfig-a"); - # XXX Not properly implemented, just pick the first MAC after - # sorting alphabetically. - if (open (FILE, $path)) { - my @macs; - while(<FILE>) { - chomp; - if (m/Link encap:Ethernet\s+HWaddr (\S+)\s+/) { - push(@macs, $1); - } - } - close(FILE); + my @macs = get_macaddresses_from_ifconfig($path); + if (@macs) { return (sort @macs)[0]; } else { return undef; @@ -207,20 +224,13 @@ sub get_primary_macaddress { # sub get_unique_ether_id { my $ifconfigoutput = shift; - my $eth0mac; - my $eth1mac; - my $eth2mac; - open(IFCONFIG, $ifconfigoutput) || return undef; - while (<IFCONFIG>) { - chomp; - $eth0mac = $1 if (m/^eth0\s+Link encap:Ethernet HWaddr (\S+)/); - $eth1mac = $1 if (m/^eth1\s+Link encap:Ethernet HWaddr (\S+)/); - $eth2mac = $1 if (m/^eth2\s+Link encap:Ethernet HWaddr (\S+)/); + my @macs = get_macaddresses_from_ifconfig($ifconfigoutput); + if (@macs) { + my $mac = (sort @macs)[0]; + return lc("ether-$mac"); + } else { + return undef; } - close (IFCONFIG); - #print STDERR "MAC: $eth0mac\n"; - my $mac = $eth0mac || $eth1mac || $eth2mac || "unknown"; - return lc("ether-$mac"); } # |
