module Riemann::Tools

Public Class Methods

included(base) click to toggle source
# File lib/riemann/tools.rb, line 8
def self.included(base)
  base.instance_eval do
    def run
      new.run
    end

    def opt(*a)
      a.unshift :opt
      @opts ||= []
      @opts << a
    end

    def options
      p = Trollop::Parser.new
      @opts.each do |o|
        p.send *o
      end
      Trollop::with_standard_exception_handling(p) do
        p.parse ARGV
      end
    end

    opt :host, "Riemann host", :default => '127.0.0.1'
    opt :port, "Riemann port", :default => 5555
    opt :event_host, "Event hostname", :type => String
    opt :interval, "Seconds between updates", :default => 5
    opt :tag, "Tag to add to events", :type => String, :multi => true
    opt :ttl, "TTL for events", :type => Integer
    opt :attribute, "Attribute to add to the event", :type => String, :multi => true
    opt :timeout, "Timeout (in seconds) when waiting for acknowledgements", :default => 30
    opt :tcp, "Use TCP transport instead of UDP (improves reliability, slight overhead.", :default => true
    opt :daemon, "Run in background", :default => false
    opt :logfile, "logfile path", :type => String, :default => '/tmp/riemann-tools.log'
    opt :pidfile, "pidfile path", :type => String, :default => '/tmp/riemann-tools.pid'
  end
end

Public Instance Methods

attributes() click to toggle source
# File lib/riemann/tools.rb, line 51
def attributes
  @attributes ||= Hash[options[:attribute].map do |attr|
    k,v = attr.split(/=/)
    if k and v
      [k,v]
    end
  end]
end
daemonize() click to toggle source
# File lib/riemann/tools.rb, line 115
def daemonize
  exit if fork
  Process.setsid
  $0 = self.class.name.downcase.gsub('::','_')
  $stdout.reopen(opts[:logfile], 'w')
  $stdout.sync = true
  $stderr.reopen($stdout)
  exit if fork
  f = File.new(opts[:pidfile], 'w')
  f.write Process.pid
  f.close
end
new_riemann_client() click to toggle source
# File lib/riemann/tools.rb, line 83
def new_riemann_client
  r = Riemann::Client.new(
    :host => options[:host],
    :port => options[:port]
  )
  if options[:tcp]
    r.tcp
  else
    r
  end
end
opt(*a) click to toggle source
# File lib/riemann/tools.rb, line 14
def opt(*a)
  a.unshift :opt
  @opts ||= []
  @opts << a
end
options() click to toggle source
# File lib/riemann/tools.rb, line 20
def options
  p = Trollop::Parser.new
  @opts.each do |o|
    p.send *o
  end
  Trollop::with_standard_exception_handling(p) do
    p.parse ARGV
  end
end
Also aliased as: opts
opts()
Alias for: options
r()
Alias for: riemann
report(event) click to toggle source
# File lib/riemann/tools.rb, line 60
def report(event)
  if options[:tag]
    # Work around a bug with beefcake which can't take frozen strings.
    event[:tags] = options[:tag].map(&:dup)
  end

  event[:ttl] ||= (options[:ttl] || (options[:interval] * 2))

  if options[:event_host]
    event[:host] = options[:event_host].dup
  end

  event = event.merge(attributes)

  begin
    Timeout::timeout(options[:timeout]) do
      riemann << event
    end
  rescue Timeout::Error
    riemann.connect
  end
end
riemann() click to toggle source
# File lib/riemann/tools.rb, line 95
def riemann
  @riemann ||= new_riemann_client
end
Also aliased as: r
run() click to toggle source
# File lib/riemann/tools.rb, line 10
def run
  new.run
end
tick() click to toggle source
# File lib/riemann/tools.rb, line 128
def tick
end