module Riemann::Tools

Attributes

argv[R]

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(*args)
      args.unshift :opt
      @opts ||= []
      @opts << args
    end

    def options
      p = Optimist::Parser.new
      @opts.each do |o|
        p.send(*o)
      end
      Optimist.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 (twice the interval when unspecified)', type: Integer
    opt :minimum_ttl, 'Minimum TTL for events', type: Integer, short: :none
    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 :tls, 'Use TLS for securing traffic', default: false
    opt :tls_key, 'TLS Key to use when using TLS', type: String
    opt :tls_cert, 'TLS Certificate to use when using TLS', type: String
    opt :tls_ca_cert, 'Trusted CA Certificate when using TLS', type: String
    opt :tls_verify, 'Verify TLS peer when using TLS', default: true
  end
end
new(allow_arguments: false) click to toggle source
# File lib/riemann/tools.rb, line 50
def initialize(allow_arguments: false)
  options
  @argv = ARGV.dup
  abort "Error: stray arguments: #{ARGV.map(&:inspect).join(', ')}" if ARGV.any? && !allow_arguments

  options[:ttl] ||= options[:interval] * 2
  options[:ttl] = [options[:minimum_ttl], options[:ttl]].compact.max
end

Public Instance Methods

attributes() click to toggle source
# File lib/riemann/tools.rb, line 65
def attributes
  @attributes ||= options[:attribute].to_h do |attr|
    k, v = attr.split('=')
    [k, v] if k && v
  end
end
endpoint_name(address, port) click to toggle source
# File lib/riemann/tools.rb, line 105
def endpoint_name(address, port)
  if address.ipv6?
    "[#{address}]:#{port}"
  else
    "#{address}:#{port}"
  end
end
opt(*args) click to toggle source
# File lib/riemann/tools.rb, line 14
def opt(*args)
  args.unshift :opt
  @opts ||= []
  @opts << args
end
options() click to toggle source
# File lib/riemann/tools.rb, line 20
def options
  p = Optimist::Parser.new
  @opts.each do |o|
    p.send(*o)
  end
  Optimist.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 72
def report(event)
  event[:tags] = event.fetch(:tags, []) + options[:tag]

  event[:ttl] ||= options[:ttl]

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

  event = event.merge(attributes)

  riemann << event
end
riemann() click to toggle source
# File lib/riemann/tools.rb, line 84
def riemann
  @riemann ||= RiemannClientWrapper.new(options)
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 103
def tick; end