Module: Riemann::Tools
- Defined in:
- lib/riemann/tools.rb
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
-
#options ⇒ Object
(also: #opts)
Returns parsed options (cached) from command line.
- #report(event) ⇒ Object
- #riemann ⇒ Object (also: #r)
- #run ⇒ Object
- #tick ⇒ Object
Class Method Details
.included(base) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 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 end end |
Instance Method Details
#attributes ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/riemann/tools.rb', line 47 def attributes @attributes ||= Hash[[:attribute].map do |attr| k,v = attr.split(/=/) if k and v [k,v] end end] end |
#options ⇒ Object Also known as: opts
Returns parsed options (cached) from command line.
42 43 44 |
# File 'lib/riemann/tools.rb', line 42 def @options ||= self.class. end |
#report(event) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/riemann/tools.rb', line 56 def report(event) if [:tag] # Work around a bug with beefcake which can't take frozen strings. event[:tags] = [:tag].map(&:dup) end if [:ttl] event[:ttl] = [:ttl] end if [:event_host] event[:host] = [:event_host].dup end event = event.merge(attributes) begin Timeout::timeout([:timeout]) do riemann << event end rescue Timeout::Error riemann.connect end end |
#riemann ⇒ Object Also known as: r
81 82 83 84 85 86 |
# File 'lib/riemann/tools.rb', line 81 def riemann @riemann ||= Riemann::Client.new( :host => [:host], :port => [:port] ) end |
#run ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/riemann/tools.rb', line 89 def run t0 = Time.now loop do begin tick rescue => e $stderr.puts "#{e.class} #{e}\n#{e.backtrace.join "\n"}" end # Sleep. sleep([:interval] - ((Time.now - t0) % [:interval])) end end |
#tick ⇒ Object
103 104 |
# File 'lib/riemann/tools.rb', line 103 def tick end |