class Echo_cli::Echo

Public Instance Methods

convert(timestamp) click to toggle source
# File lib/echo_cli/cli.rb, line 191
def convert(timestamp)
  if timestamp.include? ":"
    time = Time.iso8601(timestamp).to_i.to_s
  else
    time = Time.at(timestamp.to_i).to_datetime.to_s
  end
  puts "\nConverted timstamp: ".green + time.yellow
  return time
end
now() click to toggle source
# File lib/echo_cli/cli.rb, line 215
def now
  timestamp = Time.new.to_i.to_s
  puts "\nCurrent Epoch time: ".green + timestamp.to_s.yellow + "\nCurrent time: ".green + Time.at(timestamp.to_i).to_datetime.to_s.yellow
end
populate(metric, timespan = 1209600, frequency = 600) click to toggle source
# File lib/echo_cli/cli.rb, line 148
def populate(metric, timespan = 1209600, frequency = 600)
  helper = Helper.new
  helper.set_envs
  metric = metric.gsub(/'/,"").gsub(/"/,"")
  exit if !helper.check_metric_format(metric)

  now = Time.new.to_i
  prng = Random.new

  puts "\nWARNING: You are about to back-populate a metric.\nAre you sure you want to back-populate ".red + metric.yellow + " for the last ".red + timespan.to_s.yellow + " seconds?".red
  input = ask "[y/n]".red

  if input == "y"
    puts "\nPopulating..."
    timespan = timespan.to_i
    frequency = frequency.to_i
    while timespan >= 0
      to_post = helper.get_metric_string_populate(metric) + " " + prng.rand(0...100).to_s + " " + (now - timespan).to_s
      helper.do_post_tcp(ENV["ECHO_CLI_HOST"], "2003", to_post)
      timespan = timespan - frequency
    end
    puts "Complete."
  else
    exit
  end
end
post(metric, num = 1) click to toggle source
# File lib/echo_cli/cli.rb, line 37
def post(metric, num = 1)
  helper = Helper.new
  helper.set_envs()
  helper.check_for_IP()

  metric = metric.gsub(/'/,"").gsub(/"/,"")
  exit if !helper.check_metric_format(metric)

  if options[:z]
    time_start = Time.new.to_i.to_s

    # sort of a mini-smoketest to ensure the posts will work before creating a background process
    helper.do_post_udp(ENV["ECHO_CLI_HOST"], "8125", metric)

    pid = Process.fork do
      prng = Random.new
      while true do
        helper.do_post_udp(ENV["ECHO_CLI_HOST"], "8125", metric)
        sleep prng.rand(0.1...1.0)
      end
    end
    puts "\nProcess ID: ".green + pid.to_s.yellow + "\nUse ".green + "$ kill ".yellow + pid.to_s.yellow + " to kill the zombie process.".green
    puts "Process began posting at ".green + time_start.yellow
  else
    num = num.to_i
    if num <= 0
      puts "\nThe number of posts specified is an incorrect expression, or is less than or equal to zero!".red
      exit
    end

    while num > 0
      helper.do_post_udp(ENV["ECHO_CLI_HOST"], "8125", metric)
      puts "\nPosted ".green + metric.yellow + " to ".green + "http://".yellow + ENV["ECHO_CLI_HOST"].yellow + ":".yellow + "8125".yellow + " at ".green + Time.new.to_i.to_s.yellow
      num -= 1
    end
  end

  helper.unset_envs()
end
query(metric, time_start = '-15min', time_end = 'now') click to toggle source
# File lib/echo_cli/cli.rb, line 103
def query(metric, time_start = '-15min', time_end = 'now')
  helper = Helper.new
  helper.set_envs()
  helper.check_for_IP()
  metric = metric.gsub(/'/,"").gsub(/"/,"")
  exit if !helper.check_metric_format(metric)

  query_url = 'http://' + ENV["ECHO_CLI_HOST"] + ':8080/render?target=' + helper.get_metric_string(metric) + '&from=' + time_start + '&until=' + time_end + '&format=json'

  response = helper.query_graphite(query_url)
  if(response.code != '200')
    puts "\nThe query returned a status code of ".red + response.code.yellow + ". If the query has start or end times, ensure they are formatted correctly.".red
    puts "Otherwise, check your metric query formatting and the 'ECHO_CLI_HOST' environment variable value.".red
    puts "\nResponse Body:\n".red
    puts response.body
  else
    res = JSON.parse(response.body)
    if(!options[:n])
      res.each_with_index{|target, index| res[index]["datapoints"].delete_if{|value| value[0] == nil}}
    end
    puts "\nSuccessful Query:\n\n".green + JSON.pretty_generate(res).to_s.green
  end
  helper.unset_envs()
end