Class: Echo_cli::Echo

Inherits:
Thor
  • Object
show all
Defined in:
lib/echo_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#convert(timestamp) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/echo_cli/cli.rb', line 142

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

#nowObject



166
167
168
169
# File 'lib/echo_cli/cli.rb', line 166

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

#post(metric, num = 1) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/echo_cli/cli.rb', line 36

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_tcp(ENV["ECHO_CLI_HOST"], "8125", metric)

    pid = Process.fork do
      while true do
        helper.do_post_tcp(ENV["ECHO_CLI_HOST"], "8125", metric)
        sleep 1
      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_tcp(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') ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/echo_cli/cli.rb', line 101

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