Skip to content

Commit

Permalink
Log sigar gem load failures
Browse files Browse the repository at this point in the history
These two plugins run on every system and spam debug logs. There are other plugins that use sigar, but they're confined to HPUX where you need sigar for ohai to operate.
  • Loading branch information
tas50 committed Feb 15, 2016
1 parent ac9f5a2 commit 2724ecc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
56 changes: 30 additions & 26 deletions lib/ohai/plugins/network_listeners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 33 additions & 28 deletions lib/ohai/plugins/sigar/network_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2724ecc

Please sign in to comment.