From 9b686159a352863dc6984a8b8b704f2065a061b0 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 11 Jun 2019 20:52:51 -0700 Subject: Simplify how we create empty mashes in the plugins This is a lot easier to read. Signed-off-by: Tim Smith --- lib/ohai/plugins/aix/network.rb | 12 +++++------ lib/ohai/plugins/bsd/virtualization.rb | 2 +- lib/ohai/plugins/darwin/network.rb | 27 ++++++++++++------------ lib/ohai/plugins/darwin/virtualization.rb | 2 +- lib/ohai/plugins/dmi.rb | 4 ++-- lib/ohai/plugins/dragonflybsd/network.rb | 18 ++++++++-------- lib/ohai/plugins/filesystem.rb | 10 ++++----- lib/ohai/plugins/freebsd/network.rb | 18 ++++++++-------- lib/ohai/plugins/linux/network.rb | 34 +++++++++++++++--------------- lib/ohai/plugins/linux/sessions.rb | 4 ++-- lib/ohai/plugins/linux/virtualization.rb | 2 +- lib/ohai/plugins/netbsd/network.rb | 18 ++++++++-------- lib/ohai/plugins/network.rb | 4 ++-- lib/ohai/plugins/openbsd/network.rb | 18 ++++++++-------- lib/ohai/plugins/solaris2/dmi.rb | 2 +- lib/ohai/plugins/solaris2/network.rb | 14 ++++++------ lib/ohai/plugins/windows/network.rb | 6 +++--- lib/ohai/plugins/windows/virtualization.rb | 2 +- 18 files changed, 99 insertions(+), 98 deletions(-) diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb index 5a1bffa3..4dfb977b 100644 --- a/lib/ohai/plugins/aix/network.rb +++ b/lib/ohai/plugins/aix/network.rb @@ -44,7 +44,7 @@ Ohai.plugin(:Network) do iface = Mash.new network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new # 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 @@ -89,7 +89,7 @@ Ohai.plugin(:Network) do netmask = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s end - iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses] ||= Mash.new iface[interface][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix } iface[interface][:addresses][tmp_addr][:netmask] = netmask @@ -98,7 +98,7 @@ Ohai.plugin(:Network) do end elsif lin =~ /inet6 ([a-f0-9\:]+)%?([\d]*)\/?(\d*)?/ # TODO do we have more properties on inet6 in aix? broadcast - iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses] ||= Mash.new iface[interface][:addresses][$1] = { "family" => "inet6", "zone_index" => $2, "prefixlen" => $3 } else # load all key-values, example "tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1" @@ -113,7 +113,7 @@ Ohai.plugin(:Network) do # Query macaddress e_so = shell_out("entstat -d #{interface} | grep \"Hardware Address\"") - iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses] ||= Mash.new e_so.stdout.lines.each do |l| if l =~ /Hardware Address: (\S+)/ iface[interface][:addresses][$1.upcase] = { "family" => "lladdr" } @@ -139,9 +139,9 @@ Ohai.plugin(:Network) do so = shell_out("arp -an") count = 0 so.stdout.lines.each do |line| - network[:arp] = Mash.new unless network[:arp] + network[:arp] ||= Mash.new if line =~ /\s*(\S+) \((\S+)\) at ([a-fA-F0-9\:]+) \[(\w+)\] stored in bucket/ - network[:arp][count] = Mash.new unless network[:arp][count] + network[:arp][count] ||= Mash.new network[:arp][count][:remote_host] = $1 network[:arp][count][:remote_ip] = $2 network[:arp][count][:remote_mac] = $3.downcase diff --git a/lib/ohai/plugins/bsd/virtualization.rb b/lib/ohai/plugins/bsd/virtualization.rb index 2e2c3a76..f82da617 100644 --- a/lib/ohai/plugins/bsd/virtualization.rb +++ b/lib/ohai/plugins/bsd/virtualization.rb @@ -26,7 +26,7 @@ Ohai.plugin(:Virtualization) do collect_data(:freebsd, :openbsd, :netbsd, :dragonflybsd) do virtualization Mash.new unless virtualization - virtualization[:systems] = Mash.new unless virtualization[:systems] + virtualization[:systems] ||= Mash.new # detect when in a jail or when a jail is actively running (not in stopped state) so = shell_out("sysctl -n security.jail.jailed") diff --git a/lib/ohai/plugins/darwin/network.rb b/lib/ohai/plugins/darwin/network.rb index 9b48e6e3..f77107e2 100644 --- a/lib/ohai/plugins/darwin/network.rb +++ b/lib/ohai/plugins/darwin/network.rb @@ -87,9 +87,9 @@ Ohai.plugin(:Network) do require "scanf" network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new so = shell_out("route -n get default") so.stdout.lines do |line| @@ -109,7 +109,8 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /^([0-9a-zA-Z\.\:\-]+): \S+ mtu (\d+)$/ cint = $1 - iface[cint] = Mash.new unless iface[cint]; iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint] ||= Mash.new + iface[cint][:addresses] ||= Mash.new iface[cint][:mtu] = $2 if line =~ /\sflags\=\d+\<((UP|BROADCAST|DEBUG|SMART|SIMPLEX|LOOPBACK|POINTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC|,)+)\>\s/ flags = $1.split(",") @@ -124,37 +125,37 @@ Ohai.plugin(:Network) do end end if line =~ /^\s+ether ([0-9a-f\:]+)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } iface[cint][:encapsulation] = "Ethernet" end if line =~ /^\s+lladdr ([0-9a-f\:]+)\s/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask 0x(([0-9a-f]){1,8})\s*$/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * "." } end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask 0x(([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * ".", "broadcast" => $4 } end if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*) prefixlen (\d+)\s*/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 , "scope" => scope_lookup($1) } end if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*) prefixlen (\d+) scopeid 0x([a-f0-9]+)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 , "scope" => scope_lookup($1) } end if line =~ /^\s+media: ((\w+)|(\w+ [a-zA-Z0-9\-\<\>]+)) status: (\w+)/ - iface[cint][:media] = Mash.new unless iface[cint][:media] + iface[cint][:media] ||= Mash.new iface[cint][:media][:selected] = parse_media($1) iface[cint][:status] = $4 end if line =~ /^\s+supported media: (.*)/ - iface[cint][:media] = Mash.new unless iface[cint][:media] + iface[cint][:media] ||= Mash.new iface[cint][:media][:supported] = parse_media($1) end end @@ -164,7 +165,7 @@ Ohai.plugin(:Network) do if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([a-zA-Z0-9\.\:\-]+).*\[(\w+)\]/ # MAC addr really should be normalized to include all the zeroes. next if iface[$3].nil? # this should never happen - iface[$3][:arp] = Mash.new unless iface[$3][:arp] + iface[$3][:arp] ||= Mash.new iface[$3][:arp][$1] = $2 end end @@ -188,7 +189,7 @@ Ohai.plugin(:Network) do line =~ /^([a-zA-Z0-9\.\:\-\*]+)\s+\d+\s+\<[a-zA-Z0-9\#]+\>(\s+)(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ ifname = locate_interface(iface, $1, $2) next if iface[ifname].nil? # this shouldn't happen, but just in case - net_counters[ifname] = Mash.new unless net_counters[ifname] + net_counters[ifname] ||= Mash.new net_counters[ifname] = { rx: { bytes: $5, packets: $3, errors: $4, drop: 0, overrun: 0, frame: 0, compressed: 0, multicast: 0 }, tx: { bytes: $8, packets: $6, errors: $7, drop: 0, overrun: 0, collisions: $9, carrier: 0, compressed: 0 }, } diff --git a/lib/ohai/plugins/darwin/virtualization.rb b/lib/ohai/plugins/darwin/virtualization.rb index 2aaa17d5..49611f3a 100644 --- a/lib/ohai/plugins/darwin/virtualization.rb +++ b/lib/ohai/plugins/darwin/virtualization.rb @@ -44,7 +44,7 @@ Ohai.plugin(:Virtualization) do collect_data(:darwin) do virtualization Mash.new unless virtualization - virtualization[:systems] = Mash.new unless virtualization[:systems] + virtualization[:systems] ||= Mash.new if docker_exists? virtualization[:system] = "docker" diff --git a/lib/ohai/plugins/dmi.rb b/lib/ohai/plugins/dmi.rb index 7c3e5f4b..c8a70b13 100644 --- a/lib/ohai/plugins/dmi.rb +++ b/lib/ohai/plugins/dmi.rb @@ -82,8 +82,8 @@ Ohai.plugin(:DMI) do dmi_record = { type: Ohai::Common::DMI.id_lookup(handle[2]) } - dmi[dmi_record[:type]] = Mash.new unless dmi.key?(dmi_record[:type]) - dmi[dmi_record[:type]][:all_records] = [] unless dmi[dmi_record[:type]].key?(:all_records) + dmi[dmi_record[:type]] ||= Mash.new + dmi[dmi_record[:type]][:all_records] ||= [] dmi_record[:position] = dmi[dmi_record[:type]][:all_records].length dmi[dmi_record[:type]][:all_records].push(Mash.new) dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:record_id] = handle[1] diff --git a/lib/ohai/plugins/dragonflybsd/network.rb b/lib/ohai/plugins/dragonflybsd/network.rb index ac1c1765..75abc09d 100644 --- a/lib/ohai/plugins/dragonflybsd/network.rb +++ b/lib/ohai/plugins/dragonflybsd/network.rb @@ -22,9 +22,9 @@ Ohai.plugin(:Network) do collect_data(:dragonflybsd) do network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new so = shell_out("route -n get default") so.stdout.lines do |line| @@ -52,11 +52,11 @@ Ohai.plugin(:Network) do end # call the family lladdr to match linux for consistency if line =~ /\s+ether (.+?)\s/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } end if line =~ /\s+inet ([\d.]+) netmask ([\da-fx]+)\s*\w*\s*([\d.]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new # convert the netmask to decimal for consistency netmask = "#{$2[2, 2].hex}.#{$2[4, 2].hex}.#{$2[6, 2].hex}.#{$2[8, 2].hex}" if $3.empty? @@ -67,7 +67,7 @@ Ohai.plugin(:Network) do end end if line =~ /\s+inet6 ([a-f0-9\:]+)%?(\w*)\s+prefixlen\s+(\d+)\s*\w*\s*([\da-fx]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new if $4.empty? iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $3 } else @@ -89,7 +89,7 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$3] # this should never happen - iface[$3][:arp] = Mash.new unless iface[$3][:arp] + iface[$3][:arp] ||= Mash.new iface[$3][:arp][$1] = $2.downcase end end @@ -107,9 +107,9 @@ Ohai.plugin(:Network) do # ed0 1500 54:52:00:68:92:85 333604 26 151905886 175472 0 24897542 0 905 # $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 if line =~ /^([\w\.\*]+)\s+\d+\s+\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ - net_counters[$1] = Mash.new unless net_counters[$1] - net_counters[$1]["rx"] = Mash.new unless net_counters[$1]["rx"] - net_counters[$1]["tx"] = Mash.new unless net_counters[$1]["tx"] + net_counters[$1] ||= Mash.new + net_counters[$1]["rx"] ||= Mash.new + net_counters[$1]["tx"] ||= Mash.new net_counters[$1]["rx"]["packets"] = $3 net_counters[$1]["rx"]["errors"] = $4 net_counters[$1]["rx"]["bytes"] = $5 diff --git a/lib/ohai/plugins/filesystem.rb b/lib/ohai/plugins/filesystem.rb index 76ab8ed9..82649309 100644 --- a/lib/ohai/plugins/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -65,7 +65,7 @@ Ohai.plugin(:Filesystem) do def generate_device_view(fs) view = {} fs.each_value do |entry| - view[entry[:device]] = Mash.new unless view[entry[:device]] + view[entry[:device]] ||= Mash.new entry.each do |key, val| next if %w{device mount}.include?(key) view[entry[:device]][key] = val @@ -82,7 +82,7 @@ Ohai.plugin(:Filesystem) do view = {} fs.each_value do |entry| next unless entry[:mount] - view[entry[:mount]] = Mash.new unless view[entry[:mount]] + view[entry[:mount]] ||= Mash.new entry.each do |key, val| next if %w{mount device}.include?(key) view[entry[:mount]][key] = val @@ -182,7 +182,7 @@ Ohai.plugin(:Filesystem) do so.stdout.each_line do |line| if line =~ /^(.+?) on (.+?) type (.+?) \((.+?)\)$/ key = "#{$1},#{$2}" - fs[key] = Mash.new unless fs.key?(key) + fs[key] ||= Mash.new fs[key][:device] = $1 fs[key][:mount] = $2 fs[key][:fs_type] = $3 @@ -380,7 +380,7 @@ Ohai.plugin(:Filesystem) do so.stdout.lines do |line| if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ key = "#{$1},#{$2}" - fs[key] = Mash.new unless fs.key?(key) + fs[key] ||= Mash.new fs[key][:mount] = $2 fs[key][:fs_type] = $3 fs[key][:mount_options] = $4.split(/,\s*/) @@ -426,7 +426,7 @@ Ohai.plugin(:Filesystem) do so.stdout.lines do |line| next unless line =~ /^(.+?) on (.+?) (.+?) on (.+?)$/ key = "#{$2},#{$1}" - fs[key] = Mash.new unless fs.key?(key) + fs[key] ||= Mash.new fs[key][:mount] = $1 fs[key][:mount_time] = $4 # $4 must come before "split", else it becomes nil fs[key][:mount_options] = $3.split("/") diff --git a/lib/ohai/plugins/freebsd/network.rb b/lib/ohai/plugins/freebsd/network.rb index fa61ff5c..cff51bc3 100644 --- a/lib/ohai/plugins/freebsd/network.rb +++ b/lib/ohai/plugins/freebsd/network.rb @@ -22,9 +22,9 @@ Ohai.plugin(:Network) do collect_data(:freebsd) do network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new so = shell_out("route -n get default") so.stdout.lines do |line| @@ -52,11 +52,11 @@ Ohai.plugin(:Network) do end # call the family lladdr to match linux for consistency if line =~ /\s+ether (.+?)\s/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } end if line =~ /\s+inet ([\d.]+) netmask ([\da-fx]+)\s*\w*\s*([\d.]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new # convert the netmask to decimal for consistency netmask = "#{$2[2, 2].hex}.#{$2[4, 2].hex}.#{$2[6, 2].hex}.#{$2[8, 2].hex}" if $3.empty? @@ -67,7 +67,7 @@ Ohai.plugin(:Network) do end end if line =~ /\s+inet6 ([a-f0-9\:]+)%?(\w*)\s+prefixlen\s+(\d+)\s*\w*\s*([\da-fx]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new if $4.empty? iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $3 } else @@ -89,7 +89,7 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$3] # this should never happen - iface[$3][:arp] = Mash.new unless iface[$3][:arp] + iface[$3][:arp] ||= Mash.new iface[$3][:arp][$1] = $2.downcase end end @@ -107,9 +107,9 @@ Ohai.plugin(:Network) do # ed0 1500 54:52:00:68:92:85 333604 26 151905886 175472 0 24897542 0 905 # $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 if line =~ /^([\w\.\*]+)\s+\d+\s+\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ - net_counters[$1] = Mash.new unless net_counters[$1] - net_counters[$1]["rx"] = Mash.new unless net_counters[$1]["rx"] - net_counters[$1]["tx"] = Mash.new unless net_counters[$1]["tx"] + net_counters[$1] ||= Mash.new + net_counters[$1]["rx"] ||= Mash.new + net_counters[$1]["tx"] ||= Mash.new net_counters[$1]["rx"]["packets"] = $3 net_counters[$1]["rx"]["errors"] = $4 net_counters[$1]["rx"]["bytes"] = $5 diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 5f0847b4..685b64aa 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -58,7 +58,7 @@ Ohai.plugin(:Network) do logger.warn("neighbor list has entries for unknown interface #{interface}") next end - interface[neigh_attr] = Mash.new unless interface[neigh_attr] + interface[neigh_attr] ||= Mash.new interface[neigh_attr][$1] = $3.downcase end end @@ -208,8 +208,8 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ IPROUTE_INT_REGEX tmp_int = $2 - iface[tmp_int] = Mash.new unless iface[tmp_int] - net_counters[tmp_int] = Mash.new unless net_counters[tmp_int] + iface[tmp_int] ||= Mash.new + net_counters[tmp_int] ||= Mash.new end if line =~ /^\s+(ip6tnl|ipip)/ @@ -239,7 +239,7 @@ Ohai.plugin(:Network) do if line =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ int = on_rx ? :rx : :tx - net_counters[tmp_int][int] = Mash.new unless net_counters[tmp_int][int] + net_counters[tmp_int][int] ||= Mash.new net_counters[tmp_int][int][:bytes] = $1 net_counters[tmp_int][int][:packets] = $2 net_counters[tmp_int][int][:errors] = $3 @@ -255,7 +255,7 @@ Ohai.plugin(:Network) do end if line =~ /qlen (\d+)/ - net_counters[tmp_int][:tx] = Mash.new unless net_counters[tmp_int][:tx] + net_counters[tmp_int][:tx] ||= Mash.new net_counters[tmp_int][:tx][:queuelen] = $1 end @@ -266,7 +266,7 @@ Ohai.plugin(:Network) do else tmp_id = $1 end - iface[tmp_int][:vlan] = Mash.new unless iface[tmp_int][:vlan] + iface[tmp_int][:vlan] ||= Mash.new iface[tmp_int][:vlan][:id] = tmp_id iface[tmp_int][:vlan][:protocol] = tmp_prot if tmp_prot @@ -320,7 +320,7 @@ Ohai.plugin(:Network) do if line =~ /link\/(\w+) ([\da-f\:]+) / iface[cint][:encapsulation] = linux_encaps_lookup($1) unless $2 == "00:00:00:00:00:00" - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$2.upcase] = { "family" => "lladdr" } end end @@ -340,8 +340,8 @@ Ohai.plugin(:Network) do cint = alias_int end - iface[cint] = Mash.new unless iface[cint] # Create the fake alias interface if needed - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint] ||= Mash.new # Create the fake alias interface if needed + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix } iface[cint][:addresses][tmp_addr][:netmask] = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s @@ -365,7 +365,7 @@ Ohai.plugin(:Network) do def parse_ip_addr_inet6_line(cint, iface, line) if line =~ /inet6 ([a-f0-9\:]+)\/(\d+) scope (\w+)( .*)?/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new tmp_addr = $1 tags = $4 || "" tags = tags.split(" ") @@ -476,9 +476,9 @@ Ohai.plugin(:Network) do net_counters = Mash.new network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new # ohai.plugin[:network][:default_route_table] = 'default' if configuration(:default_route_table).nil? || configuration(:default_route_table).empty? @@ -597,16 +597,16 @@ Ohai.plugin(:Network) do iface[cint][:encapsulation] = linux_encaps_lookup($1) end if line =~ /HWaddr (.+?)\s/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } end if line =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet" } tmp_addr = $1 end if line =~ /inet6 addr: ([a-f0-9\:]+)\/(\d+) Scope:(\w+)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("Host") ? "Node" : $3) } end if line =~ /Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ @@ -626,7 +626,7 @@ Ohai.plugin(:Network) do iface[cint][:peer] = $1 end if line =~ /RX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)/ - net_counters[cint] = Mash.new unless net_counters[cint] + net_counters[cint] ||= Mash.new net_counters[cint][:rx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "frame" => $5 } end if line =~ /TX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)/ @@ -650,7 +650,7 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) \[(\w+)\] on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$4] # this should never happen - iface[$4][:arp] = Mash.new unless iface[$4][:arp] + iface[$4][:arp] ||= Mash.new iface[$4][:arp][$1] = $2.downcase end end diff --git a/lib/ohai/plugins/linux/sessions.rb b/lib/ohai/plugins/linux/sessions.rb index d1008325..bc503f85 100644 --- a/lib/ohai/plugins/linux/sessions.rb +++ b/lib/ohai/plugins/linux/sessions.rb @@ -28,8 +28,8 @@ Ohai.plugin(:Sessions) do loginctl = shell_out(cmd) sessions Mash.new unless sessions - sessions[:by_session] = Mash.new unless sessions[:by_session] - sessions[:by_user] = Mash.new unless sessions[:by_user] + sessions[:by_session] ||= Mash.new + sessions[:by_user] ||= Mash.new loginctl.stdout.split("\n").each do |line| session, uid, user, seat = line.split diff --git a/lib/ohai/plugins/linux/virtualization.rb b/lib/ohai/plugins/linux/virtualization.rb index e99997eb..1f6b0064 100644 --- a/lib/ohai/plugins/linux/virtualization.rb +++ b/lib/ohai/plugins/linux/virtualization.rb @@ -36,7 +36,7 @@ Ohai.plugin(:Virtualization) do collect_data(:linux) do virtualization Mash.new unless virtualization - virtualization[:systems] = Mash.new unless virtualization[:systems] + virtualization[:systems] ||= Mash.new # Docker hosts if docker_exists? diff --git a/lib/ohai/plugins/netbsd/network.rb b/lib/ohai/plugins/netbsd/network.rb index d39e679b..368f921f 100644 --- a/lib/ohai/plugins/netbsd/network.rb +++ b/lib/ohai/plugins/netbsd/network.rb @@ -22,9 +22,9 @@ Ohai.plugin(:Network) do collect_data(:netbsd) do network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new so = shell_out("route -n get default") so.stdout.lines do |line| @@ -52,11 +52,11 @@ Ohai.plugin(:Network) do end # call the family lladdr to match linux for consistency if line =~ /\s+address: (.+?)\s/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } end if line =~ /\s+inet ([\d.]+) netmask ([\da-fx]+)\s*\w*\s*([\d.]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new # convert the netmask to decimal for consistency netmask = "#{$2[2, 2].hex}.#{$2[4, 2].hex}.#{$2[6, 2].hex}.#{$2[8, 2].hex}" if $3.empty? @@ -67,7 +67,7 @@ Ohai.plugin(:Network) do end end if line =~ /\s+inet6 ([a-f0-9\:]+)%?(\w*)\s+prefixlen\s+(\d+)\s*\w*\s*([\da-fx]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new if $4.empty? iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $3 } else @@ -89,7 +89,7 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$3] # this should never happen - iface[$3][:arp] = Mash.new unless iface[$3][:arp] + iface[$3][:arp] ||= Mash.new iface[$3][:arp][$1] = $2.downcase end end @@ -107,9 +107,9 @@ Ohai.plugin(:Network) do # em0 1500 00:11:25:2d:90:be 3719557 0 3369969 0 0 0 # $1 $2 $3 $4 $5 $6 $7 $8 if line =~ /^([\w\.\*]+)\s+\d+\s+\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ - net_counters[$1] = Mash.new unless net_counters[$1] - net_counters[$1]["rx"] = Mash.new unless net_counters[$1]["rx"] - net_counters[$1]["tx"] = Mash.new unless net_counters[$1]["tx"] + net_counters[$1] ||= Mash.ne + net_counters[$1]["rx"] ||= Mash.new + net_counters[$1]["tx"] ||= Mash.new net_counters[$1]["rx"]["packets"] = $3 net_counters[$1]["rx"]["errors"] = $4 net_counters[$1]["tx"]["packets"] = $5 diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index 9b9811dc..2f5925e1 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -135,9 +135,9 @@ Ohai.plugin(:NetworkAddresses) do results = {} network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new # inet family is processed before inet6 to give ipv4 precedence Ohai::Mixin::NetworkConstants::FAMILIES.keys.sort.each do |family| diff --git a/lib/ohai/plugins/openbsd/network.rb b/lib/ohai/plugins/openbsd/network.rb index 25ebfcba..9868b2c2 100644 --- a/lib/ohai/plugins/openbsd/network.rb +++ b/lib/ohai/plugins/openbsd/network.rb @@ -22,9 +22,9 @@ Ohai.plugin(:Network) do collect_data(:openbsd) do network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new so = shell_out("route -n get default") so.stdout.lines do |line| @@ -52,11 +52,11 @@ Ohai.plugin(:Network) do end # call the family lladdr to match linux for consistency if line =~ /\s+lladdr (.+?)\s/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "lladdr" } end if line =~ /\s+inet ([\d.]+) netmask ([\da-fx]+)\s*\w*\s*([\d.]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new # convert the netmask to decimal for consistency netmask = "#{$2[2, 2].hex}.#{$2[4, 2].hex}.#{$2[6, 2].hex}.#{$2[8, 2].hex}" if $3.empty? @@ -67,7 +67,7 @@ Ohai.plugin(:Network) do end end if line =~ /\s+inet6 ([a-f0-9\:]+)%?(\w*)\s+prefixlen\s+(\d+)\s*\w*\s*([\da-fx]*)/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new if $4.empty? iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $3 } else @@ -89,7 +89,7 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$3] # this should never happen - iface[$3][:arp] = Mash.new unless iface[$3][:arp] + iface[$3][:arp] ||= Mash.new iface[$3][:arp][$1] = $2.downcase end end @@ -107,9 +107,9 @@ Ohai.plugin(:Network) do # em0 1500 00:11:25:2d:90:be 3719557 0 3369969 0 0 0 # $1 $2 $3 $4 $5 $6 $7 $8 if line =~ /^([\w\.\*]+)\s+\d+\s+\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ - net_counters[$1] = Mash.new unless net_counters[$1] - net_counters[$1]["rx"] = Mash.new unless net_counters[$1]["rx"] - net_counters[$1]["tx"] = Mash.new unless net_counters[$1]["tx"] + net_counters[$1] ||= Mash.new + net_counters[$1]["rx"] ||= Mash.new + net_counters[$1]["tx"] ||= Mash.new net_counters[$1]["rx"]["packets"] = $3 net_counters[$1]["rx"]["errors"] = $4 net_counters[$1]["tx"]["packets"] = $5 diff --git a/lib/ohai/plugins/solaris2/dmi.rb b/lib/ohai/plugins/solaris2/dmi.rb index ecbc6f2d..823d51cf 100644 --- a/lib/ohai/plugins/solaris2/dmi.rb +++ b/lib/ohai/plugins/solaris2/dmi.rb @@ -142,7 +142,7 @@ Ohai.plugin(:DMI) do next end - dmi[dmi_record[:type]] = Mash.new unless dmi.key?(dmi_record[:type]) + dmi[dmi_record[:type]] ||= Mash.new dmi[dmi_record[:type]][:all_records] = [] unless dmi[dmi_record[:type]].key?(:all_records) dmi_record[:position] = dmi[dmi_record[:type]][:all_records].length dmi[dmi_record[:type]][:all_records].push(Mash.new) diff --git a/lib/ohai/plugins/solaris2/network.rb b/lib/ohai/plugins/solaris2/network.rb index d93da9d6..7e7ffc5e 100644 --- a/lib/ohai/plugins/solaris2/network.rb +++ b/lib/ohai/plugins/solaris2/network.rb @@ -95,9 +95,9 @@ Ohai.plugin(:Network) do iface = Mash.new network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new so = shell_out("ifconfig -a") cint = nil @@ -106,7 +106,7 @@ Ohai.plugin(:Network) do # regex: https://rubular.com/r/ZiIHbsnfiWPW1p if line =~ /^([0-9a-zA-Z\.\:\-]+): \S+ mtu (\d+)(?: index (\d+))?/ cint = $1 - iface[cint] = Mash.new unless iface[cint] + iface[cint] ||= Mash.new iface[cint][:mtu] = $2 iface[cint][:index] = $3 if line =~ / flags\=\d+\<((ADDRCONF|ANYCAST|BROADCAST|CoS|DEPRECATED|DHCP|DUPLICATE|FAILED|FIXEDMTU|INACTIVE|L3PROTECT|LOOPBACK|MIP|MULTI_BCAST|MULTICAST|NOARP|NOFAILOVER|NOLOCAL|NONUD|NORTEXCH|NOXMIT|OFFLINE|PHYSRUNNING|POINTOPOINT|PREFERRED|PRIVATE|ROUTER|RUNNING|STANDBY|TEMPORARY|UNNUMBERED|UP|VIRTUAL|XRESOLV|IPv4|IPv6|,)+)\>\s/ @@ -122,15 +122,15 @@ Ohai.plugin(:Network) do end end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8})\s*$/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * "." } end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * ".", "broadcast" => $4 } end if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*)\/(\d+)\s*$/ - iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] + iface[cint][:addresses] ||= Mash.new iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 } end end @@ -141,7 +141,7 @@ Ohai.plugin(:Network) do so.stdout.lines do |line| if line =~ /([0-9a-zA-Z]+)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\w+)?\s+([a-zA-Z0-9\.\:\-]+)/ next unless iface[arpname_to_ifname(iface, $1)] # this should never happen, except on solaris because sun hates you. - iface[arpname_to_ifname(iface, $1)][:arp] = Mash.new unless iface[arpname_to_ifname(iface, $1)][:arp] + iface[arpname_to_ifname(iface, $1)][:arp] ||= Mash.new iface[arpname_to_ifname(iface, $1)][:arp][$2] = $5 end end diff --git a/lib/ohai/plugins/windows/network.rb b/lib/ohai/plugins/windows/network.rb index a81a1644..68e08060 100644 --- a/lib/ohai/plugins/windows/network.rb +++ b/lib/ohai/plugins/windows/network.rb @@ -120,13 +120,13 @@ Ohai.plugin(:Network) do iface_config = Mash.new iface_instance = Mash.new network Mash.new unless network - network[:interfaces] = Mash.new unless network[:interfaces] + network[:interfaces] ||= Mash.new counters Mash.new unless counters - counters[:network] = Mash.new unless counters[:network] + counters[:network] ||= Mash.new network_data[:addresses].each do |adapter| i = adapter["index"] || adapter["InterfaceIndex"] - iface_config[i] = Mash.new unless iface_config[i] + iface_config[i] ||= Mash.new iface_config[i][:ip_address] ||= [] iface_config[i][:ip_address] << adapter["IPAddress"] diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb index 3a4e5532..0ce24418 100644 --- a/lib/ohai/plugins/windows/virtualization.rb +++ b/lib/ohai/plugins/windows/virtualization.rb @@ -27,7 +27,7 @@ Ohai.plugin(:Virtualization) do require "wmi-lite/wmi" virtualization Mash.new unless virtualization - virtualization[:systems] = Mash.new unless virtualization[:systems] + virtualization[:systems] ||= Mash.new # Grab system DMI data from WMI to determine vendor information wmi = WmiLite::Wmi.new -- cgit v1.2.1