diff --git a/lib/ohai/plugins/network_listeners.rb b/lib/ohai/plugins/network_listeners.rb index 7ecfef72e..62661c773 100644 --- a/lib/ohai/plugins/network_listeners.rb +++ b/lib/ohai/plugins/network_listeners.rb @@ -22,35 +22,39 @@ depends "network", "counters/network" collect_data do - require "sigar" - flags = Sigar::NETCONN_TCP | Sigar::NETCONN_SERVER + begin + require 'sigar' + flags = Sigar::NETCONN_TCP | Sigar::NETCONN_SERVER - network Mash.new unless network - listeners = Mash.new + network Mash.new unless network + listeners = Mash.new - sigar = Sigar.new - sigar.net_connection_list(flags).each do |conn| - port = conn.local_port - addr = conn.local_address.to_s - if addr == "0.0.0.0" || addr == "::" - addr = "*" + sigar = Sigar.new + sigar.net_connection_list(flags).each do |conn| + port = conn.local_port + addr = conn.local_address.to_s + if addr == "0.0.0.0" || addr == "::" + addr = "*" + end + listeners[port] = Mash.new + listeners[port][:address] = addr + begin + pid = sigar.proc_port(conn.type, port) + # workaround for a failure of proc_state to throw + # after the first 0 has been supplied to it + # + # no longer required when hyperic/sigar#48 is fixed + throw ArgumentError.new("No such process") if pid == 0 + listeners[port][:pid] = pid + listeners[port][:name] = sigar.proc_state(pid).name + rescue + end end - listeners[port] = Mash.new - listeners[port][:address] = addr - begin - pid = sigar.proc_port(conn.type, port) - # workaround for a failure of proc_state to throw - # after the first 0 has been supplied to it - # - # no longer required when hyperic/sigar#48 is fixed - throw ArgumentError.new("No such process") if pid == 0 - listeners[port][:pid] = pid - listeners[port][:name] = sigar.proc_state(pid).name - rescue - end - end - network[:listeners] = Mash.new - network[:listeners][:tcp] = listeners + network[:listeners] = Mash.new + network[:listeners][:tcp] = listeners + rescue LoadError + Ohai::Log.debug('Could not load sigar gem. Skipping NetworkListeners plugin') + end end end diff --git a/lib/ohai/plugins/sigar/network_route.rb b/lib/ohai/plugins/sigar/network_route.rb index eaed40528..f4d3161fa 100644 --- a/lib/ohai/plugins/sigar/network_route.rb +++ b/lib/ohai/plugins/sigar/network_route.rb @@ -19,41 +19,46 @@ require "ohai/mixin/network_constants" Ohai.plugin(:NetworkRoutes) do - include Ohai::Mixin::NetworkConstants + begin + require "sigar" - provides "network/interfaces/adapters/route" - depends "network/interfaces" + include Ohai::Mixin::NetworkConstants - def flags(flags) - f = "" - if (flags & Sigar::RTF_UP) != 0 - f += "U" - end - if (flags & Sigar::RTF_GATEWAY) != 0 - f += "G" - end - if (flags & Sigar::RTF_HOST) != 0 - f += "H" + provides "network/interfaces/adapters/route" + depends "network/interfaces" + + def flags(flags) + f = "" + if (flags & Sigar::RTF_UP) != 0 + f += "U" + end + if (flags & Sigar::RTF_GATEWAY) != 0 + f += "G" + end + if (flags & Sigar::RTF_HOST) != 0 + f += "H" + end + f end - f - end - collect_data(:default) do - require "sigar" - sigar = Sigar.new + collect_data(:default) do + sigar = Sigar.new - sigar.net_route_list.each do |route| - next unless network[:interfaces][route.ifname] # this should never happen - network[:interfaces][route.ifname][:route] = Mash.new unless network[:interfaces][route.ifname][:route] - route_data = {} - Ohai::Mixin::NetworkConstants::SIGAR_ROUTE_METHODS.each do |m| - if(m == :flags) - route_data[m] = flags(route.send(m)) - else - route_data[m] = route.send(m) + sigar.net_route_list.each do |route| + next unless network[:interfaces][route.ifname] # this should never happen + network[:interfaces][route.ifname][:route] = Mash.new unless network[:interfaces][route.ifname][:route] + route_data = {} + Ohai::Mixin::NetworkConstants::SIGAR_ROUTE_METHODS.each do |m| + if(m == :flags) + route_data[m] = flags(route.send(m)) + else + route_data[m] = route.send(m) + end end + network[:interfaces][route.ifname][:route][route.destination] = route_data end - network[:interfaces][route.ifname][:route][route.destination] = route_data end + rescue LoadError + Ohai::Log.debug('Could not load sigar gem. Skipping NetworkRoutes plugin') end end