Class: Echo_cli::Helper

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

Instance Method Summary collapse

Instance Method Details

#check_for_IPObject



202
203
204
205
206
207
# File 'lib/echo_cli/cli.rb', line 202

def check_for_IP
  if !ENV["ECHO_CLI_HOST"] || ENV["ECHO_CLI_HOST"] == ''
    puts "\nFailed to find IP or URL. Please set the environment variable: ".red + "'ECHO_CLI_HOST'".yellow + " to continue.".red
    exit
  end
end

#check_metric_format(metric) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/echo_cli/cli.rb', line 223

def check_metric_format(metric)
  if /.*(:\d*\.?\d*)\|(ms|c|g|s)/.match(metric).to_s != metric
    puts "\nThe posted metric was not of the correct form.\nMetrics must be of the form ".red + "<Application_name>.<tag1>.<tag2>...<tag(n)>:<value>|<metric_type>".yellow
    puts "Where <value> is a number, and <type> is any of the following: 'ms', 'c', 's', 'g'.".red
    return false
  end
  return true
end

#do_post_tcp(statsd_uri, port, request_body) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/echo_cli/cli.rb', line 232

def do_post_tcp(statsd_uri, port, request_body)
  begin
    timeout(10) do
      socket = TCPSocket.new(statsd_uri, port)
      socket.puts(request_body)
    end
  rescue Timeout::Error
    puts "\nOperation timed out! Ensure the 'ECHO_CLI_HOST' environment variable is correct.".red
    exit
  end
end

#get_metric_string(metric) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/echo_cli/cli.rb', line 209

def get_metric_string(metric)
  type = metric[metric.index('|') + 1 .. metric.length]
  case type
  when 'c'
    return "stats.counters." + metric[0..metric.index(':')-1] + ".*"
  when 'ms'
    return "stats.timers." + metric[0..metric.index(':')-1] + ".*"
  when 's'
    return "stats.sets." + metric[0..metric.index(':')-1] + ".*"
  when 'g'
    return "stats.gauges." + metric[0..metric.index(':')-1]
  end
end

#query_graphite(graphite_uri) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/echo_cli/cli.rb', line 244

def query_graphite(graphite_uri)
  uri = URI.parse(graphite_uri)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = false
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE if https.use_ssl?
  req = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'}, )
  res = https.request(req)
  return res
end

#set_envsObject



192
193
194
195
# File 'lib/echo_cli/cli.rb', line 192

def set_envs
  ENV["http_proxy"] = ENV["HTTP_PROXY"]
  ENV["https_proxy"] = ENV["HTTPS_PROXY"]
end

#stfuObject

useful silencing function, plz leave here



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/echo_cli/cli.rb', line 174

def stfu
  begin
    orig_stderr = $stderr.clone
    orig_stdout = $stdout.clone
    $stderr.reopen File.new('/dev/null', 'w')
    $stdout.reopen File.new('/dev/null', 'w')
    retval = yield
  rescue Exception => e
    $stdout.reopen orig_stdout
    $stderr.reopen orig_stderr
    raise e
  ensure
    $stdout.reopen orig_stdout
    $stderr.reopen orig_stderr
  end
  retval
end

#unset_envsObject



197
198
199
200
# File 'lib/echo_cli/cli.rb', line 197

def unset_envs
  ENV["http_proxy"] = ENV[""]
  ENV["https_proxy"] = ENV[""]
end