module Riemann::Monitors

Constants

VERSION
VERSION_MAJOR
VERSION_MINOR
VERSION_TINY

Public Class Methods

included(base) click to toggle source
# File lib/riemann-monitors/main.rb, line 9
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 :ssl, "Use SSL.", default: false
    opt :ssl_ca_file, "SSL certificate authority cert", :default => File.join(Dir.home, ".config", "riemann-tools", "ca.crt")
    opt :ssl_cert_file, "SSL client certificate public key", :default => File.join(Dir.home, ".config", "riemann-tools", "#{Socket.gethostname}.crt")
    opt :ssl_key_file, "SSL client certificate private key", :default => File.join(Dir.home, ".config", "riemann-tools", "#{Socket.gethostname}.key")
  end
end

Public Instance Methods

attributes() click to toggle source
# File lib/riemann-monitors/main.rb, line 53
def attributes
  @attributes ||= Hash[options[:attribute].map do |attr|
    k,v = attr.split(/=/)
    if k and v
      [k.to_sym,v]
    end
  end]
end
new_riemann_client() click to toggle source
# File lib/riemann-monitors/main.rb, line 70
def new_riemann_client
  riemann_options = {
    :server  => "#{options[:host]}:#{options[:port]}",
    :connect_timeout => options[:timeout]
  }
  if options.has_keys?(:ssl_ca_file, :ssl_cert_file, :ssl_key_file) && options[:ssl]
    # These are given to OpenSSL::SSL::SSLContext
    riemann_options[:ssl] = {
      ca_file: File.expand_path(options[:ssl_ca_file]),
      cert:    OpenSSL::X509::Certificate.new(File.read(File.expand_path(options[:ssl_cert_file]))),
      key:     OpenSSL::PKey::RSA.new(File.read(File.expand_path(options[:ssl_key_file]))),
      verify_mode: OpenSSL::SSL::VERIFY_PEER,
      ssl_version: :TLSv1_2
    }
  end
  Riemann::Experiment::Client.new(riemann_options)
end
opt(*a) click to toggle source
# File lib/riemann-monitors/main.rb, line 15
def opt(*a)
  a.unshift :opt
  @opts ||= []
  @opts << a
end
options() click to toggle source
# File lib/riemann-monitors/main.rb, line 21
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_hash) click to toggle source
# File lib/riemann-monitors/main.rb, line 62
def report(event_hash)
  event_hash[:tags] = (event_hash[:tags] || []) + (options[:tag] || [])
  event_hash[:ttl] ||= (options[:ttl] || (options[:interval] * 2))
  event_hash[:host] ||= options[:event_host]
  event_hash.merge!(attributes)
  riemann.add_event(event_hash)
end
riemann() click to toggle source
# File lib/riemann-monitors/main.rb, line 88
def riemann
  @riemann ||= new_riemann_client
end
Also aliased as: r
run() click to toggle source
# File lib/riemann-monitors/main.rb, line 11
def run
  new.run
end
tick() click to toggle source
# File lib/riemann-monitors/main.rb, line 108
def tick
end