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



249
250
251
252
253
254
# File 'lib/echo_cli/cli.rb', line 249

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



284
285
286
287
288
289
290
291
# File 'lib/echo_cli/cli.rb', line 284

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



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/echo_cli/cli.rb', line 293

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

#get_metric_string(metric) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/echo_cli/cli.rb', line 256

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

#get_metric_string_populate(metric) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/echo_cli/cli.rb', line 270

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

#query_graphite(graphite_uri) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/echo_cli/cli.rb', line 307

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



239
240
241
242
# File 'lib/echo_cli/cli.rb', line 239

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

#stfuObject

useful silencing function, plz leave here



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/echo_cli/cli.rb', line 221

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



244
245
246
247
# File 'lib/echo_cli/cli.rb', line 244

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