Sha256: 1363777ad5b07f5333b7a5a801e8cb192f8760e32b7465b716501f5cbf6cbb3d

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

module Riemann
  module Tools
    require 'rubygems'
    require 'trollop'
    require 'riemann/client'

    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 :interval, "Seconds between updates", :default => 5
      end
    end

    def initialize
      super
    end

    def tool_options
      {}
    end

    # Returns parsed options (cached) from command line.
    def options
      @options ||= self.class.options
    end
    alias :opts :options

    # Add a new command line option
    def opt(*a)
      @option_parser.opt *a
    end

    def report(event)
      riemann << event
    end

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

    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

    def tick
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
riemann-tools-0.0.8 lib/riemann/tools.rb
riemann-tools-0.0.7 lib/riemann/tools.rb
riemann-tools-0.0.6 lib/riemann/tools.rb
riemann-tools-0.0.5 lib/riemann/tools.rb
riemann-tools-0.0.4 lib/riemann/tools.rb
riemann-tools-0.0.3 lib/riemann/tools.rb
riemann-tools-0.0.2 lib/riemann/tools.rb