class Echo_cli::Helper

Public Instance Methods

check_for_IP() click to toggle source
# File lib/echo_cli/cli.rb, line 251
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) click to toggle source
# File lib/echo_cli/cli.rb, line 286
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) click to toggle source
# File lib/echo_cli/cli.rb, line 305
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
do_post_udp(statsd_uri, port, request_body) click to toggle source
# File lib/echo_cli/cli.rb, line 295
def do_post_udp(statsd_uri, port, request_body)
  begin
    udpsocket = UDPSocket.new
    udpsocket.connect(statsd_uri, port)
    udpsocket.write(request_body)
    udpsocket.close
  rescue Errno::ECONNREFUSED
  end
end
get_metric_string(metric) click to toggle source
# File lib/echo_cli/cli.rb, line 258
def get_metric_string(metric)
  type = metric[metric.index('|') + 1 .. metric.length]
  case type
  when 'c'
    return metric[0..metric.index(':')-1] + ".*"
  when 'ms'
    return metric[0..metric.index(':')-1] + ".*"
  when 's'
    return metric[0..metric.index(':')-1] + ".*"
  when 'g'
    return metric[0..metric.index(':')-1]
  end
end
get_metric_string_populate(metric) click to toggle source
# File lib/echo_cli/cli.rb, line 272
def get_metric_string_populate(metric)
  type = metric[metric.index('|') + 1 .. metric.length]
  case type
  when 'c'
    return metric[0..metric.index(':')-1] + ".count"
  when 'ms'
    return metric[0..metric.index(':')-1] + ".count"
  when 's'
    return metric[0..metric.index(':')-1] + ".count"
  when 'g'
    return metric[0..metric.index(':')-1]
  end
end
query_graphite(graphite_uri) click to toggle source
# File lib/echo_cli/cli.rb, line 319
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_envs() click to toggle source
# File lib/echo_cli/cli.rb, line 241
def set_envs
  ENV["http_proxy"] = ENV["HTTP_PROXY"]
  ENV["https_proxy"] = ENV["HTTPS_PROXY"]
end
stfu() { || ... } click to toggle source

useful silencing function, plz leave here

# File lib/echo_cli/cli.rb, line 223
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_envs() click to toggle source
# File lib/echo_cli/cli.rb, line 246
def unset_envs
  ENV["http_proxy"] = ENV[""]
  ENV["https_proxy"] = ENV[""]
end