Module: Riemann::Tools

Defined in:
lib/riemann/tools.rb

Class Method Summary collapse

Instance Method Summary collapse

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 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
  end
end

Instance Method Details

#attributesObject



47
48
49
50
51
52
53
54
# File 'lib/riemann/tools.rb', line 47

def attributes
  @attributes ||= Hash[options[:attribute].map do |attr|
    k,v = attr.split(/=/)
    if k and v
      [k,v]
    end
  end]
end

#optionsObject Also known as: opts

Returns parsed options (cached) from command line.



42
43
44
# File 'lib/riemann/tools.rb', line 42

def options
  @options ||= self.class.options
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 options[:tag]
    # Work around a bug with beefcake which can't take frozen strings.
    event[:tags] = options[:tag].map(&:dup)
  end

  if options[:ttl]
    event[:ttl] = options[:ttl]
  end

  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

#riemannObject Also known as: r



81
82
83
84
85
86
# File 'lib/riemann/tools.rb', line 81

def riemann
  @riemann ||= Riemann::Client.new(
    :host => options[:host],
    :port => options[:port]
  )
end

#runObject



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(options[:interval] - ((Time.now - t0) % options[:interval]))
  end
end

#tickObject



103
104
# File 'lib/riemann/tools.rb', line 103

def tick
end