New way to get list of all physical ethernet interfaces

Changes:

* support predictable network interfaces naming based [0]
* better and more reliable way to detect non-virtual, not a wireless ethernet interfaces.
* support old and new Ruby version (1.8.7+)

[0] http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames

Co-Authored-By: Alexei Sheplyakov <asheplyakov@mirantis.com>

Change-Id: Ia61b23beaef38d3d74bf195d58c1e30dac690b7f
Closes-Bug: #1502198
(cherry picked from commit 3e9d17211d)
This commit is contained in:
Vladimir Sharshov (warpc) 2015-11-10 16:40:54 +03:00 committed by Alexey Shtokolov
parent 3837543a80
commit 71361290d2
1 changed files with 11 additions and 5 deletions

16
agent
View File

@ -28,6 +28,8 @@ require 'ipaddr'
require 'rethtool'
require 'digest'
require 'timeout'
# TODO(vsharshov): replace below lines by this string after excluding Ruby 1.8
require 'pathname'
unless Process.euid == 0
puts "You must be root"
@ -268,11 +270,15 @@ class NodeAgent
begin
(@os[:network][:interfaces] or {} rescue {}).each do |int, intinfo|
# Send info about physical interfaces only
next if intinfo[:type] !~ /^eth.*/
# Exception: eth0.0(example) have "type" => "eth" but it is not physical interface
next if int =~ /\d+\.\d+$/ or int =~ /vlan\d+$/
# Remove interfaces like eth0.101-hapr, eth1-hapr
next if int =~ /\d+-.+/
next if intinfo[:encapsulation] !~ /^Ethernet.*/
# Avoid virtual devices like loopback, tunnels, bonding, vlans ...
# TODO(vsharshov): replace below lines by this string after excluding Ruby 1.8
# next if File.realpath("/sys/class/net/#{int}") =~ /virtual/
next if Pathname.new("/sys/class/net/#{int}").realpath.to_s =~ /virtual/
# Avoid wireless
next if File.exist?("/sys/class/net/#{int}/phy80211") ||
File.exist?("/sys/class/net/#{int}/wireless")
int_meta = {:name => int}
int_meta[:state] = intinfo[:state]