class Observium::Agent::Poller

Public Class Methods

new(host, **args) click to toggle source
# File lib/observium/agent/poller.rb, line 4
def initialize(host, **args)
  @host    = host
  @port    = args[:port]    || Observium::Agent.defaults[:port]
  @timeout = args[:timeout] || Observium::Agent.defaults[:timeout]
end

Public Instance Methods

poll() click to toggle source

Connects to the given host on the agent port to get the agent output. Connection times out after 5 seconds.

Raises a PollingFailed error if no data was output, or the command failed

Returns the raw output returned by the agent as a multiline string

# File lib/observium/agent/poller.rb, line 16
def poll
  @output = %x(nc -w #{@timeout} #{@host} #{@port} 2>&1)
  
  $?.success? or raise Errors::PollingFailed, "Agent check failed: #{error_msg}"
  has_data?   or raise Errors::PollingFailed, "Agent didn't return any data"

  @output
end

Private Instance Methods

error_msg() click to toggle source

Parses the error message from the netcat error output to stderr

# File lib/observium/agent/poller.rb, line 34
def error_msg
  @output.split(":").last.strip
end
has_data?() click to toggle source

Checks if the agent returned any data, usually signified by more than a single line being present in the output

# File lib/observium/agent/poller.rb, line 29
def has_data?
  @output.lines.count > 1
end