summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/ohai/plugins/linux/network.rb13
-rw-r--r--spec/unit/plugins/linux/network_spec.rb36
2 files changed, 48 insertions, 1 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb
index 4a5ddb3c..cbecae86 100644
--- a/lib/ohai/plugins/linux/network.rb
+++ b/lib/ohai/plugins/linux/network.rb
@@ -103,9 +103,20 @@ Ohai.plugin(:Network) do
next
end
route_endings.each do |route_ending|
+ route_int = nil
if route_ending =~ /\bdev\s+([^\s]+)\b/
route_int = $1
- else
+ end
+ # does any known interface own the src address?
+ # we want to override the interface set via nexthop but only if possible
+ if line =~ /\bsrc\s+([^\s]+)\b/ && (!route_int || line.include?("nexthop"))
+ # only clobber previously set route_int if we find a match
+ if (match = iface.select { |name, intf| intf.fetch("addresses", {}).any? { |addr, _| addr == $1 } }.keys.first)
+ route_int = match
+ end
+ end
+
+ unless route_int
logger.trace("Plugin Network: Skipping route entry without a device: '#{line}'")
next
end
diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb
index 48f84ffc..0bb20597 100644
--- a/spec/unit/plugins/linux/network_spec.rb
+++ b/spec/unit/plugins/linux/network_spec.rb
@@ -1335,6 +1335,42 @@ describe Ohai::System, "Linux Network Plugin" do
end
end
+ describe "when there's a source field in a local route entry but no dev" do
+ let(:linux_ip_route) do
+ <<~EOM
+ default proto bird src 172.16.19.39 metric 10 \\ nexthop via 10.1.81.1 dev eth0 weight 20
+ default via 10.116.201.1 dev eth0 metric 10 src 10.116.201.1
+ 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+ 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+ default via 10.116.201.254 dev eth0 metric 20 src 10.116.201.74
+ EOM
+ end
+ let(:linux_ip_route_inet6) { "" }
+
+ it "maps the no-dev route and sets ipaddress" do
+ plugin.run
+ expect(plugin["ipaddress"]).to eq("172.16.19.39")
+ end
+ end
+
+ describe "when there's a source field in a local route entry but no dev with a higher priority default route" do
+ let(:linux_ip_route) do
+ <<~EOM
+ default proto bird src 172.16.19.39 metric 10 \\ nexthop via 10.1.81.1 dev eth0 weight 20
+ default via 10.116.201.1 dev eth0 metric 10 src 10.116.201.1
+ 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+ 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+ default via 10.116.201.254 dev eth0 metric 5 src 10.116.201.74
+ EOM
+ end
+ let(:linux_ip_route_inet6) { "" }
+
+ it "maps the no-dev route and sets ipaddress" do
+ plugin.run
+ expect(plugin["ipaddress"]).to eq("10.116.201.74")
+ end
+ end
+
describe "with a link level default route" do
let(:linux_ip_route) do
<<~EOM