module Riemann::Babbler::Plugins::Helpers

Constants

RIEMANN_RESERVED_FIELDS

Public Instance Methods

event_from_hash(hash=nil) click to toggle source
# File lib/riemann/babbler/plugins/helpers/event.rb, line 19
def event_from_hash(hash=nil)
  if hash
    new_hash = Hash.new
    RIEMANN_RESERVED_FIELDS.each do |key|
      new_hash[key] = hash[key] || hash[key.to_s]
    end
    new_hash
  else
    Hash.new
  end
end
event_from_json(str) click to toggle source
# File lib/riemann/babbler/plugins/helpers/event.rb, line 31
def event_from_json(str)
  event_from_hash(JSON.parse(str))
end
rest_get(url) click to toggle source

http rest

# File lib/riemann/babbler/plugins/helpers/rest.rb, line 11
def rest_get(url)
  begin
    Timeout::timeout(plugin.timeout) do
      begin
        uri = URI(url)
        if uri.userinfo
          open("#{uri.scheme}://#{uri.hostname}:#{uri.port}#{uri.request_uri}", 
            :http_basic_authentication => [uri.user, uri.password]).read
        else
          open(url).read
        end
      rescue
        raise "Get from url: #{url} failed"
      end
    end
  rescue Timeout::Error
    raise "Get from url: #{url}, timeout error"
  end
end
shell(*cmd) click to toggle source

helper: stdout+stderr и exit status

# File lib/riemann/babbler/plugins/helpers/shell.rb, line 11
def shell(*cmd)
  exit_status=nil
  err        =nil
  out        =nil
  Timeout::timeout(plugin.timeout) {
    Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
      err = stderr.gets(nil)
      out = stdout.gets(nil)
      [stdin, stdout, stderr].each { |stream| stream.send('close') }
      exit_status = wait_thread.value
    end
  }
  if exit_status.to_i > 0
    err = err.chomp if err
    raise 'Error while running shell: ' + err.to_s
  elsif out
    return out.strip
  else
    # exit status 0, no stdout
    ''
  end
end
unixnow() click to toggle source
# File lib/riemann/babbler/plugins/helpers/unixnow.rb, line 6
def unixnow
  Time.now.to_i
end