Class: Echo_cli::Helper

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

Instance Method Summary collapse

Instance Method Details

#check_get_tags(metric) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/echo_cli/cli.rb', line 394

def check_get_tags(metric)
  tags = ""

  if metric.include? "."
    tags = metric[metric.index(".")+1..-1] + "."
    metric = metric[0..metric.index(".")-1]

    toReturn = ""
    count = 1
    while tags.include?(".") do
      toReturn = toReturn + "\"tag" + count.to_s + "\": \"" + tags[0..tags.index(".")-1] + "\""
      count = count + 1
      tags = tags[tags.index(".")+1..-1]
      if tags.include?(".")
        toReturn = toReturn + ",\n"
      end
    end

    tags = toReturn
  end
  return tags
end

#get_metric(metric) ⇒ Object



417
418
419
420
421
422
423
# File 'lib/echo_cli/cli.rb', line 417

def get_metric(metric)
  metric = metric.gsub(/'/,"")
  if metric.include? ":"
    metric = metric[0..metric.index(":")-1]
  end
  return metric
end

#get_metric_name(metric) ⇒ Object



425
426
427
428
429
430
# File 'lib/echo_cli/cli.rb', line 425

def get_metric_name(metric)
  if metric.include?(".")
    metric = metric[0..metric.index(".")-1]
  end
  return metric
end

#get_query(time_start, time_end, metric, tags) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/echo_cli/cli.rb', line 432

def get_query(time_start, time_end, metric, tags)
  opentsdb_query =
  "{
    \"start\": " + time_start + ",
    \"end\": " + time_end + ",
    \"queries\": [
      {
      \"aggregator\": \"sum\",
      \"metric\": \"" + metric + "\",
      \"tags\": {" + tags +
        "}
      }
    ]
  }"
  return opentsdb_query
end

#set_envsObject



449
450
451
452
# File 'lib/echo_cli/cli.rb', line 449

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

#stfuObject



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/echo_cli/cli.rb', line 376

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



454
455
456
457
# File 'lib/echo_cli/cli.rb', line 454

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