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



132
133
134
135
136
137
138
139
140
# File 'lib/echo_cli/cli.rb', line 132

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



156
157
158
159
# File 'lib/echo_cli/cli.rb', line 156

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(/'/,"")
  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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/echo_cli/cli.rb', line 95

def query(metric, time_start = '-15min', time_end = 'now')
  helper = Helper.new
  helper.set_envs()
  helper.check_for_IP()
  metric = metric.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
    puts "\nSuccessful Query:\n\n".green + JSON.parse(response.body).to_s.green
  end
  helper.unset_envs()
end