summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-11-17 16:21:23 -0800
committerTim Smith <tsmith84@gmail.com>2020-11-17 16:21:23 -0800
commit0f9e809a579748bea07d504abdb578099f884e9e (patch)
tree22cd9c6649df36f2b821f8c01df9c64dc7d5f736
parent4fe1390dc3b1d4b91398cb95d8550228b7c8dfcc (diff)
downloadohai-0f9e809a579748bea07d504abdb578099f884e9e.tar.gz
Use pure ruby vs. shelling out to grep to parse netstat data
Besides avoiding potentially pathing issues with grep this is faster: ``` Comparison: pure_ruby: 55.9 i/s grep: 41.6 i/s - 1.34x (± 0.00) slower ``` Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/plugins/aix/network.rb14
-rw-r--r--spec/unit/plugins/aix/network_spec.rb14
2 files changed, 20 insertions, 8 deletions
diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb
index 02d064e7..f36c3dd8 100644
--- a/lib/ohai/plugins/aix/network.rb
+++ b/lib/ohai/plugins/aix/network.rb
@@ -44,16 +44,16 @@ Ohai.plugin(:Network) do
# We unfortunately have to do things a bit different here, if ohai is running
# within a WPAR. For instance, the WPAR isn't aware of some of its own networking
- # minutia such as default gateway/route.
+ # minutia such as default gateway/route. lpars return 0 here. wpars return > 0
unless shell_out("uname -W").stdout.to_i > 0
# :default_interface, :default_gateway - route -n get 0
- so = shell_out("netstat -rn |grep default")
- so.stdout.lines.each do |line|
+ netstat_so = shell_out("netstat -rn").stdout
+ netstat_so.each_line do |line|
+ next unless line.start_with?('default')
items = line.split(" ")
- if items[0] == "default"
- network[:default_gateway] = items[1]
- network[:default_interface] = items[5]
- end
+ network[:default_gateway] = items[1]
+ network[:default_interface] = items[5]
+ return
end
end
diff --git a/spec/unit/plugins/aix/network_spec.rb b/spec/unit/plugins/aix/network_spec.rb
index 85c4c73f..dcc94a90 100644
--- a/spec/unit/plugins/aix/network_spec.rb
+++ b/spec/unit/plugins/aix/network_spec.rb
@@ -21,7 +21,19 @@ require "spec_helper"
describe Ohai::System, "AIX network plugin" do
before do
@netstat_rn_grep_default = <<~NETSTAT_RN_GREP_DEFAULT
- default 172.31.8.1 UG 2 121789 en0 - -
+ Destination Gateway Flags Refcnt Use Interface
+ Destination Gateway Flags Refs Use If Exp Groups
+ Only the root user can specify the Z flag
+ (Internet):
+ default 172.31.0.1 UG 2 1652046 en0 - - =>
+ 127/8 127.0.0.1 U 5 2455591 lo0 - - =>
+ 172.31.0.0 172.31.10.23 UHSb 0 0 en0 - - =>
+ 172.31/20 172.31.10.23 U 1 1015674 en0 - - =>
+ 172.31.10.23 127.0.0.1 UGHS 0 1 lo0 - -
+ 172.31.15.255 172.31.10.23 UHSb 0 1 en0 - - =>
+ Only the root user can specify the Z flag
+ (Internet v6):
+ ::1%1 ::1%1 UH 1 677032 lo0 - - =>
NETSTAT_RN_GREP_DEFAULT
@ifconfig = <<~IFCONFIG